#!/bin/sh
set -eu

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH:-}"
export PATH

LABEL="DMR+_IPSC3-VKDMR"
DMR_ID="5050"
HOST="ipsc3.vkdmr.com"
PASSWORD="passw0rd"
PORT="55555"
HOSTS_FILE="/root/DMR_Hosts.txt"
UPDATE_SCRIPT="/usr/local/sbin/HostFilesUpdate.sh"

if [ "$(id -u)" -ne 0 ]; then
    echo "This installer must run as root. Use:"
    echo "  curl -fsSL https://ipsc3.vkdmr.com/downloads/pi-star-ipsc3-vkdmr | sudo sh"
    exit 1
fi

run_if_available() {
    for candidate in "$@"; do
        if [ -x "$candidate" ]; then
            "$candidate"
            return $?
        fi
    done
    return 127
}

hosts_file_writable() {
    hosts_dir="$(dirname "$HOSTS_FILE")"
    probe="$hosts_dir/.ipsc3-rw-test.$$"

    if ( : > "$probe" ) 2>/dev/null; then
        rm -f "$probe"
        return 0
    fi

    return 1
}

make_rw() {
    if hosts_file_writable; then
        return 0
    fi

    echo "Trying Pi-Star rpi-rw..."
    run_if_available /usr/local/sbin/rpi-rw /usr/sbin/rpi-rw /sbin/rpi-rw || {
        rpi_rw_path="$(command -v rpi-rw 2>/dev/null || true)"
        if [ -n "$rpi_rw_path" ]; then
            "$rpi_rw_path" || true
        fi
    }
    if hosts_file_writable; then
        return 0
    fi

    echo "Trying direct root filesystem remount..."
    mount -o remount,rw / 2>/dev/null || true
    if hosts_file_writable; then
        return 0
    fi

    echo "Trying direct /root remount..."
    mount -o remount,rw /root 2>/dev/null || true
    if hosts_file_writable; then
        return 0
    fi

    echo "Failed to make $HOSTS_FILE writable."
    echo "Current mounts:"
    mount
    exit 1
}

make_ro() {
    if run_if_available /usr/local/sbin/rpi-ro /usr/sbin/rpi-ro /sbin/rpi-ro; then
        return 0
    fi

    rpi_ro_path="$(command -v rpi-ro 2>/dev/null || true)"
    if [ -n "$rpi_ro_path" ]; then
        "$rpi_ro_path" || true
        return 0
    fi

    mount -o remount,ro /root 2>/dev/null || true
    mount -o remount,ro / 2>/dev/null || true
}

cleanup() {
    make_ro || true
}

trap cleanup EXIT INT TERM

echo "Switching Pi-Star filesystem read-write..."
make_rw

entry="$(printf '%s\t%s\t%s\t%s\t%s' "$LABEL" "$DMR_ID" "$HOST" "$PASSWORD" "$PORT")"
tmp_file="$(mktemp)"

touch "$HOSTS_FILE"
awk -v label="$LABEL" -v entry="$entry" '
    BEGIN { replaced = 0 }
    $1 == label {
        if (!replaced) {
            print entry
            replaced = 1
        }
        next
    }
    { print }
    END {
        if (!replaced) {
            print entry
        }
    }
' "$HOSTS_FILE" > "$tmp_file"

cat "$tmp_file" > "$HOSTS_FILE"
rm -f "$tmp_file"

echo "Installed tab-delimited host entry:"
printf '%s[TAB]%s[TAB]%s[TAB]%s[TAB]%s\n' "$LABEL" "$DMR_ID" "$HOST" "$PASSWORD" "$PORT"

if [ ! -x "$UPDATE_SCRIPT" ]; then
    echo "Host update script not found or not executable: $UPDATE_SCRIPT"
    exit 1
fi

echo "Updating Pi-Star host files..."
if command -v sudo >/dev/null 2>&1; then
    sudo "$UPDATE_SCRIPT"
else
    "$UPDATE_SCRIPT"
fi

sync
echo "Switching Pi-Star filesystem read-only..."
trap - EXIT INT TERM
make_ro
sleep 1
sync
sleep 2

printf "Reboot Pi-Star now? [y/N] "
read -r reboot_answer
case "$reboot_answer" in
    y|Y|yes|YES)
        echo "Rebooting Pi-Star..."
        reboot
        ;;
    *)
        echo "Reboot skipped. Reboot Pi-Star manually before selecting IPSC3-VKDMR."
        ;;
esac
