#!/bin/sh
# Launcher for TRX. /usr/share/games/trx/ is read-only, so the engine is
# run with the TRX_* variables of the upstream split install
# (docs/trx/INSTALLING.md) pointing to the XDG directories of the user:
#   TRX_DIR        $XDG_DATA_HOME/trx    games/, modules/, scripts/, saves/,
#                                        screenshots/, TRX.log
#   TRX_CONFIG_DIR $XDG_CONFIG_HOME/trx  configuration
#   TRX_CACHE_DIR  $XDG_CACHE_HOME/trx   cache
# The files of trx-data are linked there; a file of the user with the same
# name is kept and used instead.
# Written by Claude Opus 5.5 <noreply@anthropic.com>, reviewed by Nicolas HOUDELOT.
set -e

sysdir=/usr/share/games/trx
engine=/usr/libexec/trx/trx

# xdg_dir VALUE DEFAULT: the XDG directory, DEFAULT under $HOME when VALUE is
# empty or not an absolute path
xdg_dir() {
	case "$1" in
	/*) printf '%s\n' "$1" ;;
	*) printf '%s\n' "$HOME/$2" ;;
	esac
}
xdg_data=$(xdg_dir "${XDG_DATA_HOME:-}" .local/share)
xdg_config=$(xdg_dir "${XDG_CONFIG_HOME:-}" .config)
datadir=$xdg_data/trx
configdir=$xdg_config/trx
cachedir=$(xdg_dir "${XDG_CACHE_HOME:-}" .cache)/trx

# link_tree SRC DEST: link every file of SRC to the same place under DEST
link_tree() {
	mkdir -p "$2"
	(cd "$1" && find . -type d) | while IFS= read -r d; do
		[ -d "$2/$d" ] || mkdir -p "$2/$d"
	done
	(cd "$1" && find . -type f) | while IFS= read -r f; do
		[ -e "$2/$f" ] || [ -L "$2/$f" ] || ln -s "$1/${f#./}" "$2/$f"
	done
}

# link_files DEST FILE...: link the files to DEST, unless already there
link_files() {
	dest=$1
	shift
	for f in "$@"; do
		[ -f "$f" ] || continue
		mkdir -p "$dest"
		name=${f##*/}
		[ -e "$dest/$name" ] || [ -L "$dest/$name" ] || ln -s "$f" "$dest/$name"
	done
}

# links to files removed from trx-data or from the old directories
for d in "$datadir" "$configdir"; do
	[ -d "$d" ] || continue
	find "$d" -xtype l \( -lname "$sysdir/*" -o -lname "/usr/share/games/tr[12]x/*" \
		-o -lname "$xdg_data/games/tr[12]x/*" \) -exec rm -f {} +
done

link_tree "$sysdir/cfg" "$configdir"
for d in games modules scripts; do
	link_tree "$sysdir/$d" "$datadir/$d"
done
mkdir -p "$cachedir"

# files of the tr1x and tr2x packages before 1.12
for n in 1 2; do
	userold=$xdg_data/games/tr${n}x
	# configuration, in ~/.config/trNx/ before 1.0.3-0ddebs2
	for f in TR${n}X.json5 TR${n}X_console_history.json5; do
		for src in "$userold/cfg/$f" "$xdg_config/tr${n}x/$f"; do
			if [ -s "$src" ] && [ ! -e "$configdir/$f" ]; then
				cp "$src" "$configdir/$f"
			fi
		done
	done
	# saved games, copied once
	if [ -d "$userold/saves" ] && [ ! -e "$datadir/saves/.tr${n}x-migrated" ]; then
		mkdir -p "$datadir/saves"
		for f in "$userold"/saves/*; do
			if [ -f "$f" ] && [ ! -e "$datadir/saves/${f##*/}" ]; then
				cp -p "$f" "$datadir/saves/"
			fi
		done
		: > "$datadir/saves/.tr${n}x-migrated"
	fi
	# original game data copied in the old directories
	for old in "$userold" "/usr/share/games/tr${n}x"; do
		[ -d "$old" ] || continue
		games=$datadir/games/tr$n
		if [ "$n" = 1 ]; then
			link_files "$games/levels" "$old"/data/*.phd "$old"/data/*.PHD
		else
			link_files "$games/levels" "$old"/data/*.tr2 "$old"/data/*.TR2
			link_files "$games" "$old"/data/main.sfx "$old"/data/MAIN.SFX
		fi
		link_files "$games/fmv" "$old"/fmv/*
		link_files "$games/music" "$old"/music/*
	done
done

export TRX_DIR="$datadir"
export TRX_CONFIG_DIR="$configdir"
export TRX_CACHE_DIR="$cachedir"

# the engine also looks for saved games in the current directory
cd "$datadir"
exec "$engine" "$@"
