forked from foss-np/batti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
executable file
·226 lines (193 loc) · 5.9 KB
/
main.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
set -e
WD="$(dirname $(readlink $0 || echo $0))"
SCHEDULE="$HOME/.cache/batti.sch"
TEMP='/tmp'
function download {
wget -c http://nea.org.np/loadshedding.html -O $TEMP/nea.html
link=($(sed -n '/supportive_docs/p' $TEMP/nea.html |\
tr '<' '\n' | sed -n 's/.*\(http.*pdf\)">.*/\1/gp'))
wget -c ${link[0]} -O $TEMP/nea.pdf
}
function extract {
[ -e $SCHEDULE ] && hash_old=$(md5sum $SCHEDULE)
rm -f $SCHEDULE
pdftotext -f 1 -layout $TEMP/nea.pdf $TEMP/raw.txt
sed -n '/;d"x÷af/,/;d"x–@/p' $TEMP/raw.txt > $TEMP/part.txt
sed -i 's/\;d"x–.//; /M/!d; s/^ \+//' $TEMP/part.txt
# NOTE: 2utf8 is not-really required but for fail and debug situations
# 2utf8 -i $TEMP/part.txt > $TEMP/uni.txt
# sed -i 's/०/0/g; s/१/1/g; s/२/2/g; s/३/3/g;
# s/४/4/g; s/५/5/g; s/६/6/g; s/७/7/g;
# s/८/8/g; s/९/9/g; s/–/-/g;' $TEMP/uni.txt
sed -i 's/)/0/g; s/!/1/g; s/@/2/g; s/#/3/g;
s/%/5/g; s/\^/6/g; s/\&/7/g;
s/\*/8/g; s/(/9/g; s/–/-/g; s/M/:/g' $TEMP/part.txt
# FIX: its hard to replace $
cat $TEMP/part.txt | tr '$' '4' > $TEMP/uni.txt
sed 's/ \+/\t/g' $TEMP/uni.txt | head -2 > $SCHEDULE
hash_new=$(md5sum $SCHEDULE)
>&2 echo "> Schedule Extracted"
if [[ "$hash_new" == "$hash_old" ]]; then
>&2 echo "> Schedule Unchanged"
else
>&2 echo "> New Schedule"
fi
}
function get_color { # arg($1:color_code)
# NOTE: cdef is always same
[[ "$SGR" = "" ]] && echo "\033[$1;$2m"
}
function rotate_field { # arg($1:day, $2:group)
f=$(($1-$2))
[ $f -le 0 ] && f=$((7+$f))
echo $f
}
function range_check { # arg($1:check_value)
if [[ $1 -gt 0 ]] && [[ $1 -le 7 ]]; then
echo -n # do nothing
else
Usage
exit 1;
fi
}
function week_view { # arg($1:group)
day=(Sun Mon Tue Wed Thr Fri Sat)
for((i=0;i<7;i++)) {
field=$(rotate_field $i $1)
f0=$((field-1))
f1=$((f0+7))
if [ $today == $i ]; then
color=$(get_color 1 32)
cdef=$(get_color 0 0)
else
color=""
cdef=""
fi
echo -e ${color}${day[$i]} # $field
echo -e "\t${data[f0]}"
echo -e "\t${data[f1]}$cdef"
}
}
function xml_dump {
data=($(cat $SCHEDULE))
day=(sunday monday tuesday wednesday thursday friday saturday)
echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<routine>"
for((g=1;g<=7;g++)) {
echo -e " <group name=\"$g\">"
grp=$(($g-2))
for((i=0;i<7;i++)) {
field=$(rotate_field $i $grp)
f0=$((field-1))
f1=$((f0+7))
echo " <day name=\"${day[$i]}\">"
echo " <item>${data[f0]}</item>"
echo " <item>${data[f1]}</item>"
echo " </day>"
}
echo -e " </group>"
}
echo "</routine>"
}
function all_sch { # arg($1:group)
# is loaded by default so data must be loaded
data=($(sed 's/://g' $SCHEDULE))
h1=$(get_color 1 32)
c1=$(get_color 1 34)
cdef=$(get_color 0 0)
echo -en " $h1"
for day in Sun Mon Tue Wed Thr Fri Sat ; do
printf " %-7s" $day
done
echo -en "$cdef\n"
today=(`date +%w`)
for((g=1;g<=7;g++)) {
echo -en "$h1 Group $g: $cdef"
grp=$(($g-2))
line2=""
if [ "$1" == $grp ]; then c2=$(get_color 0 34);
else c2=""; fi
for((i=0;i<7;i++)) {
field=$(rotate_field $i $grp)
f0=$((field-1))
f1=$((f0+7))
if [ $today == $i ]; then
echo -en "$c1${data[$f0]}$cdef "
line2+=$(echo -en "$c1${data[f1]}$cdef ")
else
echo -en "$c2${data[f0]} "
line2+=$(echo -en "$c2${data[f1]} ")
fi
}
echo -e "$cdef\n $line2"
}
}
function today_view { # arg($1:group)
field=$(rotate_field $today $1)
f0=$((field-1))
f1=$((f0+7))
echo ${data[f0]}, ${data[f1]}
}
function update {
local FILE="$TEMP/nea.pdf"
[ -e $FILE ] || download
[ -e $FILE ] && extract
}
[ -e $SCHEDULE ] || update
#checking arguments
if [ $# -eq 0 ]; then
all_sch
exit 0;
fi
function Credits {
h1=$(get_color 1 32)
cdef=$(get_color 0 0)
sed 's/^#.*/'`echo -e $h1`'&'`echo -e $cdef`'/' $WD/AUTHORS
echo
}
function Usage {
echo -e "Usage: batti [OPTIONS] [GROUP_NO]";
echo -e "\t-a | --all\tShow All [force]"
echo -e "\t-g | --group\tGroup number 1-7"
echo -e "\t-t | --today\tShow today's schedule [uses with group no]"
echo -e "\t-w | --week\tShow week's schedule"
echo -e "\t-u | --update\tCheck for update [ignores extra options]"
echo -e "\t-d | --dump\tSchedule raw dump"
echo -e "\t-x | --xml\tDump to xml"
echo -e "\t-c | --credits\tDisplay the Credits"
echo -e "\t-h | --help\tDisplay this message"
echo -e "\t-v | --version\tversion information"
}
GETOPT=$(getopt -o g:awtudxchv\
-l all,group:,week,today,update,dump,xml,credits,help,version\
-n "batti"\
-- "$@")
eval set -- "$GETOPT"
dis=0
while true; do
case $1 in
-a|--all) dis=1; shift;;
-g|--group) grp=$2; shift 2;;
-w|--week) dis=0; shift;;
-t|--today) dis=3; shift;;
-u|--update) update; exit;;
-d|--dump) cat $SCHEDULE; exit;;
-x|--xml) xml_dump; exit;;
-c|--credits) Credits; exit;;
-h|--help) Usage; exit;;
-v|--version) cat $WD/.version; exit;;
--) shift; break;;
esac
done
# extra argument
for arg do
grp=$arg
break
done
range_check "$grp"
data=($(cat $SCHEDULE))
grp=$(($grp-2)) # for rotation
today=(`date +%w`)
if (( "$dis" == 0 )); then week_view $grp
elif (( "$dis" == 1 )); then all_sch $grp
else today_view $grp; fi