#!/bin/sh
# openxcom: starts OpenXcom 1.0 on the N900. It
#   1. finds the files of the original UFO: Enemy Unknown (MyDocs/..., or the folder in ~/.openxcom/ufo-path, or $OPENXCOM_UFO),
#   2. builds ~/.openxcom/data: the data of the engine plus links to the original files (OpenXcom 1.0 looks a file up only by its exact
#      name, in UPPER case or in lower case, so the links get the upper-case names it asks for),
#   3. writes the settings for the N900 once (full screen 800x480, the keyboard grab, Ctrl instead of the missing Esc key),
#   4. starts the game. Nothing of the original game is copied or changed.
OPT=/opt/openxcom
H="${OPENXCOM_HOME:-$HOME/.openxcom}"
DATA="$H/data"; USERD="$H/user"

say() {   # a message in the terminal, and as a banner when there is a screen
    echo "openxcom: $*" >&2
    dbus-send --session --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications \
        org.freedesktop.Notifications.SystemNoteInfoprint "string:OpenXcom: $*" >/dev/null 2>&1 || true
}

if [ "$(id -u)" = 0 ]; then
    echo "openxcom: run it as the user (su user -c 'DISPLAY=:0 openxcom'), not as root" >&2
    exit 1
fi

# 1. the original game: a folder that has GEODATA/PALETTES.DAT (any case)
ufo_ok() { [ -d "$1" ] && find "$1" -maxdepth 2 -iname palettes.dat 2>/dev/null | grep -qi 'geodata/palettes.dat'; }
find_ufo() {
    [ -n "$OPENXCOM_UFO" ] && { echo "$OPENXCOM_UFO"; return; }
    if [ -s "$H/ufo-path" ]; then read -r p < "$H/ufo-path"; ufo_ok "$p" && { echo "$p"; return; }; fi
    for p in "$H/UFO" "$HOME/MyDocs/UFO" "$HOME/MyDocs/XCOM" "$HOME/MyDocs/X-Com" "$HOME/MyDocs/Games/UFO" "$HOME/MyDocs/Games/X-Com" \
             "$HOME/MyDocs/Games/DosBox/X-Com" "$HOME/MyDocs/Games/DosBox/UFO"; do
        ufo_ok "$p" && { echo "$p"; return; }
    done
    find "$HOME/MyDocs" -maxdepth 5 -iname palettes.dat 2>/dev/null | while IFS= read -r f; do
        d=$(dirname "$f")
        case $(basename "$d" | tr 'A-Z' 'a-z') in geodata) dirname "$d"; break ;; esac
    done
}
UFO=$(find_ufo)
if [ -z "$UFO" ] || ! ufo_ok "$UFO"; then
    say "the files of the original UFO: Enemy Unknown were not found. Copy the game folder (it has GEODATA, MAPS, SOUND...) to MyDocs, or write its path to $H/ufo-path"
    exit 1
fi

# 2. the data folder (rebuilt when the engine or the path of the game changed)
mkdir -p "$DATA" "$USERD"
STAMP="$UFO|$(ls -l "$OPT/bin/openxcom" | tr -s ' ' | cut -d' ' -f5-8)"
if [ "$(cat "$H/data.stamp" 2>/dev/null)" != "$STAMP" ] || [ ! -d "$DATA/GEODATA" ]; then
    say "preparing the data folder (first start only)"
    find "$DATA" -mindepth 1 -delete 2>/dev/null
    # the engine's own files; the three folders that the original game also has are merged below
    for e in "$OPT"/share/data/*; do
        b=$(basename "$e")
        case $b in MAPS|ROUTES|TERRAIN) ;; *) ln -s "$e" "$DATA/$b" ;; esac
    done
    # the folders of the original game (any case) with upper-case links to the files, plus the files of the engine
    for dir in GEODATA GEOGRAPH MAPS ROUTES SOUND TERRAIN UFOGRAPH UNITS UFOINTRO; do
        mkdir -p "$DATA/$dir"
        src=$(ls "$UFO" | while IFS= read -r n; do [ "$(echo "$n" | tr 'a-z' 'A-Z')" = "$dir" ] && { echo "$n"; break; }; done)
        if [ -n "$src" ] && [ -d "$UFO/$src" ]; then
            ls "$UFO/$src" | while IFS= read -r f; do
                ln -sf "$UFO/$src/$f" "$DATA/$dir/$(echo "$f" | tr 'a-z' 'A-Z')"
            done
        fi
        if [ -d "$OPT/share/data/$dir" ]; then
            ls "$OPT/share/data/$dir" | while IFS= read -r f; do
                [ -e "$DATA/$dir/$f" ] || ln -s "$OPT/share/data/$dir/$f" "$DATA/$dir/$f"
            done
        fi
    done
    echo "$STAMP" > "$H/data.stamp"
fi

# 3. settings for the N900, only when there are none: the game keeps its own file afterwards
if [ ! -f "$USERD/options.cfg" ]; then
    cat > "$USERD/options.cfg" <<'EOT'
options:
  displayWidth: 800
  displayHeight: 480
  fullscreen: true
  captureMouse: true
  keyCancel: 306
  keyGeoOptions: 306
  keyBattleOptions: 306
EOT
fi

# 4. start (the game needs about 30 s to load its rulesets). It crashes (segmentation fault, before it writes a log) when the current folder is
#    one that the user cannot read, e.g. /root after "su user" from a root shell, so it starts from the home folder.
cd "$HOME" 2>/dev/null || cd /
exec "$OPT/bin/openxcom" -data "$DATA/" -user "$USERD/" "$@"
