#!/bin/sh
# 777 — the house launcher for iSH.
#
# Pick a number from the menu, or say it up front:
#   777        opens the menu
#   777 3      runs the guards board, no menu
#
# 1) Talk to Brother Zero     4) Flint3 guards
# 2) 777software.net status   5) Mesh board
# 3) Security Center doors    6) SSH to the house box

set -u

# Find a sibling tool first (same folder), fall back to the PATH.
find_tool() {
    here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
    if [ -x "$here/$1" ]; then
        printf '%s\n' "$here/$1"
    else
        printf '%s\n' "$1"
    fi
}

site_status() {
    code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 15 https://777software.net)
    echo "777software.net answered: $code"
    if [ "$code" = "200" ]; then
        echo "The front door is OPEN and well."
    else
        echo "That is not a clean 200. The door may be having a moment."
    fi
}

doors_list() {
    echo "SECURITY CENTER DOORS (home Wi-Fi)"
    echo "The box is 192.168.8.166. Pages live on port 7077."
    echo ""
    echo "  Overview    http://192.168.8.166:7077/"
    echo "  MESH        http://192.168.8.166:7077/mesh.html"
    echo "  FLINT3      http://192.168.8.166:7077/flint3-guards.html"
    echo "  BROTHER ZERO http://192.168.8.166:7077/brotherzero.html"
    echo "  THE LOUNGE  http://192.168.8.166:9001/"
    echo ""
    echo "Open one in the iPad browser, or read it here"
    echo "with viewpage, like:  viewpage http://192.168.8.166:7077/mesh.html"
}

run_choice() {
    case "$1" in
        1) exec "$(find_tool bz)" ;;
        2) site_status ;;
        3) doors_list ;;
        4) exec "$(find_tool guards)" ;;
        5) exec "$(find_tool mesh)" ;;
        6) exec ssh matrix ;;
        *)
            echo "Pick a number from 1 to 6."
            return 1
            ;;
    esac
}

if [ $# -ge 1 ]; then
    run_choice "$1"
    exit $?
fi

echo ""
echo "777 HOUSE LAUNCHER"
echo "------------------"
echo "  1) Talk to Brother Zero"
echo "  2) 777software.net status"
echo "  3) Security Center doors"
echo "  4) Flint3 guards"
echo "  5) Mesh board"
echo "  6) SSH to the house box (matrix)"
echo ""
printf 'Pick a number: '
read -r choice
run_choice "$choice"
