-
Notifications
You must be signed in to change notification settings - Fork 2
/
lunch.bash
executable file
·277 lines (194 loc) · 5.18 KB
/
lunch.bash
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
#!/usr/bin/env bash
#
# It's lunchtime!!
##---- Global Variables ----------------------------------------------------##
VERSION="0.2.0"
BINARIES=( sort )
DATA_FILE="${HOME}/.lunchdata"
# Number of unique places to prevent repeats. Defaults to 5.
DEFAULT_UNIQUE_PLACES=5
MAX_DATA_FILE_LINES=$DEFAULT_UNIQUE_PLACES
# Default lunch choices.
PLACES="Chipotle
Jimmy John's
Subway
Taco Bell
Wendy's
Whole Foods"
FILE=
COIN_FLIP=
LUNCHTIME=
##---- End Global Variables ------------------------------------------------##
##---- Private -------------------------------------------------------------##
#
# Check max data file lines.
#
_check_max_data_file_lines() {
local num=$(echo "${PLACES}" | wc -l)
if [ $num -lt $MAX_DATA_FILE_LINES ]; then
MAX_DATA_FILE_LINES="${num}"
fi
}
#
# Validate and read input file.
#
_validate_file() {
if [ ! -e $FILE ]; then
printf "Irregular file: %s\n" $FILE
exit 1
fi
PLACES=$(<$FILE)
_check_max_data_file_lines
}
#
# Ensure we have access to sort -R, --random-sort
#
_check_sort() {
local name=$(uname)
echo "foo" | sort -R > /dev/null 2>&1
if [ $? -gt 0 ]; then
case $name in
"Darwin")
echo "System: ${name} "
echo "brew install coreutils should fix the problem..."
;;
"Linux")
echo "System: ${name} "
echo "sudo <package manager> install coreutils should fix the problem..."
;;
*)
echo "System: ${name} "
echo "Not supported :'("
;;
esac
exit 1
fi
}
#
# Ensure binaries are available.
#
_check_binaries() {
for b in ${BINARIES[@]}; do
hash "$b" > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "Missing binary: ${b} please install to continue..."
exit 1
fi
done
_check_sort
}
#
# Heads or tails?
#
_flip_coin() {
COIN_FLIP=$(expr ${RANDOM} % 2)
}
#
# Ensure data file exists.
#
_check_data_file() {
local lines=0
if [ ! -e "${DATA_FILE}" ]; then
> ${DATA_FILE}
return
fi
lines=$(wc -l ${DATA_FILE} | cut -d " " -f 1)
if [ $lines -ge $MAX_DATA_FILE_LINES ]; then
> ${DATA_FILE}
fi
}
#
# Make sure we don't eat at the same place more than once a week!
#
_no_repeats() {
local repeat=0
while read line; do
if [ "${line}" == "${LUNCHTIME}" ]; then
repeat=1
break
fi
done < $DATA_FILE
if [ $repeat -gt 0 ]; then
lunchtime
else
echo $LUNCHTIME >> $DATA_FILE
fi
}
##---- End Private ---------------------------------------------------------##
##---- Public --------------------------------------------------------------##
usage() {
cat <<-USAGE
Lunchtime v$VERSION
usage: lunch.bash [OPTIONS]
Optional arguments:
-f lunchbox file Path to newline (\n) delimited lunchbox file.
-u number of unique places Number of unique places lunchtime returns
before introducing repeats.
Defaults to $DEFAULT_UNIQUE_PLACES.
-v version Display version and exit.
(-h) Display this message and exit.
License:
MIT
Author:
Ken Seal
USAGE
}
#
# Where to?
#
lunchtime() {
_flip_coin
_check_data_file
if [[ $COIN_FLIP -eq 0 ]]; then
LUNCHTIME=$(echo "${PLACES}" | sort -R | head -n 1)
else
LUNCHTIME=$(echo "${PLACES}" | sort -R | tail -n 1)
fi
_no_repeats
if [ -z "${LUNCHTIME}" ]; then
echo "An unexpected error occurred. Go to McDonalds..."
exit 1
fi
}
##---- End Public ----------------------------------------------------------##
##---- Begin ---------------------------------------------------------------##
while getopts ':f:u:vh' OPTION; do
case $OPTION in
f) FILE="${OPTARG}"
_validate_file
;;
u) MAX_DATA_FILE_LINES="${OPTARG}"
;;
v) echo "v${VERSION}"
exit 0
;;
h) usage
exit 0
;;
\:) printf "Argument missing: %s option\n" $OPTARG
usage
exit 1
;;
\?) printf "Unknown option: %s\n" $OPTARG
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
_check_binaries
lunchtime
cat <<-ART
######################################################################
###################. .. ......############/----------------###########
###############... .. .... .. ..######## / I want to go to \
#############..... .... ... .....#######/ $LUNCHTIME /
###########....,...,;,...... ... .#####/ /############################
##########.....(*)(*).\... ......#####//##############################
##########.... .......|⊐...... .######################################
###########......,___/........########################################
#############...............##########################################
######################################################################
ART
exit 0
##---- End -----------------------------------------------------------------##