File: archive_mail

package info (click to toggle)
linpac 0.28-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: cpp: 18,310; sh: 10,697; ansic: 4,036; makefile: 210; perl: 101
file content (150 lines) | stat: -rwxr-xr-x 3,708 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
#
# LinPac mail archivation tool
# (c) 1999 by Radek Burget OK2JBG
#
# This script archives messages older than specified number of days
# and deletes them
#
LIST_PATH="/var/ax25/ulistd"
MAIL_PATH="/var/ax25/mail"

###########################################################################
# Print a list of messages ################################################
message_list()
{
  awk '
       {
         printf "%s ", $1
       }
      ' ${LIST_PATH}/$BBS_CALL
}

###########################################################################
# List new messages #######################################################
list_new()
{
  awk -v "LIMIT=$1" '
       {
         if (LIMIT <= $1) print $0
       }
      ' ${LIST_PATH}/$BBS_CALL
}

###########################################################################
# List old messages #######################################################
list_old()
{
  awk -v "LIMIT=$1" '
       {
         if (LIMIT > $1) print $0
       }
      ' ${LIST_PATH}/$BBS_CALL
}

###########################################################################
# Print a list of messages older than $1 and files exist
###########################################################################
older_messages()
{
  for NUM in `message_list`; do
    if [ $1 -gt $NUM ]; then
      if [ -e ${MAIL_PATH}/$BBS_CALL/$NUM ]; then
        echo $NUM
      fi
    else
      exit 0
    fi
  done
}

###########################################################################
# Print a date of this message ############################################
message_date()
{
  awk '
       BEGIN {
         FS = "[:/]"
       }

       /^R:/ {
         if (FNR == 1) print $2
       }
      ' ${MAIL_PATH}/$BBS_CALL/$1
}

###########################################################################
# Print the first message number where the date of the message ############
# is newer than specified number of days. #################################
first_message()
{
  # date of last message
  LASTDATE="0"

  # scan all messages
  for NUM in `message_list`
  do
    if [ -r ${MAIL_PATH}/${BBS_CALL}/$NUM ]; then
      LASTDATE=`message_date $NUM`
      if [ ! $MDATE -gt $LASTDATE ]; then
        echo $NUM
        exit 0
      fi
    fi
  done

  # nothing has been found - return last+1
  echo $(($NUM+1))
  exit 0
}

###########################################################################
###########################################################################

if [ ! $# -eq 2 ]; then
  echo 'Usage: archive_mail <bbs_callsign> <days>'
  echo archives messages older than specified number of days and deletes them
  exit 1
fi

BBS_CALL=`echo $1 | awk '{ print toupper($0) }'`
MDATE=`date --date "$2 days ago" '+%y%m%d'`

if [ ! -r ${LIST_PATH}/$BBS_CALL ]; then
  echo List for $BBS_CALL doesn\'t exist
  exit 2
fi

if [ ! -d ${MAIL_PATH}/$BBS_CALL ]; then
  echo Mail directory for $BBS_CALL doesn\'t exist
  exit 3
fi

echo Checking message dates
FIRSTNUM=`first_message`
echo Archiving messages up to number $FIRSTNUM
ARCNAME=${MAIL_PATH}/${BBS_CALL}/archive_`date +%y%m%d`.tar.gz
echo Archiving to "\"$ARCNAME\""

if [ -e $ARCNAME ]; then
  echo Todays archive already exists - exiting
  exit 1
fi

cd ${MAIL_PATH}/${BBS_CALL}
tar --remove-files -cvz -f $ARCNAME `older_messages $FIRSTNUM` >/dev/null
cd $OLDPWD

echo Updating mail index ${LIST_PATH}/${BBS_CALL}

if [ -e ${LIST_PATH}/${BBS_CALL}.old.gz ]; then
  gzip -d ${LIST_PATH}/${BBS_CALL}.old.gz
fi

list_old $FIRSTNUM >>${LIST_PATH}/${BBS_CALL}.old
list_new $FIRSTNUM >>${LIST_PATH}/${BBS_CALL}.new
mv ${LIST_PATH}/${BBS_CALL}.new ${LIST_PATH}/${BBS_CALL}

gzip ${LIST_PATH}/${BBS_CALL}.old

echo Done.