#!/bin/sh
# nfmc-flirc-setup.sh — teach a Flirc USB receiver the buttons NeoFlux Media
# Center expects, one remote button at a time.
#
# A Flirc appears to Linux as an ordinary USB keyboard: you pair each button
# of whatever IR remote you own with a key, the Flirc remembers it, and from
# then on pressing the button types that key. Nothing on the computer needs
# a driver. This script runs flirc_util's recording mode for each key nfmc's
# default keys.toml binds, prompting you to press the matching remote button.
#
# Needs flirc_util (Flirc's command-line tool, from https://flirc.tv/ — the
# Linux download, or Flirc's apt repository on Debian/Ubuntu) and the Flirc
# plugged into THIS machine. Run as your normal user; if flirc_util cannot
# open the device, Flirc's udev rule (shipped with its package) is missing or
# you need to re-plug the receiver.
#
# Already-paired buttons are skipped unless you pass --force. Press `s` at a
# prompt to skip that key, Ctrl-C to stop (what you recorded so far stays).
set -u

if ! command -v flirc_util >/dev/null 2>&1; then
    echo "flirc_util not found. Install Flirc's Linux tools from https://flirc.tv/ first." >&2
    exit 1
fi

force=0
[ "${1:-}" = "--force" ] && force=1

# key-name as flirc_util spells it | what to tell the person | what nfmc does with it
KEYS='
up|Up|move the menu / dock-app highlight up
down|Down|move down
left|Left|move left (closes a menu)
right|Right|move right (opens a submenu)
return|OK / Select / Enter|activate the highlighted item
escape|Back / Exit|close the menu, leave the slit, back out
F1|Home / Menu|open the app menu (nfmc binds F1 to root-menu)
tab|Guide / next (any spare button)|next window / next dock app in the slit
F2|Movie / fullscreen (any spare button)|movie mode on and off (nfmc binds F2)
vol_up|Volume up|system volume up
vol_down|Volume down|system volume down
mute|Mute|mute toggle
play/pause|Play / Pause|play or pause the current player
'

echo "Point the remote at the Flirc. For each key below, press the remote button"
echo "you want to use when asked (s = skip this key, Ctrl-C = stop)."
echo
printf '%s\n' "$KEYS" | while IFS='|' read -r key label purpose; do
    [ -z "$key" ] && continue
    printf '\n== %s  (%s)\n   %s\n' "$label" "$key" "$purpose"
    printf '   Press the remote button now (or s + Enter to skip): '
    # flirc_util record waits for an IR press itself; give the person a
    # chance to skip first by reading a line with a short timeout isn't
    # portable sh, so: a plain prompt, then record.
    read -r answer </dev/tty || answer=""
    case "$answer" in
        s|S) echo "   skipped"; continue ;;
    esac
    if [ "$force" = 1 ]; then
        flirc_util record "$key" </dev/tty
    else
        # record refuses a button that is already paired; that is fine.
        flirc_util record "$key" </dev/tty || echo "   (not recorded — already paired, or flirc_util rejected the name \"$key\"; run 'flirc_util record' with no name to list the names it knows)"
    fi
done

echo
echo "Done. Test: press buttons with a terminal focused and watch the keys appear."
echo "Pairings live in the Flirc itself; 'flirc_util format' erases them all."
