-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5pm_again
executable file
·373 lines (352 loc) · 9.11 KB
/
5pm_again
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
#
# Help
showhelp () {
cat <<eof
This script is used to calculate when it's "5pm".
It calculates the time that the user can leave after working the defined workinghours.
This is done by using the time that the user first logged in.
Options:
--more | -m : Show some more output.
--quiet | -q : Don't show any output, for running on startup
--workhours | -w : Set the workhours, defaults to '08:30'
--debug | -d : Enable debug mode, aka echo some params
--start | -s : Don't use last to get the login time but the current time
--version | -v : Get the version
--snappy | -sn : Get a snappy comment back
--traffic : Show the current traffic situation
Configuration based on a daily fasion can be done in the /home/$whoami/.5pm/config file.
Here one can add days where there are specific options. This is done in the following format:
Wed=--workhours='07:50' -m
This line sets the script to use 07:50 as workhours on Wednesday and also print more output that day.
The matching is done based on 'date +%a'.
eof
exit
}
# Methods
debug () {
if $debug
then
echo "Debug: $@"
fi
}
error () {
echo "Error: $@"
exit 1
}
# Params
params () {
# Optional params
if [ -z $debug ]; then debug=false ; fi
if [ -z $workhours ]; then debug 'workhours not given'; workhours='08:30'; fi
if [ -z $quiet ]; then quiet=false; fi
if [ -z $more ]; then more=false; fi
if [ -z $starts ]; then starts=false; fi
if [ -z $version ]; then version=false; fi
if [ -z $snappy ]; then snappy=false; fi
if [ -z $traffic ]; then traffic=false; fi
if [ -z $onlytime ]; then onlytime=false; fi
if [ -z $onlyleft ]; then onlyleft=false; fi
if [ -z $onlysnappy ]; then onlysnappy=false; fi
if [ -z $routes ]; then routes=''; fi
# Needed for script
installdir=$( dirname "${BASH_SOURCE[0]}" )
trafficcliscript='/home/bjanssens/Inuits/Scripts/Trafficcli/get_traffic_cli'
dateYmd=$( date +%Y%m%d )
# Params needed for config
username=$( whoami )
# Directories and files
currentdir="/home/${username}/.5pm"
configfile="${currentdir}/config"
# Check if users .5pm directory exists
if [ ! -d $currentdir ]; then
debug Creating "$currentdir" directory
mkdir $currentdir
fi
# Tempfile
tempfile="${currentdir}/.${dateYmd}.tmp"
# Param checking
regexHHMM='^[0-2]{0,1}[0-9]{1}\:[0-9]{2}$'
if [ ! -z $workhours ]; then
if [[ ! $workhours =~ $regexHHMM ]]; then
error Workhours did not match the expected format, please use "hh:mm"
error Regex that is used to match agains: $regexHHMM
fi
fi
}
# Read the config from a config file
config () {
if [ -f "$configfile" ]; then
# %a locale's abbreviated weekday name (e.g., Sun)
currentD=$( date +%a )
for config in $( cat $configfile | sed 's/ /+/g' )
do
configwunder=$( echo $config | sed 's/+/ /g' )
#regexConfig='^(Mon|Tue|Wed|Thu|Fri)\=((\s{0,1}(--workhours|-w)\=\"[0-2]{0,1}[0-9]{1}\:[0-9]{2}\")|\s(--routes\s*)|\s(-m)){0,5}$'
#if [[ ! $configwunder =~ $regexConfig ]]
#then
# error Configuration syntax issue on line: \` $configwunder \`
#fi
day=$( echo $config | cut -d '=' -f 1 )
if [[ "${day}" == "$currentD" ]]
then
configOpts=$( echo $config | sed "s/$day=//g" | sed 's/+/ /g' )
parse_options $configOpts
fi
done
else
debug No config file found, using defaults
fi
}
# Format the Seconds to HH:MM
# Requiers one arugment: xxxxxxxS
parse_nice () {
S=$1
H=$(( $S / 3600 ))
M=$( echo "( $S - $H * 3600 ) / 60" | bc )
if (( $( echo "$H" | wc -m ) < 3 ))
then
H=$( echo "0${H}" )
fi
if (( $( echo "$M" | wc -m ) < 3 ))
then
M=$( echo "0${M}" )
fi
echo "${H}:${M}"
}
# Set times
cal_times () {
# Time stamps
dateYmd=$( date +%Y%m%d )
currentH=$( date +%H )
currentM=$( date +%M )
workhoursH=$( echo $workhours | cut -d ':' -f1 | sed "s/'//g" )
workhoursM=$( echo $workhours | cut -d ':' -f2 | sed "s/'//g" )
}
# Get the uptime
# Check if the machine was rebooted
get_logintime () {
if [ -f "$tempfile" ]
then
logintime=$( cat $tempfile )
debug Got uptime from tempfile: $tempfile
else
if $starts
then
logintime="$currentH:$currentM"
else
logintime=$( last | awk '{ print $7 }' | head -n 1 )
fi
# By putting it in the shell line the script gets called before the user is logged in.
# This gets the current date in stead of the login time then.
if [ $logintime == '' ]
then
logintime=$( date +%H:%M )
fi
echo "$logintime" > ${tempfile}
debug Wrote logintime to tempfile
# Clean up old tempfiles
find $currentdir -name *.tmp -ctime +2 -delete
fi
debug logintime: $logintime
logintimeH=$( echo $logintime | cut -d ':' -f1 )
logintimeM=$( echo $logintime | cut -d ':' -f2 | sed 's/^0//g' )
logintimeS=$(( $( echo $logintimeH | sed 's/^0//g' )*60*60 + $logintimeM*60 ))
}
# Calculate the time to leave
get_leave () {
leaveH=$( echo "$logintimeH + $workhoursH + ( $logintimeM + $workhoursM ) / 60" | bc )
leaveM=$( echo "( $logintimeM + $workhoursM ) % 60" | bc )
leaveS=$( echo "$leaveH * 3600 + $leaveM * 60" | bc )
leave=$( parse_nice $leaveS )
debug Leavingtime: $leave
}
# Calculate time remaining
get_remaining () {
currentS=$( echo "$currentH * 3600 + $currentM * 60" | bc )
remainingS=$( echo "$leaveS - $currentS" | bc )
remaining=$( parse_nice $remainingS)
debug Remaining: $remaining
}
# Calculate the time worked
get_worked () {
workedS=$(( $currentS - $logintimeS ))
workedhours=$( parse_nice $workedS )
debug Worked: $workedhours
}
# Get version by getting the amount of git commits
get_version () {
cd $installdir
version=$( git log | grep commit | wc -l )
echo "Version: 1.0.$version"
cd - 2>&- 1>&-
exit
}
# Print the output
nice_print () {
debug onlytime: $onlytime
debug onlyleft: $onlyleft
debug onlysnappy: $onlysnappy
if $onlytime
then
echo "$leave"
elif $onlysnappy
then
snappy $leaveS
elif $onlyleft
then
echo "$remaining"
else
echo "It's 5pm at $leave"
echo "Time remaining is $remaining"
if $more
then
echo "First logged in at $logintime"
echo "Workhours set to $workhours"
echo "Hours worked are $workedhours"
fi
if $snappy
then
snappy $leaveS
fi
if $traffic
then
get_traffictime
fi
fi
}
# Make some snappy remark based upon $remaining
snappy () {
# 8 hours remaining
if (( $remainingS > 25200 ))
then
echo "You just started!"
# 7 hours remaining
elif (( $remainingS <= 25200 )) && (( $remainingS > 21600 ))
then
echo "Seriously, you just started!"
# 6 hours remaining
elif (( $remainingS <= 21600 )) && (( $remainingS > 18000 ))
then
echo "Already tired?"
# 5 hours remaining
elif (( $remainingS <= 18000 )) && (( $remainingS > 14400 ))
then
echo "Trolrolrolrolr..."
# 4 hours remaining
elif (( $remainingS <= 14400 )) && (( $remainingS > 10800 ))
then
echo "Luckily your job isn't very mentally challanging."
# 3 hours remaining
elif (( $remainingS <= 10800 )) && (( $remainingS > 7200 ))
then
echo "Don't you need to be working?"
# 2 hours remaining
elif (( $remainingS <= 7200 )) && (( $remainingS > 3600 ))
then
echo "Keep working, working, working..."
# 1 hour remaining
elif (( $remainingS <= 3600 )) && (( $remainingS > 1200 ))
then
echo "Hang in there, you can leave in about 1 hour."
# 20 min remaining
elif (( $remainingS <= 1200 )) && (( $remainingS > 600 ))
then
echo "You might want to check the traffic report..."
elif (( $remainingS <= 600 )) && (( $remainingS > 300 ))
then
echo "No one would notice if you left now."
elif (( $remainingS <= 300 )) && (( $remainingS > 0 ))
then
echo "You still working?"
elif (( $remainingS < 0 ))
then
echo "Do you need to compensate for something?"
fi
}
get_traffictime () {
if [ ! -f $trafficcliscript ]
then
error get_traffic_cli script not found
exit 1
else
$trafficcliscript -r "$routes"
fi
}
parse_options () {
while test -n "$1"
do
case "$1" in
--help|-h)
showhelp
;;
--start|-s)
starts=true
shift
;;
--debug|-d)
debug=true
shift
;;
--more|-m)
more=true
shift
;;
--quiet|-q)
quiet=true
shift
;;
--workhours|-w)
shift
workhours=$1
shift
;;
--route|-r)
shift
routes="$routes $1"
shift
;;
--snappy|-sn)
snappy=true
shift
;;
--version|-v)
version=true
shift
;;
--traffic)
traffic=true
shift
;;
--onlytime)
onlytime=true
shift
;;
--onlysnappy)
onlysnappy=true
shift
;;
--onlyleft)
onlyleft=true
shift
;;
*)
showhelp
;;
esac
done
}
params
config
parse_options $@
cal_times
if $version; then
get_version
fi
get_logintime
get_leave
get_remaining
get_worked
if ! $quiet; then
nice_print
fi