-
Notifications
You must be signed in to change notification settings - Fork 20
/
taoup-fortune
executable file
·55 lines (51 loc) · 2.2 KB
/
taoup-fortune
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
#!/bin/bash
# Note: The `while read line...done` stuff is an ugly hack for portable random `sort`, mostly for OSX users.
# If you don't yet have `cowsay`, stop reading right now and get it installed!
# Alternatively, try:
# - https://github.com/matheuss/parrotsay
# - https://github.com/busyloop/lolcat
# - https://github.com/sckott/cowsay (enhanced version)
dir=`dirname "${BASH_SOURCE[0]}"`
# USER ALERT!!! @ronjouch complained about execution speed at https://github.com/globalcitizen/taoup/issues/11
# ... therefore we add caching ... therefore first ensure we have an up to date cache via one of ...
# md5sum
if [ `which md5sum 2>/dev/null` ]; then
MD5SUM=`md5sum ${dir}/taoup | cut -d ' ' -f1`
# md5
elif [ `which md5 2>/dev/null` ]; then
MD5SUM=`md5 -q ${dir}/taoup | cut -d ' ' -f1`
# openssl
elif [ `which openssl 2>/dev/null` ]; then
MD5SUM=`cat ${dir}/taoup | openssl md5 | grep -o '[[:xdigit:]][[:xdigit:]]*$' |cut -d '=' -f2- |cut -c 2-`
# ruby
elif [ `which ruby 2>/dev/null` ]; then
MD5SUM=`ruby -rdigest/md5 -e"puts Digest::MD5.file'${dir}/taoup'"`
fi
# determine cachefile name
cachefile=${dir}/.taoup-fortune.cache.${MD5SUM}
# create if necessary
if [ ! -r $cachefile ]; then
${dir}/taoup $@ >${cachefile}
fi
# handle all classes of society
if [ `which cowsay 2>/dev/null` ]; then
# educated user execution path
if [ `which shuf 2>/dev/null` ]; then
cat ${cachefile} |grep -v '^---' | shuf -n 1 | cowsay -f eyes -n |head -n 2 |tail -n 1
elif [ `echo test|sort -R 2>/dev/null` ]; then
cat ${cachefile} |grep -v '^---' | sort -R | head -n 1 | cowsay -f eyes -n |head -n 2 |tail -n 1
else
cat ${cachefile} |grep -v '^---' | while read line;do echo $RANDOM'|'$line;done |sort|cut -d'|' -f2- |head -n 2 |tail -n 1
fi
echo '' | cowsay -f eyes |tail -n 10 |sed 's/^/[0;32;40m/'
else
# impoverished user execution path
if [ `which shuf 2>/dev/null` ]; then
cat ${cachefile} |grep -v '^---' | shuf -n 1
elif [ `echo test |sort -R 2>/dev/null` ]; then
cat ${cachefile} |grep -v '^---' | sort -R | head -n 1
else
cat ${cachefile} |grep -v '^---' | while read line;do echo $RANDOM'|'$line;done |sort|cut -d'|' -f2- |head -n 2 |tail -n 1
fi
fi
echo "(B[m"