#!/bin/bash
# ASK WHICH WAY THIS SCREEN IS HUNG. ONCE, ON THE UNIT, BY THE PERSON HANGING IT.
#
# The orientation used to be baked into the image — pinned to 180 because a
# photograph of the installer console looked upside down. That was a guess about
# somebody else's wall, made from a picture, and it is not knowable from here:
# the same board goes on a wall landscape in one branch and rotated portrait in
# the next, and only the person mounting it can see which.
#
# So this asks, and writes the answer to the operator override that
# maestro-kds-orientation already treats as final.
#
# IT MUST NEVER LEAVE A SCREEN STUCK ON A QUESTION. If nobody answers inside the
# timeout it falls through to automatic detection — accelerometer, then the
# panel's own hint, then the native mode — and the kiosk starts. A unit shipped
# to a branch with no keyboard still comes up; it just might come up sideways,
# which somebody can fix in one command afterwards. A board waiting forever for
# a keypress nobody is there to make is a board showing no tickets.
set -u

OVERRIDE=/etc/maestro-kds/orientation
TIMEOUT="${MAESTRO_SETUP_TIMEOUT:-90}"

# Already answered — say nothing and get out of the way. This is what makes the
# question a first-boot event rather than something a cook sees every morning.
if [ "${1:-}" != "--again" ] && [ -r "$OVERRIDE" ]; then
    exit 0
fi

mkdir -p "$(dirname "$OVERRIDE")"

cat <<'BANNER'

  ────────────────────────────────────────────────────────────
    MAESTRO KDS — WHICH WAY IS THIS SCREEN MOUNTED?
  ────────────────────────────────────────────────────────────

    1)  Landscape                 (wider than tall, normal)
    2)  Portrait — rotate RIGHT   (turned clockwise)
    3)  Portrait — rotate LEFT    (turned anticlockwise)
    4)  Landscape, upside down    (hung inverted)

    Enter = detect automatically

  ────────────────────────────────────────────────────────────
    Console:  Ctrl+Alt+F2   (login: maestro)
    Back:     Ctrl+Alt+F1
    The screen keeps running while you are on the console.
  ────────────────────────────────────────────────────────────

BANNER
printf '  Choice [1-4], %ss to answer: ' "$TIMEOUT"

# bash, not sh, for ONE reason: `read -t`. Ubuntu's /bin/sh is dash, whose read
# has no timeout at all — so the sh version had to guard with `[ -t 0 ]` and
# skip reading entirely when stdin was not a terminal. That silently discarded
# every answer when driven from a pipe (which is how it is tested), and gave no
# real timeout on a tty either. It reported the default for all four choices.
#
# read -t returns non-zero on timeout or EOF, and that is exactly the
# "nobody answered" case: fall through to detection and start the kiosk.
if ! read -r -t "$TIMEOUT" choice; then
    choice=""
fi

case "$choice" in
    1) v=normal ;;
    2) v=90     ;;   # wlroots turns the OUTPUT, so a screen rotated clockwise
    3) v=270    ;;   # needs the image turned the same way to read upright
    4) v=180    ;;
    *)
        echo
        echo "  No choice — detecting automatically."
        echo "  To set it later:  sudo maestro-kds-setup --again"
        exit 0
        ;;
esac

printf '%s\n' "$v" > "$OVERRIDE"
echo
echo "  Saved: $v"
echo "  To change it later:  sudo maestro-kds-setup --again"
echo
exit 0
