updated on Sat Jan 14 00:11:12 UTC 2012
[aur-mirror.git] / moblock / moblock-update
blobaae861d139dcd420c789bd3517c20545b852c145
1 #!/bin/bash
3 . /etc/moblock/config
5 CONF_DIR=/etc/moblock
6 TEMP_DIR=$(/usr/bin/mktemp -t -d moblock-updateXXXXXXXX)
7 LIST_FILE=banned.list
9 USECOLOR="no"
10 . /etc/rc.d/functions
11 PREFIX_REG=" >"
12 PREFIX_HL="::"
14 function extract()
16 /usr/bin/find $TEMP_DIR -type f -name '*.gz' -o -name '*.zip' |\
17 while read N
19 case "$N" in
20 *.zip) /usr/bin/unzip -oqq "$N" 2>/dev/null
21 if [ $? -gt 0 ]; then
22 rm -f "$N"
23 return 1
24 else
25 rm -f "$N"
28 *.gz) /bin/gunzip -f "$N" 2>/dev/null
29 if [ $? -gt 0 ]; then
30 rm -f "$N"
31 return 1
34 *) continue
36 esac
37 done
38 return 0
41 cd $TEMP_DIR
43 printf "${C_SEPARATOR} ------------------------------\n"
44 printhl "Downloading and extracting files:\n"
46 # Bluetack lists (with fallback)
47 for i in ${BLUETACK[@]}
49 if [ $(echo $i | /bin/grep '^[^\!]' | /usr/bin/wc -l) -eq 1 ]; then
50 stat_busy "BLUETACK '${i}'... "
51 /usr/bin/wget ${WGET_OPTS} "http://www.bluetack.co.uk/config/${i}.gz" && extract
52 if [ $? -gt 0 ] || [ ! -f ${i} ]; then
53 stat_fail
54 bfile=$i
55 if [ "$bfile" = "ads-trackers-and-bad-pr0n" ]; then
56 bfile="ads"
57 elif [ "$bfile" = "Microsoft" ];then
58 bfile="microsoft"
60 stat_busy "[!!] BLUETACK '${i}' (fallback link)... "
61 /usr/bin/wget ${WGET_OPTS} "http://list.iblocklist.com/?list=bt_${bfile%%-*}" -O "${i}.gz" && extract
62 if [ $? -gt 0 ]; then
63 stat_fail
64 else
65 stat_done
67 else
68 stat_done
71 done
73 # Blocklist lists
74 for i in ${BLOCKLIST[@]}
76 if [ $(echo $i | /bin/grep '^[^\!]' | /usr/bin/wc -l) -eq 1 ]; then
77 stat_busy "BLOCKLIST '${i}'... "
78 /usr/bin/wget ${WGET_OPTS} "blocklist.org/${i}.p2b.gz" && extract
79 if [ $? -gt 0 ]; then
80 stat_fail
81 else
82 stat_done
85 done
87 # Old phoenixlabs.org lists
88 for i in ${PHOENIXLABS[@]}
90 if [ $(echo $i | /bin/grep '^[^\!]' | /usr/bin/wc -l) -eq 1 ]; then
91 stat_busy "PHOENIXLABS '${i}'... "
92 /usr/bin/wget ${WGET_OPTS} "fox.phoenixlabs.org/${i}" && extract
93 if [ $? -gt 0 ]; then
94 stat_fail
95 else
96 stat_done
99 done
101 if [ $(/bin/cat "$TEMP_DIR"/* | /usr/bin/wc -l) -eq 0 ]; then
102 printf "\n"
103 printhl "ERROR: No files were downloaded"
104 printf "${C_SEPARATOR} ------------------------------\n"
105 exit 1
108 # Check files
109 printsep
110 printhl "Checking integrity of downloaded files:\n"
112 /usr/bin/find -type f | while read N
114 stat_busy "File '$(echo $N | /bin/awk -F/ '{print $NF}')'... "
115 scan1=$(/bin/cat "$N" | /usr/bin/wc -l)
116 scan2=$(/bin/egrep -o ":[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*-[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" "$N" | /usr/bin/wc -l)
117 if [ $scan1 -eq $scan2 ]; then
118 stat_done
119 else
120 if [ $scan2 -gt 0 ]; then
121 if [ $scan1 -gt $scan2 ]; then
122 stat_append "$(($scan1-$scan2)) of $scan1 entries failed validation; keeping the file"
123 stat_done
125 else
126 stat_fail
127 stat_busy "[!!] Removing corrupted file... "
128 rm "$N" 2>/dev/null
129 if [ $? -gt 0 ]; then
130 stat_fail
131 exit 1
132 else
133 stat_done
137 done
139 printsep
140 printhl "Saving the list:\n"
142 # Make backup
143 if [ "$BACKUP_OLD_LIST" = "yes" ] && [ -f "$CONF_DIR"/"$LIST_FILE" ]; then
144 stat_busy "Backing up old list to '$CONF_DIR/$LIST_FILE.gz'... "
145 /bin/gzip -f "$CONF_DIR"/"$LIST_FILE" 2>/dev/null
146 if [ $? -gt 0 ]; then
147 stat_fail
148 else
149 stat_done
153 # Save the list
154 stat_busy "Saving new list to '$CONF_DIR/$LIST_FILE'... "
155 /bin/cat "$TEMP_DIR"/* > "$CONF_DIR"/"$LIST_FILE" 2>&1
156 if [ $? -gt 0 ]; then
157 stat_fail
158 exit 1
159 else
160 stat_done
161 printf "\n"
162 printhl "Saved `cat "$CONF_DIR"/"$LIST_FILE" | wc -l` ranges"
163 printf "${C_SEPARATOR} ------------------------------\n"
166 rm -rf "$TEMP_DIR"
168 # Restart MoBlock
169 if [ -f /var/run/moblock.pid ]; then
170 /bin/kill -HUP `cat /var/run/moblock.pid` >/dev/null 2>&1
173 exit 0