-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-mailrc.sh
executable file
·133 lines (123 loc) · 2.54 KB
/
setup-mailrc.sh
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
#!/bin/sh
#
# keywords
#
gmail_pass=""
outlook_pass=""
work_pass=""
procmail_bin=""
sed_bin="sed -r"
#
# cross platform sed
#
function guess_sed {
local platform=$(uname)
case $platform in
Linux)
;;
Darwin|FreeBSD)
sed_bin="sed -E"
;;
esac
}
#
# this function load the keys from keys.txt file if needed
#
function load_keys {
if [ -f ./keys.txt ]; then
source ./keys.txt
fi
}
#
# get the procmail path
#
function procmail_path {
if [ -z "$procmail_bin"] ; then
procmail_bin=$(type -p procmail)
if [ -z "$procmail_bin" ]; then
echo "ERROR: can't find procmail binary, probably not installed?"
exit 1
fi
fi
}
#
# This function does the actual patch
#
function do_patch {
local src=$1
local dst=$2
local mode=$3
if [ ! -f $dst -o ! -s $dst ]; then
load_keys
$sed_bin \
-e "s|<gmail_pass>|$gmail_pass|g" \
-e "s|<outlook_pass>|$outlook_pass|g" \
-e "s|<work_pass>|$work_pass|g" \
-e "s|<procmail>|$procmail_bin|g" \
-e "s|<USER>|$USER|g" \
< $src \
> $dst
if [ ! -z "$mode" ]; then
chmod $mode $dst
fi
fi
}
#
# Patch the rc files
#
# I am using '|' instead of '/' for sed because $procmail_bin
# contains '/' which invalidates sed command
#
function patch_rcfiles {
do_patch dotforward ~/.forward
do_patch dotfetchmailrc ~/.fetchmailrc 600
if type -p esmtp > /dev/null; then
do_patch dotesmtprc ~/.esmtprc 600
fi
if type -p msmtp > /dev/null; then
do_patch dotmsmtprc ~/.msmtprc 600
fi
}
#
# Delete exsiting rc files
#
function delete_rcfiles {
rm -f ~/.forward ~/.fetchmailrc ~/.esmtprc ~/.msmtprc
}
#
# crontab for 'fetchmail'
#
function setup_cron {
crontab -l > ~/.mycron
if ! grep -q 'fetchmail' ~/.mycron; then
local fetchmail_bin=$(type -p fetchmail)
if [ -z $fetchmail_bin ]; then
echo "ERROR: fetchmail not found. possibly not installed?"
exit 1
fi
echo "* * * * * $fetchmail_bin >/dev/null 2>&1" >> ~/.mycron
crontab ~/.mycron
fi
rm ~/.mycron
}
#
# main
#
while getopts :d opt; do
case $opt in
d)
delete_rcfiles
;;
*)
echo "usage: `basename $0` [-d]"
exit 2
esac
done
shift `expr $OPTIND - 1`
OPTIND=1
load_keys
procmail_path
guess_sed
patch_rcfiles
# no need to cron, instead, use 'fetchmail -d 60'
#setup_cron