#!/bin/sh
# Turn mods for "Ein Königreich für ein Lama" on and off.
#
#   lama-mod an|on       enable the mods in ~/.wine-lama/mods/*/
#   lama-mod aus|off     original state (L: = unmodified CD data)
#   lama-mod status
#   lama-mod debug an    enable the debug view (collision/invisible objects)
#   lama-mod debug aus   disable the debug view (other mods stay as they are)
#
# A mod is a folder whose files are laid out like on the CD under
# Setup/Eng/Cd (e.g. wads/T1L9M001.wad, T191.COR, movies/191INTRO.DAT).
# Activation uses an overlay of symlinks: original files stay untouched,
# mod files are added or replace files of the same name.
# Mods with a file ".aus" ("off") in their folder are skipped (this is how
# "lama-mod debug an|aus" toggles the "debug" mod independently of the others).
# Order: alphabetical, later mods win (testlevel > debug).
set -e
export WINEPREFIX="${WINEPREFIX:-${LAMA_WINEPREFIX:-$HOME/.wine-lama}}"
MODS="$WINEPREFIX/mods"
OVL="$WINEPREFIX/cd-overlay"
DBG="$MODS/debug"

if [ -d /opt/ein-koenigreich-fuer-ein-lama/cd ]; then
    BASE=/opt/ein-koenigreich-fuer-ein-lama/cd
else
    BASE="$WINEPREFIX/iso-dk/Setup/Eng/Cd"
fi

overlay_an() {
    rm -rf "$OVL"
    mkdir -p "$OVL"
    (cd "$BASE" && find . -type d) | while IFS= read -r d; do mkdir -p "$OVL/$d"; done
    (cd "$BASE" && find . -type f) | while IFS= read -r f; do ln -s "$BASE/$f" "$OVL/$f"; done
    for m in "$MODS"/*/; do
        [ -d "$m" ] || continue
        if [ -e "$m/.aus" ]; then
            echo "Mod: $(basename "$m") (disabled)"
            continue
        fi
        echo "Mod: $(basename "$m")"
        (cd "$m" && find . -path ./src -prune -o -name .aus -prune -o \( -type f -o -type l \) -print) | while IFS= read -r f; do
            # replace an existing file of the same name (case-insensitive)
            dir=$(dirname "$f"); name=$(basename "$f")
            mkdir -p "$OVL/$dir"
            find "$OVL/$dir" -maxdepth 1 -iname "$name" -exec rm -f {} +
            ln -s "$m/$f" "$OVL/$f"
            [ -n "$LEISE" ] || echo "  + $f"
        done
    done
    ln -sfn "$OVL" "$WINEPREFIX/dosdevices/l:"
    echo "Mods active (L: -> $OVL)"
}

case "${1:-status}" in
an|on)
    overlay_an
    ;;
aus|off)
    ln -sfn "$BASE" "$WINEPREFIX/dosdevices/l:"
    rm -rf "$OVL"
    echo "Original state (L: -> $BASE)"
    ;;
debug)
    if [ ! -d "$DBG/wads" ]; then
        echo "Debug mod missing ($DBG/wads)." >&2; exit 1
    fi
    case "${2:-status}" in
    an|on)
        rm -f "$DBG/.aus"
        LEISE=1 overlay_an
        echo "Debug view ON ($(ls "$DBG/wads" | wc -l) levels). Takes effect the next time a level is loaded."
        ;;
    aus|off)
        touch "$DBG/.aus"
        if [ "$(readlink "$WINEPREFIX/dosdevices/l:")" = "$OVL" ]; then
            LEISE=1 overlay_an
        fi
        echo "Debug view OFF."
        ;;
    status)
        if [ -e "$DBG/.aus" ]; then echo "Debug view: off"; else echo "Debug view: on (if mods are active)"; fi
        ;;
    *)
        sed -n '2,9p' "$0"; exit 1 ;;
    esac
    ;;
status)
    echo "L: -> $(readlink "$WINEPREFIX/dosdevices/l:")"
    for m in "$MODS"/*/; do
        [ -d "$m" ] || continue
        if [ -e "$m/.aus" ]; then s=" (disabled)"; else s=""; fi
        echo "installed mod: $(basename "$m")$s"
    done
    ;;
*)
    sed -n '2,9p' "$0"; exit 1 ;;
esac
