don't munge "=> ?!"
[gemrepl.git] / examples / zyg.sh
blobe264739c04352a9ec3954f4ab01a8948f9855c4c
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 function readline {
8 echo -n '<' >&3
9 read line
10 ret=$?
11 echo -n '>' >&3
12 echo "$line"
13 return $ret
16 [ -n "$TLS_CLIENT_HASH" ] || exit 1
18 if [ -n "$SPAWN_PARAMETER" ]; then
19 sanitised="$(tr -dc [:alnum:]_ <<< "$SPAWN_PARAMETER")"
20 gamefile="$var/games/$sanitised"
21 if ! ( [ -n "$sanitised" ] && [ -e "$gamefile" ] ); then
22 echo "Unknown game: $sanitised"
23 exit 1
25 else
26 echo "Choose your poison:"
27 ls -1 "$var/games/"
29 while input="$(readline)"; do
30 sanitised="$(tr -dc [:alnum:]_ <<< "$input")"
31 gamefile="$var/games/$sanitised"
32 if [ -n "$sanitised" ] && [ -e "$gamefile" ]; then
33 break;
35 echo "Please enter one of the listed games"
36 done
39 echo "Starting $sanitised. Savefiles will be preserved."
40 echo
42 savesdir="$var/$TLS_CLIENT_HASH/$sanitised"
44 mkdir -p "$savesdir" || exit 1
45 cd "$savesdir" || exit 1
47 # Impose some limits to reduce potential for abuse
48 ulimit -t 1200
49 ulimit -f 5000
50 ulimit -v 20000
52 if [[ "$(realpath "$gamefile")" =~ \.gblorb$ ]]; then
53 stdbuf -i0 -o0 cheapgit-zyg "$gamefile"
54 else
55 # using the z-machine interpreter infuse, which has a nice stdio interface:
56 # https://gitlab.com/monkeymind/infuse
57 # Lightly hacked to improve output and to write to stderr when prompting
58 stdbuf -i0 -o0 infuse "$gamefile"
60 # Previous attempt: using dfrotz, but it has some problems.
61 #dfrotz -m -Z0 -R. "$gamefile"