forked from phyver/GameShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·567 lines (503 loc) · 15.1 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/bin/sh
# 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
export GSH_WORKING_DIR=$(pwd -P)
export GSH_ROOT="$(dirname "$0")"
# shellcheck source=scripts/gsh_gettext.sh
. "$GSH_ROOT/scripts/gsh_gettext.sh"
# 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")"
}
# possible values: index, simple (default), overwrite
export GSH_SAVEFILE_MODE="simple"
export GSH_AUTOSAVE=1
export GSH_COLOR="OK"
GSH_MODE="ANONYMOUS"
# if GSH_NO_GETTEXT is non-empty, gettext won't be used anywhere, the only language will thus be English
# export GSH_NO_GETTEXT=1 # DO NOT CHANGE OR REMOVE THIS LINE, it is used by utils/archive.sh
RESET=""
# hack to parse long options --index-savefiles --overwrite-savefiles --simple-savefiles
# cf https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
_long_option=0
while getopts ":hnPdDACRXUVqGL:KBZc:F-:" opt
do
if [ "$opt" = "-" ]
then
opt="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$opt}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
_long_option=1
fi
case $opt in
index-savefiles)
GSH_SAVEFILE_MODE=index
;;
simple-savefiles)
GSH_SAVEFILE_MODE=simple
;;
overwrite-savefiles)
GSH_SAVEFILE_MODE=overwrite
;;
h)
display_help
exit 0
;;
n)
GSH_COLOR=""
;;
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)
export LANGUAGE="$OPTARG" # only works on GNU systems
;;
G)
export GSH_NO_GETTEXT=1
;;
X | U)
echo "$(gettext "Error: this option is only available from an executable archive!")" >&2
exit 1
;;
V)
# when lib/header.sh sees the -V flag, it displays the version and exits,
# so the next case isn't used.
# this is only used when running GameShell directly from start.sh
if git rev-parse --is-inside-work-tree >/dev/null 2>&1
then
echo "GameShell $(git describe --always --tags --dirty)"
fi
echo "run directly from start.sh"
exit 0;
;;
B)
export GSH_SHELL=bash
;;
Z)
export GSH_SHELL=zsh
;;
c)
GSH_COMMAND=$OPTARG
;;
K|F)
: # used by the self-extracting archive
;;
*)
if [ "$_long_option" = "1" ]
then
OPTARG="-$opt"
fi
echo "$(eval_gettext "Error: invalid option: '-\$OPTARG'")" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ $(id -u) -eq 0 ]
then
echo "$(gettext "Error: you shouldn't run Gameshell as root!")" >&2
exit 1
fi
# check we have a shell compatible with GameShell
if [ -z "$GSH_SHELL" ]
then
if [ -n "$BASH_VERSION" ]
then
export GSH_SHELL=bash
elif [ -n "$ZSH_VERSION" ]
then
export GSH_SHELL=zsh
else
case "$SHELL" in
*bash)
export GSH_SHELL=$SHELL
;;
*zsh)
export GSH_SHELL=$SHELL
;;
*)
echo "$(eval_gettext "Error: unknown shell '\$SHELL'.
Run GameShell with either bash or zsh.")" >&2
exit 1
;;
esac
fi
fi
_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
# remove all the game data
rm -rf "$GSH_HOME"
rm -rf "$GSH_CONFIG"
rm -rf "$GSH_TMP"
rm -rf "$GSH_BIN"
rm -rf "$GSH_SBIN"
# recreate them
mkdir -p "$GSH_HOME"
mkdir -p "$GSH_CONFIG"
awk -v seed_file="$GSH_CONFIG/PRNG_seed" 'BEGIN { srand(); printf("%s", int(2^32 * rand())) > seed_file; }'
cp "$GSH_LIB/gshrc" "$GSH_CONFIG"
# save current locale
locale > "$GSH_CONFIG"/config.sh
echo "export GSH_MODE=$GSH_MODE" >> "$GSH_CONFIG"/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_TMP"
# 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"
### generate GameShell translation files
# NOTE: nullglob don't expand in POSIX sh and there is no shopt -s nullglob as in bash
if [ -z "$GSH_NO_GETTEXT" ] && command -v msgfmt >/dev/null && [ -n "$(find "$GSH_ROOT/i18n" -maxdepth 1 -name '*.po' | head -n1)" ]
then
for PO_FILE in "$GSH_ROOT"/i18n/*.po; do
PO_LANG=$(basename "$PO_FILE" .po)
MO_FILE="$GSH_ROOT/locale/$PO_LANG/LC_MESSAGES/$TEXTDOMAIN.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/$TEXTDOMAIN.mo" "$PO_FILE"
fi
done
fi
# hide cursor and disable echoing of keystrokes
tput civis 2>/dev/null
stty -echo 2>/dev/null
# those traps will be redefined in lib/gsh.sh
trap "tput cnorm 2>/dev/null; stty echo 2>/dev/null; echo" INT TERM EXIT
# Clear the screen.
if [ "$GSH_MODE" = "DEBUG" ]
then
printf "[MISSION INITIALISATION]" >&2
elif [ -z "$GSH_QUIET_INTRO" ]
then
clear
fi
make_index "$@" | sed -e "s;$GSH_MISSIONS;.;" > "$GSH_CONFIG/index.txt"
if [ "$GSH_MODE" != "DEBUG" ]
then
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 [ -z "$GSH_NO_GETTEXT" ] && command -v msgfmt >/dev/null && [ -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' | head -n1)" ]
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 '*' | head -n1)" ]
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 '*' | head -n1)" ]
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/gshrc" ]
then
GSHRC_FILE=$GSH_CONFIG/gshrc_${FULL_NB}_$(basename "$MISSION_DIR").sh
{
echo "export MISSION_DIR=\"$MISSION_DIR\"";
echo "export TEXTDOMAIN=\"$DOMAIN\"";
} >"$GSHRC_FILE"
cat "$MISSION_DIR/gshrc" >> "$GSHRC_FILE"
echo "export TEXTDOMAIN=gsh" >> "$GSHRC_FILE"
unset GSHRC_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.")" >&2
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
if [ -z "$GSH_QUIET_INTRO" ]
then
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
else
echo
fi
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 "$@"
# change the HOME dir, but save the "real" one in a variable
export REAL_HOME="$HOME"
export HOME="$GSH_HOME"
# set TMPDIR, that may be used by external scripts like mktemp
export TMPDIR="$GSH_TMP"
### test some of the scripts
if ! sh "$GSH_ROOT/lib/bin_test.sh"
then
echo "$(gettext "Error: a least one base function is not working properly.
Aborting!")"
exit 1
fi
cd "$GSH_HOME"
export GSH_UID=$(cat "$GSH_CONFIG/uid")
date "+%Y-%m-%d %H:%M:%S" | sed 's/^/#>>> /' >> "$GSH_CONFIG/missions.log"
# put a ".save" file to indicate the archive needs to be saved on exit
touch "$GSH_ROOT/.save"
# if the user uses a special TERMINFO entry, it might not be found because
# GameShell redefines HOME
if [ -z "$TERMINFO" ]
then
export TERMINFO=$REAL_HOME/.terminfo
else
# this might be run with sh, which doesn't have variable string substitution
TERMINFO=$(echo "$TERMINFO" | sed -e "s#~#$REAL_HOME#g")
fi
generate_rcfile
if [ -n "$GSH_COMMAND" ]
then
# NOTE, with "-c", environment isn't inherited by bash / zsh
# we need to re-source profile.sh
# exec $GSH_SHELL -i -c "GSH_ROOT=\"$GSH_ROOT\"
# . \"\$GSH_ROOT/lib/profile.sh\"
# $GSH_COMMAND"
# NOTE, the above works in bash, but when running the following script with
# GSH_SHELL=zsh, it fails with "zsh: suspended (tty output)"
# ======== script =======
# #!/bin/sh
# ./gameshell.sh -qc "gsh exit"; ./gameshell.sh -qc "gsh exit"
# =======================
# FIX: don't start the shell in interactive mode, and source the rcfile
# explicitly
case "$GSH_SHELL" in
*bash)
RC_FILE=.bashrc
;;
*zsh)
RC_FILE=.zshrc
;;
esac
# start GameShell
exec $GSH_SHELL -c "export GSH_NON_INTERACTIVE=1
GSH_ROOT=\"$GSH_ROOT\"
. \"\$GSH_ROOT/lib/profile.sh\"
. \"\$GSH_HOME/$RC_FILE\"
$GSH_COMMAND"
else
# start GameShell
exec $GSH_SHELL
fi
# vim: shiftwidth=2 tabstop=2 softtabstop=2