#!/bin/sh
#
# Kiosk Maintenance Script
# Menu-driven interface for managing the MyMo kiosk system
#

# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
    echo "Error: This script must be run as root"
    echo "Usage: sudo kiosk"
    exit 1
fi

# Hostname management
change_hostname() {
    current=$(hostname)
    echo ""
    echo "Current hostname: $current"
    printf "Enter new hostname (or press Enter to cancel): "
    read new_hostname

    if [ -z "$new_hostname" ]; then
        echo "Cancelled"
        return
    fi

    hostnamectl set-hostname "$new_hostname"
    echo "Hostname changed to: $new_hostname"
}

# Browser refresh
refresh_browser() {
    echo ""
    echo "Refreshing browser..."
    pkill chromium
    echo "Browser killed - X session will restart it automatically"
}

# First wired ethernet device
wired_device() {
    nmcli -t -f DEVICE,TYPE device status 2>/dev/null | awk -F: '$2 == "ethernet" { print $1; exit }'
}

# Active connection name for a device
device_connection() {
    nmcli -t -f NAME,DEVICE connection show --active 2>/dev/null | awk -F: -v d="$1" '$2 == d { print $1; exit }'
}

set_dhcp() {
    dev=$(wired_device)
    if [ -z "$dev" ]; then
        echo "Error: no wired ethernet device found"
        return
    fi
    con=$(device_connection "$dev")
    if [ -z "$con" ]; then
        echo "Error: no active connection on $dev"
        return
    fi

    echo ""
    echo "Switching $con ($dev) to DHCP..."
    if nmcli con mod "$con" ipv4.method auto ipv4.addresses "" ipv4.gateway "" \
        && nmcli con up "$con"; then
        echo "DHCP enabled on $dev"
    else
        echo "Error: failed to enable DHCP on $dev"
    fi
}

set_static() {
    dev=$(wired_device)
    if [ -z "$dev" ]; then
        echo "Error: no wired ethernet device found"
        return
    fi
    con=$(device_connection "$dev")
    if [ -z "$con" ]; then
        echo "Error: no active connection on $dev"
        return
    fi

    echo ""
    printf "Enter static address in CIDR form (e.g. 192.168.1.50/24, or press Enter to cancel): "
    read address
    if [ -z "$address" ]; then
        echo "Cancelled"
        return
    fi
    printf "Enter gateway (or press Enter for none): "
    read gateway
    printf "Enter DNS server [1.1.1.1]: "
    read dns
    dns=${dns:-1.1.1.1}

    echo ""
    echo "Applying static configuration to $con ($dev)..."
    if nmcli con mod "$con" \
        ipv4.method manual \
        ipv4.addresses "$address" \
        ipv4.gateway "$gateway" \
        ipv4.dns "$dns" \
        && nmcli con up "$con"; then
        echo "Static configuration applied"
        echo "  address: $address  gateway: ${gateway:-none}  dns: $dns"
    else
        echo "Error: failed to apply static configuration"
    fi
}

connect_wifi() {
    echo ""
    echo "Scanning for Wi-Fi networks..."
    nmcli device wifi list 2>/dev/null || {
        echo "Error: no Wi-Fi device found"
        return
    }
    echo ""
    printf "Enter SSID (or press Enter to cancel): "
    read ssid
    if [ -z "$ssid" ]; then
        echo "Cancelled"
        return
    fi
    printf "Enter password (leave empty for open network): "
    read password

    echo ""
    echo "Connecting to $ssid..."
    if [ -n "$password" ]; then
        nmcli device wifi connect "$ssid" password "$password"
    else
        nmcli device wifi connect "$ssid"
    fi
}

# Network configuration
show_network_menu() {
    while true; do
        echo ""
        echo "=== Network Configuration ==="

        nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device status 2>/dev/null \
            | awk -F: '$2 == "ethernet" || $2 == "wifi" { printf "%s (%s): %s [%s]\n", $1, $2, $3, ($4 ? $4 : "no connection") }'

        # Determine which interface currently has the default route
        current_gw_dev=$(ip route show default 2>/dev/null | head -1 | sed -n 's/.*dev \([^ ]*\).*/\1/p')
        current_gw_ip=$(ip route show default 2>/dev/null | head -1 | sed -n 's/.*via \([^ ]*\).*/\1/p')
        if [ -n "$current_gw_dev" ]; then
            echo "Default route: $current_gw_ip via $current_gw_dev"
        else
            echo "Default route: none"
        fi
        echo ""

        echo "1. Use DHCP (wired)"
        echo "2. Set static IP (wired)"
        echo "3. Connect to Wi-Fi"
        echo "4. Back to main menu"
        echo ""
        printf "Select option: "
        read choice

        case "$choice" in
            1) set_dhcp ;;
            2) set_static ;;
            3) connect_wifi ;;
            4) return ;;
            *) echo "Invalid option" ;;
        esac
    done
}

# Main menu
main_menu() {
    while true; do
        echo ""
        echo "=== MyMo Kiosk Maintenance Menu ==="
        echo "1. Change hostname"
        echo "2. Refresh browser"
        echo "3. Network configuration"
        echo "4. Exit"
        echo ""
        printf "Select option: "
        read choice

        case "$choice" in
            1) change_hostname ;;
            2) refresh_browser ;;
            3) show_network_menu ;;
            4) echo "Goodbye"; exit 0 ;;
            *) echo "Invalid option" ;;
        esac
    done
}

# Run main menu
main_menu
