#!/usr/bin/env bash
set -uo pipefail

APP_TITLE="40 Winks PC Port"
EXPECTED_SHA256="057232ef7618e25f5645df50d3cd45f08cb5a2cccb3e2fdf48faa8755c4ddb1a"
APPDIR="${FORTY_WINKS_BUILDER_APPDIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}"
PAYLOAD_DIR="$APPDIR/usr/share/40-winks-pc-port/source"
VERSION_FILE="$APPDIR/usr/share/40-winks-pc-port/VERSION"
BUILDER_VERSION="$(cat "$VERSION_FILE" 2>/dev/null || printf 'dev')"
BUILD_ID_FILE="$APPDIR/usr/share/40-winks-pc-port/BUILD_ID"
PORT_BUILD_ID="$(cat "$BUILD_ID_FILE" 2>/dev/null || printf '%s' "$BUILDER_VERSION")"
PUBLIC_APPIMAGE="${APPIMAGE:-}"

CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME:-/tmp}/.config}"
DATA_HOME="${XDG_DATA_HOME:-${HOME:-/tmp}/.local/share}"
CACHE_HOME="${XDG_CACHE_HOME:-${HOME:-/tmp}/.cache}"
CONFIG_DIR="$CONFIG_HOME/40-winks-pc-port"
DATA_DIR="$DATA_HOME/40-winks-pc-port"
CACHE_DIR="$CACHE_HOME/40-winks-pc-port"
SOURCE_DIR="$CACHE_DIR/source"
EXTERNAL_DIR="$CACHE_DIR/external/$PORT_BUILD_ID"
BUILD_LOG="$CACHE_DIR/build.log"
ROM_RECORD="$CONFIG_DIR/rom-path"
GAME_PATH_RECORD="$CONFIG_DIR/playable-path"
INSTALLED_VERSION="$DATA_DIR/builder-version"
DEFAULT_GAME_APPIMAGE="$DATA_DIR/40-Winks-Recompiled-x86_64.AppImage"
GAME_APPIMAGE="$DEFAULT_GAME_APPIMAGE"
DIALOG_BACKEND=""

usage() {
    cat <<EOF
Usage: $(basename "$0") [OPTION] [ROM]

Build and launch 40 Winks from a user-supplied ROM.

  --rom PATH      use PATH as the ROM
  --rebuild       rebuild the locally generated playable AppImage
  --select-rom    open the ROM chooser and rebuild
  --choose-output choose where the playable AppImage is installed
  --output PATH   install the playable AppImage at PATH
  --build-only    build the playable AppImage without launching it
  -- GAME_ARGS    pass the remaining arguments to the playable game
  -h, --help      show this help
EOF
}

detect_dialog() {
    if [ -n "$DIALOG_BACKEND" ] || [ "${FORTY_WINKS_BUILDER_NO_GUI:-0}" = "1" ]; then
        return
    fi

    if command -v zenity >/dev/null 2>&1; then
        DIALOG_BACKEND="zenity"
    elif command -v yad >/dev/null 2>&1; then
        DIALOG_BACKEND="yad"
    elif command -v kdialog >/dev/null 2>&1; then
        DIALOG_BACKEND="kdialog"
    fi
}

show_error() {
    local message="$1"
    message="${message//\\n/$'\n'}"
    detect_dialog
    case "$DIALOG_BACKEND" in
        zenity) zenity --error --title="$APP_TITLE" --width=680 --text="$message" 2>/dev/null || true ;;
        yad) yad --error --title="$APP_TITLE" --width=680 --text="$message" 2>/dev/null || true ;;
        kdialog) kdialog --title "$APP_TITLE" --error "$message" 2>/dev/null || true ;;
        *) printf '%s: %s\n' "$APP_TITLE" "$message" >&2 ;;
    esac
}

show_info() {
    local message="$1"
    message="${message//\\n/$'\n'}"
    detect_dialog
    case "$DIALOG_BACKEND" in
        zenity) zenity --info --title="$APP_TITLE" --width=620 --text="$message" 2>/dev/null || true ;;
        yad) yad --info --title="$APP_TITLE" --width=620 --text="$message" 2>/dev/null || true ;;
        kdialog) kdialog --title "$APP_TITLE" --msgbox "$message" 2>/dev/null || true ;;
        *) printf '%s\n' "$message" ;;
    esac
}

choose_rom() {
    local selected=""
    detect_dialog

    case "$DIALOG_BACKEND" in
        zenity)
            selected="$(zenity --file-selection \
                --title="Choose your 40 Winks N64 ROM" \
                --filename="${HOME:-/}/Desktop/" \
                --file-filter="Nintendo 64 ROMs | *.z64 *.n64 *.v64" \
                --file-filter="All files | *" 2>/dev/null)" || return 1
            ;;
        yad)
            selected="$(yad --file-selection \
                --title="Choose your 40 Winks N64 ROM" \
                --filename="${HOME:-/}/Desktop/" 2>/dev/null)" || return 1
            ;;
        kdialog)
            selected="$(kdialog --title "Choose your 40 Winks N64 ROM" \
                --getopenfilename "${HOME:-/}/Desktop/" \
                "Nintendo 64 ROMs (*.z64 *.n64 *.v64)" 2>/dev/null)" || return 1
            ;;
        *)
            if [ -t 0 ]; then
                printf 'Path to your clean 40 Winks ROM: ' >&2
                IFS= read -r selected || return 1
            else
                show_error "No graphical file chooser was found. Install zenity, yad, or kdialog, or drag the ROM onto the AppImage."
                return 1
            fi
            ;;
    esac

    [ -n "$selected" ] || return 1
    printf '%s\n' "$selected"
}

normalize_output_path() {
    local selected="$1"

    case "$selected" in
        /*) ;;
        *) selected="$PWD/$selected" ;;
    esac

    case "$selected" in
        *.AppImage) ;;
        *) selected="$selected.AppImage" ;;
    esac

    readlink -m -- "$selected"
}

choose_output_path() {
    local applications_dir="${HOME:-/tmp}/Applications"
    local suggested="$applications_dir/40-Winks-Recompiled-x86_64.AppImage"
    local selected=""

    mkdir -p "$applications_dir" 2>/dev/null || true
    detect_dialog

    case "$DIALOG_BACKEND" in
        zenity)
            selected="$(zenity --file-selection --save --confirm-overwrite \
                --title="Choose where to put the playable build" \
                --filename="$suggested" \
                --file-filter="AppImage | *.AppImage" \
                --file-filter="All files | *" 2>/dev/null)" || return 1
            ;;
        yad)
            selected="$(yad --file-selection --save --confirm-overwrite \
                --title="Choose where to put the playable build" \
                --filename="$suggested" 2>/dev/null)" || return 1
            ;;
        kdialog)
            selected="$(kdialog --title "Choose where to put the playable build" \
                --getsavefilename "$suggested" \
                "AppImage (*.AppImage)" 2>/dev/null)" || return 1
            ;;
        *)
            if [ -t 0 ]; then
                printf 'Playable AppImage path [%s]: ' "$suggested" >&2
                IFS= read -r selected || return 1
                [ -n "$selected" ] || selected="$suggested"
            else
                show_error "No graphical file chooser was found. Install zenity, yad, or kdialog, or use --output PATH."
                return 1
            fi
            ;;
    esac

    [ -n "$selected" ] || return 1
    normalize_output_path "$selected"
}

validate_output_path() {
    local output_path="$1"
    local rom_path="$2"

    if [ "$output_path" = "$(readlink -m -- "$rom_path")" ]; then
        show_error "The playable build cannot replace the selected ROM. Choose a different output path."
        return 1
    fi

    if [ -n "$PUBLIC_APPIMAGE" ] && \
       [ "$output_path" = "$(readlink -m -- "$PUBLIC_APPIMAGE")" ]; then
        show_error "The playable build cannot replace the self-building AppImage. Choose a different output path."
        return 1
    fi

    return 0
}

rom_hash() {
    sha256sum "$1" 2>/dev/null | awk '{print $1}'
}

rom_is_valid() {
    [ -f "$1" ] && [ "$(rom_hash "$1")" = "$EXPECTED_SHA256" ]
}

validate_rom() {
    local rom="$1"
    local actual=""

    if [ ! -f "$rom" ]; then
        show_error "ROM not found:\n$rom"
        return 1
    fi

    actual="$(rom_hash "$rom")"
    if [ "$actual" != "$EXPECTED_SHA256" ]; then
        show_error "Unsupported ROM revision.\n\nExpected SHA-256:\n$EXPECTED_SHA256\n\nSelected file SHA-256:\n$actual"
        return 1
    fi

    return 0
}

check_build_tools() {
    local tool=""
    local missing=()
    local required=(awk bash cc c++ cmake curl git install ldd make pkg-config python3 readlink sha256sum)

    for tool in "${required[@]}"; do
        command -v "$tool" >/dev/null 2>&1 || missing+=("$tool")
    done

    if [ "${#missing[@]}" -ne 0 ]; then
        show_error "Some build tools are missing: ${missing[*]}\n\nInstall the Linux build requirements listed at:\nhttps://github.com/Faderz48/40-Winks-PC-Port/blob/main/docs/BUILDING.md"
        return 1
    fi

    return 0
}

prepare_source_tree() {
    local staged_source="$CACHE_DIR/source.next"

    if [ ! -d "$PAYLOAD_DIR" ]; then
        show_error "The launcher source payload is missing. Download the AppImage again from the official GitHub release."
        return 1
    fi

    if [ ! -f "$SOURCE_DIR/.builder-version" ] || \
       [ "$(cat "$SOURCE_DIR/.builder-version" 2>/dev/null)" != "$PORT_BUILD_ID" ]; then
        rm -rf "$staged_source" || return 1
        mkdir -p "$staged_source" || return 1
        if ! cp -a "$PAYLOAD_DIR/." "$staged_source/"; then
            show_error "The bundled source could not be copied into the private build cache."
            return 1
        fi
        printf '%s\n' "$PORT_BUILD_ID" > "$staged_source/.builder-version" || return 1
        rm -rf "$SOURCE_DIR" || return 1
        mv "$staged_source" "$SOURCE_DIR" || return 1
    fi

    mkdir -p "$EXTERNAL_DIR" "$SOURCE_DIR/work" || return 1
    rm -rf "$SOURCE_DIR/work/external" || return 1
    ln -s "$EXTERNAL_DIR" "$SOURCE_DIR/work/external" || return 1
}

run_build_command() {
    local rom="$1"
    local jobs="${FORTY_WINKS_BUILD_JOBS:-2}"

    (
        unset APPDIR APPIMAGE ARGV0 OWD
        cd "$SOURCE_DIR" || exit 1
        make playable-appimage ROM="$rom" JOBS="$jobs"
    )
}

estimate_build_progress() {
    local log_file="$1"

    if [ ! -s "$log_file" ]; then
        printf '2|Preparing local build'
        return
    fi

    awk '
        BEGIN {
            submodules = 0
            tool_percent = 0
            runtime_percent = 0
        }
        {
            line = $0

            if (line ~ /Cloning into .*N64Recomp/) clone_n64 = 1
            if (line ~ /Cloning into .*N64ModernRuntime/) clone_runtime = 1
            if (line ~ /Cloning into .*rt64/) clone_rt64 = 1
            if (line ~ /^Submodule path /) submodules++
            if (line ~ /Build files have been written to: .*\/N64Recomp\/build/) tool_started = 1
            if (line ~ /Pinned dependencies are ready/) dependencies_ready = 1
            if (line ~ /Generated [0-9]+ function symbols/) symbols_generated = 1
            if (line ~ /Generated local recompilation sources/) cpu_generated = 1
            if (line ~ /Build files have been written to: .*\/build\/recomp-appimage/) runtime_started = 1

            if (line ~ /^\[[[:space:]]*[0-9]+%\]/) {
                percent = line
                sub(/^\[[[:space:]]*/, "", percent)
                sub(/%.*/, "", percent)
                if (runtime_started) runtime_percent = percent + 0
                else if (tool_started) tool_percent = percent + 0
            }

            if (line ~ /Installing: .*\/usr\/bin\/forty-winks-recomp/) runtime_installed = 1
            if (line ~ /^Generating squashfs/) packaging = 1
            if (line ~ /^Success[[:space:]]*$/) packaged = 1
        }
        END {
            progress = 2
            status = "Preparing local build"

            if (packaged) {
                progress = 99
                status = "Finalizing playable build"
            } else if (packaging) {
                progress = 97
                status = "Packaging playable game"
            } else if (runtime_installed) {
                progress = 94
                status = "Installing playable runtime"
            } else if (runtime_started) {
                progress = 52 + int(runtime_percent * 40 / 100)
                status = "Compiling playable runtime"
            } else if (cpu_generated) {
                progress = 50
                status = "Configuring playable runtime"
            } else if (symbols_generated) {
                progress = 47
                status = "Generating CPU translation"
            } else if (dependencies_ready) {
                progress = 44
                status = "Generating CPU translation"
            } else if (tool_started) {
                progress = 20 + int(tool_percent * 22 / 100)
                status = "Building CPU translation tools"
            } else {
                progress = 4 + int(submodules * 14 / 35)
                if (progress > 18) progress = 18
                if (clone_n64 && progress < 5) progress = 5
                if (clone_runtime && progress < 9) progress = 9
                if (clone_rt64 && progress < 14) progress = 14
                status = "Downloading build dependencies"
            }

            printf "%d|%s", progress, status
        }
    ' "$log_file"
}

build_game() {
    local rom="$1"
    local build_pid=0
    local build_status=0
    local progress_pid=0
    local built_appimage="$SOURCE_DIR/dist/recomp-appimage/40-Winks-Recompiled-x86_64.AppImage"
    local error_tail=""
    local output_dir=""

    mkdir -p "$CONFIG_DIR" "$DATA_DIR" "$CACHE_DIR"
    output_dir="$(dirname "$GAME_APPIMAGE")"
    if ! mkdir -p "$output_dir"; then
        show_error "The selected output folder could not be created:\n$output_dir"
        return 1
    fi
    : > "$BUILD_LOG"
    detect_dialog

    if [ "$DIALOG_BACKEND" = "zenity" ] || [ "$DIALOG_BACKEND" = "yad" ]; then
        run_build_command "$rom" >"$BUILD_LOG" 2>&1 &
        build_pid=$!

        (
            last_progress=0
            printf '0\n# 0%% - Starting local build\n'
            while kill -0 "$build_pid" 2>/dev/null; do
                progress_state="$(estimate_build_progress "$BUILD_LOG")"
                progress="${progress_state%%|*}"
                progress_status="${progress_state#*|}"

                case "$progress" in
                    ''|*[!0-9]*) progress="$last_progress" ;;
                esac
                if [ "$progress" -lt "$last_progress" ]; then
                    progress="$last_progress"
                fi
                last_progress="$progress"

                printf '%s\n# %s%% - %s\n' "$progress" "$progress" "$progress_status"
                sleep 1
            done
            printf '100\n# Build complete\n'
        ) | "$DIALOG_BACKEND" --progress --auto-close --no-cancel \
            --title="$APP_TITLE" --width=540 \
            --text="Preparing the first playable build. This can take several minutes." \
            2>/dev/null &
        progress_pid=$!

        wait "$build_pid"
        build_status=$?
        wait "$progress_pid" 2>/dev/null || true
    else
        show_info "The local game build is starting. The first build can take several minutes.\n\nBuild log:\n$BUILD_LOG"
        run_build_command "$rom" 2>&1 | tee "$BUILD_LOG"
        build_status=${PIPESTATUS[0]}
    fi

    if [ "$build_status" -ne 0 ]; then
        error_tail="$(tail -n 18 "$BUILD_LOG" 2>/dev/null || true)"
        show_error "The playable build did not complete.\n\nBuild log:\n$BUILD_LOG\n\nLast output:\n$error_tail"
        return 1
    fi

    if [ ! -s "$built_appimage" ]; then
        show_error "The build completed without producing the expected playable AppImage.\n\nBuild log:\n$BUILD_LOG"
        return 1
    fi

    if ! install -m 755 "$built_appimage" "$GAME_APPIMAGE.new" || \
       ! mv "$GAME_APPIMAGE.new" "$GAME_APPIMAGE" || \
       ! printf '%s\n' "$rom" > "$ROM_RECORD.new" || \
       ! mv "$ROM_RECORD.new" "$ROM_RECORD" || \
       ! printf '%s\n' "$GAME_APPIMAGE" > "$GAME_PATH_RECORD.new" || \
       ! mv "$GAME_PATH_RECORD.new" "$GAME_PATH_RECORD" || \
       ! printf '%s\n' "$PORT_BUILD_ID" > "$INSTALLED_VERSION.new" || \
       ! mv "$INSTALLED_VERSION.new" "$INSTALLED_VERSION"; then
        show_error "The playable build succeeded but could not be installed under:\n$DATA_DIR"
        return 1
    fi
    return 0
}

if [ -n "${FORTY_WINKS_PROGRESS_LOG_ONLY:-}" ]; then
    estimate_build_progress "$FORTY_WINKS_PROGRESS_LOG_ONLY"
    printf '\n'
    exit 0
fi

launch_game() {
    local rom="$1"
    shift

    unset FORTY_WINKS_BUILDER_APPDIR APPDIR APPIMAGE ARGV0 OWD
    export APPIMAGE_EXTRACT_AND_RUN=1
    exec "$GAME_APPIMAGE" --rom "$rom" "$@"
}

MODE="auto"
ROM_PATH=""
ROM_EXPLICIT=0
CHOOSE_OUTPUT=0
OUTPUT_EXPLICIT=0
GAME_ARGS=()

while [ "$#" -gt 0 ]; do
    case "$1" in
        --rom)
            shift
            if [ "$#" -eq 0 ]; then
                show_error "--rom requires a file path."
                exit 2
            fi
            ROM_PATH="$1"
            ROM_EXPLICIT=1
            ;;
        --rebuild) MODE="rebuild" ;;
        --select-rom) MODE="select" ;;
        --choose-output) CHOOSE_OUTPUT=1 ;;
        --output)
            shift
            if [ "$#" -eq 0 ]; then
                show_error "--output requires a file path."
                exit 2
            fi
            GAME_APPIMAGE="$(normalize_output_path "$1")"
            OUTPUT_EXPLICIT=1
            ;;
        --build-only) MODE="build-only" ;;
        -h|--help)
            usage
            exit 0
            ;;
        --)
            shift
            GAME_ARGS=("$@")
            break
            ;;
        -*)
            show_error "Unknown option: $1"
            exit 2
            ;;
        *)
            if [ -n "$ROM_PATH" ]; then
                show_error "Only one ROM path may be supplied."
                exit 2
            fi
            ROM_PATH="$1"
            ROM_EXPLICIT=1
            ;;
    esac
    shift
done

mkdir -p "$CONFIG_DIR" "$DATA_DIR" "$CACHE_DIR"

if [ "$OUTPUT_EXPLICIT" -eq 0 ] && [ -f "$GAME_PATH_RECORD" ]; then
    saved_game_path="$(head -n 1 "$GAME_PATH_RECORD")"
    if [ -n "$saved_game_path" ]; then
        GAME_APPIMAGE="$saved_game_path"
    fi
fi

if [ -z "$ROM_PATH" ] && [ "$MODE" != "select" ] && [ -f "$ROM_RECORD" ]; then
    ROM_PATH="$(head -n 1 "$ROM_RECORD")"
fi

if [ "$MODE" = "auto" ] && [ -x "$GAME_APPIMAGE" ] && \
   [ "$CHOOSE_OUTPUT" -eq 0 ] && [ "$OUTPUT_EXPLICIT" -eq 0 ] && \
   [ -f "$INSTALLED_VERSION" ] && \
   [ "$(cat "$INSTALLED_VERSION" 2>/dev/null)" = "$PORT_BUILD_ID" ] && \
   [ -n "$ROM_PATH" ] && rom_is_valid "$ROM_PATH"; then
    launch_game "$ROM_PATH" "${GAME_ARGS[@]}"
fi

if [ "$MODE" = "select" ] || [ -z "$ROM_PATH" ] || \
   { [ "$ROM_EXPLICIT" -eq 0 ] && ! rom_is_valid "$ROM_PATH"; }; then
    ROM_PATH="$(choose_rom)" || exit 0
fi

validate_rom "$ROM_PATH" || exit 1

if [ "$OUTPUT_EXPLICIT" -eq 0 ] && \
   { [ "$CHOOSE_OUTPUT" -eq 1 ] || [ ! -x "$GAME_APPIMAGE" ]; }; then
    GAME_APPIMAGE="$(choose_output_path)" || exit 0
fi

validate_output_path "$GAME_APPIMAGE" "$ROM_PATH" || exit 1
check_build_tools || exit 1
prepare_source_tree || exit 1
build_game "$ROM_PATH" || exit 1

if [ "$MODE" = "build-only" ]; then
    show_info "The playable build is ready at:\n$GAME_APPIMAGE"
    exit 0
fi

launch_game "$ROM_PATH" "${GAME_ARGS[@]}"
