#!/bin/sh
# Inkline managed launcher
set -eu
bundle=/home/root/.local/share/inkline
case "${1:-help}" in
    start|demo)
        action=$1; shift
        rotation=90; font_size=0; seconds=0; usb_settings=0
        while [ "$#" -gt 0 ]; do
            case "$1" in
                --rotate) test "$#" -ge 2; rotation=$2; shift 2 ;;
                --font-size) test "$#" -ge 2; test "$2" -ge 6 && test "$2" -le 48; font_size=$2; shift 2 ;;
                --quit-after) test "$#" -ge 2; seconds=$2; shift 2 ;;
                --usb-settings) usb_settings=1; shift ;;
                *) echo "Unknown Inkline option: $1" >&2; exit 2 ;;
            esac
        done
        case "$rotation" in 0|90|180|270) ;; *) echo 'Rotation must be 0, 90, 180 or 270.' >&2; exit 2 ;; esac
        case "$font_size:$seconds" in *[!0-9:]*|:*|*:) exit 2 ;; esac
        test "$font_size" -eq 0 || { test "$font_size" -ge 6 && test "$font_size" -le 48; }
        test "$seconds" -ge 0 && test "$seconds" -le 60
        test -f "$bundle/.inkline-managed"
        test -x "$bundle/current/inkline"
        exec 9>/run/inkline-manage.lock
        flock -x 9
        if systemctl is-active --quiet inkline.service; then
            systemctl kill --kill-whom=all --signal=CONT inkline.service 2>/dev/null || :
            "$bundle/current/inkline" --redraw 2>/dev/null || :
            if [ "$usb_settings" = 1 ]; then "$bundle/current/inkline" --show-usb; fi
            echo 'Inkline is already running.'; exit 0
        fi
        umask 077
        {
            printf 'INKLINE_ROTATE=%s\nINKLINE_FONT_SIZE=%s\nINKLINE_QUIT_AFTER=%s\n' "$rotation" "$font_size" "$seconds"
            printf 'INKLINE_USB_SETTINGS=%s\n' "$usb_settings"
            if [ "$action" = demo ]; then echo 'INKLINE_DEMO=1'; else echo 'INKLINE_DEMO=0'; fi
        } > /run/inkline-launch.env
        systemctl link --runtime "$bundle/current/inkline.service"
        systemctl daemon-reload
        systemctl reset-failed inkline.service 2>/dev/null || :
        systemctl start inkline.service
        echo 'Inkline started. Tap Quit or press Ctrl+Shift+Q to return to notebooks.'
        ;;
    stop)
        systemctl stop inkline.service
        systemctl start xochitl.service
        ;;
    status)
        systemctl status inkline.service --no-pager
        ;;
    shortcut)
        shift
        if [ "$#" -eq 0 ]; then set -- help; fi
        exec "$bundle/current/inkline-hotkey" "$@"
        ;;
    usb)
        shift
        exec "$bundle/current/usb/inkline-usb" "$@"
        ;;
    type)
        shift
        exec "$bundle/current/usb/keyboard-send" "$@"
        ;;
    tablet)
        shift
        exec "$bundle/current/usb/tablet-send" "$@"
        ;;
    icon)
        shift
        exec /bin/sh "$bundle/current/icon/install.sh" "${1:-install}"
        ;;
    battery)
        shift
        exec "$bundle/current/inkline-battery" "$@"
        ;;
    wifi)
        shift
        exec "$bundle/current/inkline-wifi" "$@"
        ;;
    run)
        shift
        test "$#" -gt 0 || { echo 'Usage: ~/inkline run PROGRAM [ARG ...]' >&2; exit 2; }
        exec "$bundle/current/shortcut-launch.sh" terminal "$@"
        ;;
    manual)
        if [ "${2:-}" = --install ]; then
            exec 9>/run/inkline-manage.lock
            flock -x 9
            exec "$bundle/current/install-manual.sh"
        fi
        exec "$bundle/current/inkline" --show-documents "$bundle/current/docs/Inkline Manual.pdf"
        ;;
    documents)
        shift
        if [ "${1:-}" = --list ]; then exec "$bundle/current/inkline" --list-documents; fi
        exec "$bundle/current/inkline" --show-documents "$@"
        ;;
    cleanup)
        shift
        if [ "$#" -eq 0 ] || [ "$1" = --check ]; then exec /bin/sh "$bundle/current/cleanup-app.sh" "$@"; fi
        exec "$bundle/current/prune-releases.sh" "$@"
        ;;
    uninstall)
        exec "$bundle/current/uninstall-device.sh"
        ;;
    help|--help|-h)
        echo 'Usage: ~/inkline start|demo [--rotate 0|90|180|270] [--font-size 6..48] [--quit-after 1..60]'
        echo '       ~/inkline start --usb-settings (touch setup; no virtual keyboard)'
        echo '       ~/inkline icon install|check|remove (AppLoad icon)'
        echo '       ~/inkline stop|status|battery|manual|uninstall'
        echo '       ~/inkline documents [FILE.pdf|--list] (library, reader, or named paths)'
        echo '       ~/inkline wifi status|list|pick|connect|disconnect|forget'
        echo '       ~/inkline run PROGRAM [ARG ...]'
        echo '       ~/inkline cleanup [UTILITY] [--check]'
        echo '       ~/inkline shortcut register|deregister|list|help'
        echo '       ~/inkline usb network|send-keyboard|send-tablet|receive-keyboard|status'
        echo '       tablet-send --host windows|mac|linux [--output pen|mouse]'
        echo '       keyboard-send --file FILE --profile us|mac|linux|windows'
        echo '       ~/inkline type ... remains a compatibility alias'
        ;;
    *) echo 'Run ~/inkline --help for usage.' >&2; exit 2 ;;
esac
