about summary refs log tree commit diff stats
path: root/src/launch.sh
blob: 25237672fbee2d51cb6b5a603cd20f85b32e6046 (plain) (blame)
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
#!/bin/sh -xeu
# Launch script for unix systems, loads bundled libraries if there are any.

EXIT_STATUS=0
TMPDIR=${TMPDIR:-/tmp}

failed() {
	ERROR_TEXT="The program appears to not have closed correctly. Please check output.log to see details."
	zenity --error --text="$ERROR_TEXT" || echo "$ERROR_TEXT"
	EXIT_STATUS=1
}

run_game() {
	PAYLOAD_LINE="$(awk '/^__PAYLOAD_BEGINS__/ { print NR + 1; exit 0; }' "$0")" || return 1
	tail -n +"${PAYLOAD_LINE}" "$0" | gzip -d > "$TMPFILE" || return 1
	chmod +x "$TMPFILE" || return 1
	DYLD_LIBRARY_PATH=libs/ LD_LIBRARY_PATH=libs/ "$TMPFILE" > output.log 2>&1 || return 1
	return 0
}

cd "$(dirname "$0")" || exit 1
trap '{ rm -rf "$TMPFILE"; }' EXIT HUP INT QUIT TERM

if ! TMPFILE="./$(mktemp .Pong-XXXXXX)";
then
	echo "Can't extract Pong binary to current directory. Trying $TMPDIR"
	TMPFILE="$(mktemp "$TMPDIR"/Pong-XXXXXX)"
fi

if ! run_game;
then
	failed
fi
exit "$EXIT_STATUS"
__PAYLOAD_BEGINS__