#!/bin/sh

# RMSSH: a deliberately small SSH launcher for the reMarkable 2 Paper Terminal app.
set -u
umask 077

CONFIG_DIR="${HOME:-/home/root}/.config/rmssh"
KNOWN_HOSTS="${HOME:-/home/root}/.ssh/known_hosts"
STATE_FILE="${PAPERTERMINAL_STATE_FILE:-$CONFIG_DIR/state}"

mkdir -p "$CONFIG_DIR" "${HOME:-/home/root}/.ssh"
chmod 700 "$CONFIG_DIR"
chmod 700 "${HOME:-/home/root}/.ssh"
touch "$KNOWN_HOSTS"
chmod 600 "$KNOWN_HOSTS"

# The graphical session picker must never type a connection command into an
# active remote shell. This local, owner-only state is checked before sending.
STATE_REVISION=0
set_state() {
    STATE_REVISION=$((STATE_REVISION + 1))
    printf '%s %s %s\n' "$1" "$$" "$STATE_REVISION" > "$STATE_FILE.$$"
    mv "$STATE_FILE.$$" "$STATE_FILE"
}
cleanup() {
    if [ "$(awk '{print $2}' "$STATE_FILE" 2>/dev/null)" = "$$" ]; then
        rm -f "$STATE_FILE"
    fi
    rm -f "$STATE_FILE.$$"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM

saved_connection() {
    # Deliberately not eval/source: all fields are values in a small protocol,
    # not commands. Hosts/usernames are ASCII SSH identifiers, never options.
    IFS=' ' read -r _verb _host _user _port _extra <<EOF
$1
EOF
    case $_host in ''|-*|*[!a-zA-Z0-9._:%-]*) return ;; esac
    case $_user in ''|-*|*[!a-zA-Z0-9_.@-]*) return ;; esac
    case $_port in ''|*[!0-9]*) return ;; esac
    [ "${#_host}" -le 255 ] && [ "${#_user}" -le 128 ] &&
        [ "${#_port}" -le 5 ] && [ "$_port" -ge 1 ] && [ "$_port" -le 65535 ] &&
        [ -z "$_extra" ] || return
    connect_profile "$_user@$_host" "$_user" "$_host" "$_port"
}

clear_screen() {
    printf '\033[2J\033[H'
}

pause() {
    printf '\nPress Enter to return to the menu...'
    IFS= read -r _answer
}

connect_profile() {
    set_state busy
    _name=$1
    _user=$2
    _host=$3
    _port=${4:-22}
    _identity=${5:-}
    _command=${6:-}
    _terminal=${7:-xterm-256color}
    _compression=${8:-no}

    clear_screen
    printf 'RMSSH — %s\n\n' "$_name"
    printf 'Connecting to %s@%s:%s\n\n' "$_user" "$_host" "$_port"
    printf 'Terminal: %s   Compression: %s\n\n' "$_terminal" "$_compression"

    set -- ssh -tt \
        -o StrictHostKeyChecking=ask \
        -o "UserKnownHostsFile=$KNOWN_HOSTS" \
        -o ServerAliveInterval=30 \
        -p "$_port"
    case "$_compression" in
        yes|true|1) set -- "$@" -C ;;
    esac
    if [ -n "$_identity" ]; then
        set -- "$@" -i "$_identity"
    fi
    # Treat both user input fields as values, never as ssh command options.
    set -- "$@" -l "$_user" -- "$_host"
    if [ -n "$_command" ]; then
        set -- "$@" "$_command"
    fi
    TERM=$_terminal
    export TERM
    "$@"
    _status=$?
    set_state closed
    printf '\nConnection closed (code %s).\n' "$_status"
    pause
}

quick_connect() {
    set_state busy
    clear_screen
    printf 'RMSSH — quick connection\n\n'
    printf 'Host or IP address: '
    IFS= read -r _host
    [ -n "$_host" ] || return
    printf 'Username: '
    IFS= read -r _user
    [ -n "$_user" ] || return
    printf 'Port [22]: '
    IFS= read -r _port
    _port=${_port:-22}
    connect_profile "$_user@$_host" "$_user" "$_host" "$_port" "" "" "xterm-256color" "no"
}

# The native model keeps output before the first painted frame, so startup
# needs no artificial delay. Tests can still request a deliberate delay.
set_state starting
if [ "${PAPERDESK_RMSSH_START_DELAY:-0}" != 0 ]; then
    sleep "$PAPERDESK_RMSSH_START_DELAY"
fi

while :; do
    clear_screen
    printf 'Paper Terminal — reMarkable 2\n'
    printf '====================================\n\n'
    printf '\n  n) New connection\n'
    printf '  l) Local recovery shell\n'
    printf '  q) Close application\n\n'
    # Publish readiness before the final PTY write that wakes the frontend.
    set_state ready
    printf 'Choice: '

    IFS= read -r _choice || exit 0
    case "$_choice" in
        n|N) quick_connect ;;
        'c '*) saved_connection "$_choice" ;;
        l|L)
            set_state busy
            clear_screen
            printf 'Local shell. Type exit to return to Paper Terminal.\n\n'
            /bin/sh -l
            ;;
        q|Q) exit 0 ;;
        *) ;;
    esac
done
