pass initial query as INIT_QUERY rather than writing it
[gemrepl.git] / examples / zyg.sh
blobeaeda457b9549c6cbedbdd6f6d4f998109d02069
1 #!/bin/sh
2 # Run z-machine interpreter, allowing the user to save their game in a
3 # directory specific to their client cert.
5 var=/var/gemini/zyg/
7 [ -n "$TLS_CLIENT_HASH" ] || exit 1
9 if [ -n "$INIT_QUERY" ]; then
10 sanitised="$(tr -dc [:alnum:]_ <<< "$INIT_QUERY")"
11 gamefile="$var/games/$sanitised"
12 if ! ( [ -n "$sanitised" ] && [ -e "$gamefile" ] ); then
13 echo "Unknown game: $sanitised"
14 exit 1
16 else
17 echo "Choose your poison:"
18 ls -1 "$var/games/"
20 while read input; do
21 sanitised="$(tr -dc [:alnum:]_ <<< "$input")"
22 gamefile="$var/games/$sanitised"
23 if [ -n "$sanitised" ] && [ -e "$gamefile" ]; then
24 break;
26 echo "Please enter one of the listed games"
27 done
30 echo "Starting $sanitised. Savefiles will be preserved."
31 echo
33 savesdir="$var/$TLS_CLIENT_HASH/$sanitised"
35 mkdir -p "$savesdir" || exit 1
36 cd "$savesdir" || exit 1
38 # Impose some limits to reduce potential for abuse
39 ulimit -t 1200
40 ulimit -f 5000
41 ulimit -v 20000
43 # using the z-machine interpreter infuse, which has a nice stdio interface:
44 # https://gitlab.com/monkeymind/infuse
45 stdbuf -i0 -o0 infuse "$gamefile"
47 # Previous attempt: using dfrotz, but it has some problems.
48 #dfrotz -m -Z0 -R. "$gamefile"