-
Notifications
You must be signed in to change notification settings - Fork 2
/
wdownload
executable file
·168 lines (122 loc) · 4.1 KB
/
wdownload
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/bash
[ -n $UDIR ] && UDIR=`pwd`
[ -n WPATH ] && WPATH=$UDIR
#echo "----DOWN UDIR=$UDIR"
#echo "----DOWN WPATH=$WPATH"
WDIR=.w
mkdir -p $WDIR/Cache $WDIR/Session
# --- sets COLS
[ "$1" == "-d" ] && DOWNLOAD=DOWNLOAD && shift
[ "$1" == "-l" ] && DOLOG=DOLOG && shift
GO=$1
ROWS=${2:-}
COLS=${3:-}
SAFE=`$WPATH/wquote "$GO"`
CFILE=$WDIR/Cache/$SAFE
#USERAGENT="curl/7.78.0"
USERAGENT="./w (minimalist text-only retro browser; Linux/Android/Termux) github.com/yesco/RetroFit"
# --- DOWNLOAD forces a new download
# [ $DOWNLOAD ] && rm -f $CFILE.TMP
# --- Log it (if -d)
# (logging it adds to history so we can browse it!)
[ $DOWNLOAD ] && ( \
echo "`date --iso=s` #=W $GO $CFILE.ANSI" \
| tee -a .wlog \
| tee -a $CFILE.WLOG ) \
> /dev/null
[[ $DOLOG ]] && echo "`date --iso=s` #=W $GO $CFILE.ANSI" >> .whistory
# --- local file (always copy)
[ -f $GO ] && cp $GO $CFILE.DOWN && ( touch $CFILE.TMP )
# --- log
echo "`date --iso=s` #=DOWN $GO $CFILE.ANSI" \
| tee -a .wlog \
>> $CFILE.WLOG
# --- WGET
# Exit:
# 1 Generic error code.
# 2 Parse error (options).
# 3 File I/O error.
# 4 Network failure.
# 5 SSL verification failure.
# 6 Username/password auth fail.
# 7 Protocol errors.
# 8 Server gave error.
# TODO: always update?
# --- download if not local file
if [[ $DOWNLOAD || ! -f "$CFILE.DOWN" ]]; then
# - display spinning globe!
$WPATH/spin.x & foo_pid=$!
$WPATH/wmessage "./wdownload $GO" $ROWS $COLS
# save original URL
echo "$GO" > $CFILE.URL
# - download
# (.TMP indicates download started)
# TODO: this make it not print ERROR?
# -a $CFILE.LOG
# TODO: backups?
# -N --backups=3
# has no effect as we rename...
(wget -N --backups=3 -U "$USERAGENT" -S -O $CFILE.TMP "$GO" || echo 0 ) 2>&1 | grep ERROR > $CFILE.ERR \
&& (err=$? ; echo "WGET.ERROR $?" >> $CFILE.ERR ; exit $err) \
&& rm $CFILE.TMP || rm $CFILE.ERR
# - kill the globe
( kill $foo_pid && wait $foo_pid ) 2>/dev/null 1>&2
# -- we have .ERR if fail or .TMP if ok!
[ -f $CFILE.ERR ] && $WPATH/werror "./wdownload error: $err $GO" $ROWS $COLS && exit
# ; rm $CFILE.TMP ; ( ( kill $foo_pid >/dev/null && wait $foo_pid ) 2>/dev/null 1>&2 ) ; echo "$err" >.wnetdown ; exit $err) || exit $?
rm -f .wnetdown
$WPATH/wmessage "./wdownload done: $GO" $ROWS $COLS
# - cat so get correct date on file
# (wget gives file as of HEAD date!)
# cat $CFILE.TMP > $CFILE.DOWN
# - rotate backups manually
# use actual HEAD date
rm -f $CFILE.DOWN.3
mv -f $CFILE.DOWN.2 $CFILE.DOWN.3 2>/dev/null
mv -f $CFILE.DOWN.1 $CFILE.DOWN.2 2>/dev/null
mv -f $CFILE.DOWN $CFILE.DOWN.1 2>/dev/null
mv -f $CFILE.TMP $CFILE.DOWN
rm -f $CFILE.ANSI.3
mv -f $CFILE.ANSI.2 $CFILE.ANSI.3 2>/dev/null
mv -f $CFILE.ANSI.1 $CFILE.ANSI.2 2>/dev/null
mv -f $CFILE.ANSI $CFILE.ANSI.1 2>/dev/null
# (below generates the ansi file)
# ls -l $CFILE.*
fi
# TODO: handle render errors?
# (maybe add error message in file?)
$WPATH/w.x $CFILE.DOWN $GO $COLS > $CFILE.ANSI
# generate links list etc
perl $WPATH/wlinks.pl $CFILE.ANSI > $CFILE.LINKS
perl -0777 -ne 'if (s:^.*?<(title|h1).*?>(.*?)</\1>.*$:$2:ism) { trim; s|\\s+| |smg; print $_; }' $CFILE.DOWN # > $CFILE.TITLE
rm -f $CFILE.TMP
# create unified diff and deltas
# (redo formatting to be compat)
[[ -f $CFILE.DOWN.1 ]] && {
$WPATH/w.x $CFILE.DOWN.1 $GO $COLS > $CFILE.ANSI.1
diff -I '^@[[:digit:]]' -b -U 999999 $CFILE.ANSI.1 $CFILE.ANSI > $CFILE.DIFF.TMP
diff -I '^@[[:digit:]]' -b $CFILE.ANSI.1 $CFILE.ANSI > $CFILE.DELTA.TMP
# no diff
if [[ -s $CFILE.DELTA.TMP ]]; then
mv --backup=numbered $CFILE.DIFF.TMP $CFILE.DIFF
mv --backup=numbered $CFILE.DELTA.TMP $CFILE.DELTA
else
rm -f $CFILE.DELTA $CFILE.DIFF $CFILE.DELTA.TMP $CFILE.DIFF.TMP
exit 0
fi
}
# - Signal app that requested
# TODO:
# kill:
# - https://en.m.wikipedia.org/wiki/Signal_(IPC)#Handling_signals
# - https://en.m.wikipedia.org/wiki/C_signal_handling
# 0 SIGNULL
# -1 SIGHUP
# -2 SIGINT
# -5 SIGPOLL
# -16 SIGUSR1
# -17 SIGUSR2
# -20 SIGCHLD
# -23 SIGIO
# -28 SIGWINCH
# -38 SIGDCE