-
Notifications
You must be signed in to change notification settings - Fork 133
/
start.sh
executable file
·413 lines (362 loc) · 10.8 KB
/
start.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env bash
# warning about "echo $(cmd)", used many times with echo "$(gettext ...)"
# shellcheck disable=SC2005
#
# warning about eval_gettext '$GSH_ROOT/...' about variable not expanding in single quotes
# shellcheck disable=SC2016
#
# warning about using printf "$(gettext ...)" because the string may contain
# escape characters (they don't)
# shellcheck disable=SC2059
#
# warning about declaring and initializing at the same time: local x=...
# shellcheck disable=SC2155
# shellcheck source=/dev/null
. gettext.sh
export GSH_ROOT="$(dirname "$0")"
# shellcheck source=lib/profile.sh
. "$GSH_ROOT/lib/profile.sh"
# shellcheck source=lib/mission_source.sh
. "$GSH_ROOT/lib/mission_source.sh"
display_help() {
cat "$(eval_gettext "\$GSH_ROOT/i18n/start-help/en.txt")"
}
export GSH_COLOR="OK"
GSH_MODE="ANONYMOUS"
RESET=""
while getopts ":hcnPdDACRXqL:K" opt
do
case $opt in
h)
display_help
exit 0
;;
n)
GSH_COLOR=""
;;
c)
GSH_COLOR="OK"
;;
P)
GSH_MODE="PASSPORT"
;;
d)
GSH_MODE="DEBUG"
;;
D)
GSH_MODE="DEBUG"
export GSH_VERBOSE_DEBUG="true"
;;
q)
export GSH_QUIET_INTRO="true"
;;
A)
GSH_MODE="ANONYMOUS"
;;
C)
RESET="FALSE"
;;
R)
RESET="TRUE"
;;
L)
if locale | grep "LANGUAGE" >/dev/null
then
export LANGUAGE=$OPTARG
else
if locale -a | grep -x "$OPTARG" >/dev/null
then
export LC_MESSAGES=$OPTARG
else
echo "Unknown locale: '$OPTARG'." >&2
echo "You can run 'locale -a' to get a list of all locales installed on your system." >&2
locale -a | grep "$OPTARG" && echo "The above locales might be relevant..." >&2
exit 1
fi
fi
;;
X)
echo "$(gettext "Error: this option is only available from an executable archive!")" >&2
exit 1
;;
K)
: # used by the self-extracting archive
;;
*)
echo "$(eval_gettext "Error: invalid option: '-\$OPTARG'")" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
_passport() {
local PASSPORT=$1
NOM=""
while [ -z "$NOM" ]
do
printf "$(gettext "Player's name:") "
read -r NOM
done
EMAIL=""
while [ -z "$EMAIL" ]
do
printf "$(gettext "Player's email:") "
read -r EMAIL
done
echo " $NOM <$EMAIL>" >> "$PASSPORT"
}
_confirm_passport() {
local PASSPORT=$1
echo "======================================================="
cat "$PASSPORT"
echo "======================================================="
while true
do
printf "$(gettext "Is this information correct? [Y/n]") "
read -r OK
echo
if [ -z "$OK" ] || [ "$OK" = "$(gettext "y")" ] || [ "$OK" = "$(gettext "Y")" ]
then
return 0
elif [ "$OK" = "$(gettext "n")" ] || [ "$OK" = "$(gettext "N")" ]
then
return 1
fi
done
}
progress() {
if [ -z "$progress_I" ]
then
progress_filename=$GSH_ROOT/lib/ascii-art/titlescreen
local N=$(wc -l "$GSH_CONFIG/index.txt" | awk '{print $1}')
local size=$(wc -c "$progress_filename" | awk '{print $1}')
progress_delta=$((size/N + 1))
# head -c$((progress_delta - 1)) $progress_filename => not POSIX compliant
dd if="$progress_filename" bs="$progress_delta" count=1 2>/dev/null
progress_I=1
else
# tail -c+$((progress_I * progress_delta)) $progress_filename | head -c$progress_delta => not POSIX compliant
dd if="$progress_filename" bs="$progress_delta" skip="$progress_I" count=1 2>/dev/null
progress_I=$((progress_I+1))
fi
}
progress_finish() {
# tail -c+$((progress_I*progress_delta)) $progress_filename => not POSIX compliant
dd if="$progress_filename" bs="$progress_delta" skip="$progress_I" 2>/dev/null
unset progress_filename progress_delta progress_I
# show cursor and enable echo of keystrokes
}
init_gsh() {
ADMIN_HASH='b88968dc60b003b9c188cc503a457101b4087109' # default for 'gsh'
# message when data from a previous play is found. We can either
# - restart a new game
# - continue the previous game
if [ -e "$GSH_CONFIG" ]
then
if [ -z "$RESET" ]
then
local r
printf "$(eval_gettext 'The directory $GSH_CONFIG contains meta-data from a previous game.
Do you want to remove it and start a new game? [y/N]') "
read -r r
[ "$r" = "$(gettext "y")" ] || [ "$r" = "$(gettext "Y")" ] || return 1
elif [ "$RESET" = "FALSE" ]
then
return 1
fi
fi
### if we're here, we need to reset a new game
# hide cursor and disable echoing of keystrokes
tput civis 2>/dev/null
stty -echo 2>/dev/null
trap "tput cnorm 2>/dev/null; stty echo 2>/dev/null; echo; exit 1" INT TERM QUIT EXIT
# remove all the game data
rm -rf "$GSH_HOME"
rm -rf "$GSH_CONFIG"
rm -rf "$GSH_VAR"
rm -rf "$GSH_BASHRC"
rm -rf "$GSH_BIN"
rm -rf "$GSH_SBIN"
# recreate them
mkdir -p "$GSH_HOME"
mkdir -p "$GSH_CONFIG"
echo "# mission action date checksum" >> "$GSH_CONFIG/missions.log"
awk -v seed_file="$GSH_CONFIG/PRNG_seed" 'BEGIN { srand(); printf("%s", int(2^32 * rand())) > seed_file; }'
mkdir -p "$GSH_BASHRC"
cp "$GSH_LIB/bashrc" "$GSH_BASHRC"
# save current locale
locale | sed -e "s/^/export /" > "$GSH_BASHRC"/config.sh
echo "export GSH_MODE=$GSH_MODE" >> "$GSH_BASHRC"/config.sh
# TODO save other config (color ?)
# save hash for admin password
[ -n "$ADMIN_HASH" ] && echo "$ADMIN_HASH" > "$GSH_CONFIG/admin_hash"
mkdir -p "$GSH_BIN"
mkdir -p "$GSH_SBIN"
mkdir -p "$GSH_VAR"
# id of player
PASSPORT="$GSH_CONFIG/passport.txt"
while true
do
# Reading the player name (if in passport mode).
case "$GSH_MODE" in
DEBUG)
echo "DEBUG MODE" >> "$PASSPORT"
break
;;
ANONYMOUS)
echo "ANONYMOUS MODE" >> "$PASSPORT"
break
;;
PASSPORT)
_passport "$PASSPORT"
;;
esac
# Check that the information is correct.
_confirm_passport "$PASSPORT" && break
done
printf '\n==========\nRANDOM=%d\n' $RANDOM >> "$PASSPORT"
# Generation of a unique identifier for the the player.
export GSH_UID="$(checksum < "$PASSPORT" | cut -c 1-40)"
echo "GSH_UID=$GSH_UID" >> "$PASSPORT"
echo "$GSH_UID" > "$GSH_CONFIG/uid"
# save system config, in case of problems
_gsh_systemconfig > "$GSH_CONFIG/system"
# Clear the screen.
if [ "$GSH_MODE" = "DEBUG" ]
then
printf "[MISSION INITIALISATION]" >&2
else
clear
fi
make_index "$@" | sed -e "s;$GSH_MISSIONS;.;" > "$GSH_CONFIG/index.txt"
if [ "$GSH_MODE" != "DEBUG" ]
then
echo
cat "$GSH_LIB/ascii-art/GameShell.txt"
echo
fi
# Installing all missions.
local MISSION_NB=1 # current mission number
local MISSION_SUB_NB="" # when a dummy mission is found, as a "sub-number" as well
local FULL_NB # the 2 together
while read -r MISSION_DIR
do
case $MISSION_DIR in
"" | "#"* )
continue
;;
"!"*)
MISSION_DIR=$(echo "$MISSION_DIR" | cut -c2-)
if [ -z "$MISSION_SUB_NB" ]
then
MISSION_SUB_NB=1
else
MISSION_SUB_NB=$((MISSION_SUB_NB + 1))
fi
;;
*)
MISSION_SUB_NB=""
;;
esac
FULL_NB=$(printf "%04d" $MISSION_NB)
[ -n "$MISSION_SUB_NB" ] && FULL_NB="$FULL_NB-$(printf "%04d" "$MISSION_SUB_NB")"
export MISSION_DIR
MISSION_DIR=$GSH_MISSIONS/$MISSION_DIR
# To be used as TEXTDOMAIN environment variable for the mission.
export DOMAIN=$(textdomainname "$MISSION_DIR")
# Preparing the locales
if [ -d "$MISSION_DIR/i18n" ]
then
# NOTE: shopt -s nullglob doesn't exist in POSIX sh
if [ -n "$(find "$MISSION_DIR/i18n" -maxdepth 1 -name '*.po' -print -quit)" ]
then
for PO_FILE in "$MISSION_DIR"/i18n/*.po; do
PO_LANG=$(basename "$PO_FILE" .po)
MO_FILE="$GSH_ROOT/locale/$PO_LANG/LC_MESSAGES/$DOMAIN.mo"
if ! [ -f "$MO_FILE" ] || [ "$PO_FILE" -nt "$MO_FILE" ]
then
mkdir -p "$GSH_ROOT/locale/$PO_LANG/LC_MESSAGES"
msgfmt -o "$GSH_ROOT/locale/$PO_LANG/LC_MESSAGES/$DOMAIN.mo" "$PO_FILE"
fi
done
fi
fi
# Setting up the binaries
if [ -d "$MISSION_DIR/sbin" ]
then
# NOTE: shopt -s nullglob doesn't exist in POSIX sh
if [ -n "$(find "$MISSION_DIR/sbin" -maxdepth 1 -type f -name '*' -print -quit)" ]
then
for BIN_FILE in "$MISSION_DIR"/sbin/*; do
[ -f "$BIN_FILE" ] && [ -x "$BIN_FILE" ] && copy_bin "$BIN_FILE" "$GSH_SBIN"
done
fi
fi
if [ -d "$MISSION_DIR/bin" ]
then
# NOTE: shopt -s nullglob doesn't exist in POSIX sh
if [ -n "$(find "$MISSION_DIR/bin" -maxdepth 1 -type f -name '*' -print -quit)" ]
then
for BIN_FILE in "$MISSION_DIR"/bin/*; do
[ -f "$BIN_FILE" ] && [ -x "$BIN_FILE" ] && copy_bin "$BIN_FILE" "$GSH_BIN"
done
fi
fi
# source the static part of the mission
if [ -f "$MISSION_DIR/static.sh" ]
then
mission_source "$MISSION_DIR/static.sh"
fi
# copy all the shell config files of the mission
if [ -f "$MISSION_DIR/bashrc" ]
then
BASHRC_FILE=$GSH_BASHRC/bashrc_${FULL_NB}_$(basename "$MISSION_DIR").sh
echo "export MISSION_DIR=$MISSION_DIR" > "$BASHRC_FILE"
echo "export TEXTDOMAIN=$DOMAIN" >> "$BASHRC_FILE"
cat "$MISSION_DIR/bashrc" >> "$BASHRC_FILE"
echo "export TEXTDOMAIN=gsh" >> "$BASHRC_FILE"
unset BASHRC_FILE
fi
if [ "$GSH_MODE" = "DEBUG" ] && [ "$GSH_VERBOSE_DEBUG" = true ]
then
printf ' GSH: mission %3d -> %s\n' "$MISSION_NB" "\$GSH_MISSIONS/${MISSION_DIR#$GSH_MISSIONS/}" >&2
elif [ "$GSH_MODE" = "DEBUG" ]
then
printf "." >&2
else
progress
fi
[ -z "$MISSION_SUB_NB" ] && MISSION_NB=$((MISSION_NB+1))
done < "$GSH_CONFIG/index.txt"
if [ "$MISSION_NB" -eq 1 ]
then
echo "$(gettext "Error: no mission was found!
Aborting.")"
exit 1
fi
if [ "$GSH_MODE" = "DEBUG" ]
then
[ "$GSH_VERBOSE_DEBUG" != true ] && echo "[DONE]" >&2
else
progress_finish
# show cursor back
tput cnorm 2>/dev/null
echo
printf "$(gettext "Press Enter to continue.")"
stty -echo 2>/dev/null # ignore errors, in case input comes from a redirection
read
stty echo 2>/dev/null # ignore errors, in case input comes from a redirection
clear
fi
unset MISSION_DIR MISSION_NB
# show cursor and re-enable echoing of keystrokes
tput cnorm 2>/dev/null
stty echo 2>/dev/null
}
#######################################################################
init_gsh "$@"
cd "$GSH_HOME"
export GSH_UID=$(cat "$GSH_CONFIG/uid")
# make sure the shell reads it's config file by making it interactive (-i)
exec bash --rcfile "$GSH_LIB/bashrc" -i
# vim: shiftwidth=2 tabstop=2 softtabstop=2