-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.sh
executable file
·125 lines (105 loc) · 3.23 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
#!/bin/bash
WD="$(dirname $(readlink $0 || echo $0))"
function Credits {
h1="\033[1;32m"
cdef="\033[0;0m"
sed 's/^#.*/'`echo -e $h1`'&'`echo -e $cdef`'/' $WD/AUTHORS
echo
}
function random {
local RANDOMIZE_UPTO=$1
let "number = $RANDOM % $RANDOMIZE_UPTO"
echo $number
}
function pkg_handle {
# put dot as separator
pkg=(${1/./ })
MESSAGES_PATH="$WD/pkg/${pkg[0]}"
test -d "$MESSAGES_PATH" || {
>&2 echo 'Package Error'
return
}
if [[ -e "$MESSAGES_PATH/${pkg[1]}.png" ]]; then
echo " -d $MESSAGES_PATH/${pkg[1]}.png "
return
elif [[ ${pkg[1]} != "" ]]; then
echo "${pkg[1]} Not found"
return
fi
>&2 echo 'Randomizing...'
MSG_LIST=($MESSAGES_PATH/*.png)
rand=$(random `echo ${#MSG_LIST[@]}`)
>&2 echo "${MSG_LIST[$rand]}"
echo " -d ${MSG_LIST[$rand]} "
}
function inhibit {
[ -e /tmp/present ] && exit
>&3 which xfconf-query && {
state=$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode)
}
>&3 echo STATE = $state
[ $state == 'true' ] && exit
}
function Usage {
echo "Takes all the arguments of xcowsay"
echo -e "Usage: xcowsay-utils [OPTIONS]";
echo -e "\t-a | --again Show again recently say"
echo -e "\t-l | --pkglst List package"
echo -e "\t-p | --pkg [pkg.file] use package and specify file"
echo -e "\t-m | --mem needed before using again"
echo -e "\t-i | --inhibit Disable when buzy (/tmp/present or xfce4)"
echo -e "\t-c | --credits Show credits"
echo -e "\t-v | --verbose Increase verbosity"
echo -e "\t-h | --help Display this message"
}
# TODO: don't use getopt it blocks xcowsay command
GETOPT=$(getopt -o aip:lrmcvh\
-l again,inhibit,pkg:,pkglstrandom,memory,credits,vebose,help\
-n "xcowsay-utils"\
-- "$@")
# TODO: fix this error check to getopt
if [ $? != "0" ]; then exit 1; fi
eval set -- "$GETOPT"
mem=0
VV=2
exec 3> /dev/null
exec 4> /dev/null
while true; do
case $1 in
-a|--again) $(2>/dev/null cat /tmp/xcowsay-utils) || Usage; exit;;
-p|--pkg) shift; img_file="$(pkg_handle $1)"; shift;;
-l|--pkglst) ls -1 "$WD/pkg"; exit;;
-m|--memory) mem=1; shift;;
-i|--inhibit) inhibit; shift;;
-c|--credits) Credits; exit;;
-v|--verbose) let VV++; eval "exec $VV>&2"; shift;;
-h|--help) Usage; exit;;
--) shift; break;;
esac
done
>&3 echo $WD
# extra argument
for arg do
extra_args+=" $arg"
done
# listing stuffs
IMG_PATH=$WD/faces
IMG_LIST=($IMG_PATH/*.png)
r=$(random `echo ${#IMG_LIST[@]}`)
# TODO: check the extra argument as the loop
# for ((i=0; i <= ${#args}; i++)); do
# local a=${args[i]}
# if [[ ${a:0:1} == '-' && $a != '-c' ]]; then
# /usr/bin/emacs ${args[*]}
# return
# fi
# done
if [[ "$extra_args" == "" && "$img_file" == "" ]]; then
msg=$(fortune)
fi
if [[ "$mem" == 0 ]]; then
xcowsay --image=${IMG_LIST[$r]} $extra_args $img_file "$msg"
exit 0
fi
echo xcowsay --image=${IMG_LIST[$r]} $extra_args $img_file \"$msg\" > /tmp/xcowsay-utils
$(cat /tmp/xcowsay-utils)