#!/bin/sh
# rpitalk-config - config script for RPITalk
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2026 John Heim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Install this file to /usr/lib/rpitalk/gadget.sh
#
# === read defaults from emulator.conf ===#
getConfig() {
    sed -n "s/^$1[[:space:]]*=[[:space:]]*//p" "$EmulatorConfig" | tail -n 1
    }

#=== MAIN ===#
set -e

Self=$(basename $0)
test -z "$EmulatorConfig" && EmulatorConfig="/etc/rpitalk/emulator.conf"
test -z "$GadgetConfig" && GadgetConfig="/etc/rpitalk/gadget.conf"
test -z "$Verbose" && Verbose=0

# === ask port ===#
DefaultPort="$(getConfig port)"
test -c /dev/ttyGS0 && DefaultPort="ttyGS0"
test -c /dev/ttyUSB0 && DefaultPort="ttyUSB0"
test -z "$DefaultPort" && DefaultPort="ttyGS0"
case "$DefaultPort" in
    ttyGS0) Default=1;;
    ttyUSB0) Default=2;;
    ttyS0) Default=3;;
    *) Default=4
esac
while :; do
    echo "Select port:"
    echo "  1) ttyGS0  (OTG port)"
    echo "  2) ttyUSB0  (USB-serial converter)"
    echo "  3) ttyS0    (real serial port on  a desktop machine)"
    echo "  4) Other  (enter manually)"
    printf "Choice [default: %s]: " "$Default"
    read Answer
    test -z "$Answer" && Answer="$Default"
    case "$Answer" in
        1) Port="ttyGS0"; break ;;
        2) Port="ttyUSB0"; break ;;
        3) Port="ttyS0"; break ;;
        4)
            printf "Enter port [default: %s]: " "$DefaultPort"
            read Port
            test -z "$Port" && Port="$DefaultPort"
            break;;
        *) echo "Please enter 1, 2,  3, or 4." ;;
    esac
done

# === ask emulation ===#
DefaultEmulator="$(getConfig emulate)"
test "$Port" = "ttyGS0" && DefaultEmulator="dectlk"
test "$Port" = "ttyUSB0" && DefaultEmulator="ltlk"
test -z "$DefaultEmulator" && DefaultEmulator="dectlk"
case "$DefaultEmulator" in
    dectlk) Default=1;;
    ltlk) Default=2;;
    *) Default="none"
esac
while :; do
    echo "Select emulation type:"
    if [ "$Port" = "ttyGS0" ]; then
        echo "  1) dectlk (DECtalk Express, highly recommended)"
    else
        echo "  1) dectlk (DECtalk Express)"
    fi
    if [ "$Port" = "ttyUSB0" ]; then
        echo "  2) ltlk   (LiteTalk, recommended)"
    else
        echo "  2) ltlk   (LiteTalk)"
    fi
    printf "Choice [default: %s]: " "$Default"
    read Answer
    test -z "$Answer" && Answer="$Default"
    case "$Answer" in
        1) Emulator="dectlk"; break ;;
        2) Emulator="ltlk"; break ;;
        *) echo "Please enter 1 or 2." ;;
    esac
done
# === ask debug level ===#
DefaultDebug="$(getConfig debug)"
test  -z "$DefaultDebug" && DefaultDebug="0"
while :; do
    printf "Enter debug level (0-3) [default: %s]: " "$DefaultDebug"
    read Debug
    test -z "$Debug" && Debug="$DefaultDebug"

    case "$Debug" in
        0|1|2|3) break ;;
        *) echo "debug level must be 0, 1, 2, or 3." ;;
    esac
done

# === update emulator.conf (preserve comments) ===#
sed -i \
    -e "s/^emulate[[:space:]]*=.*/emulate = $Emulator/" \
    -e "s/^port[[:space:]]*=.*/port = $Port/" \
    -e "s/^debug[[:space:]]*=.*/debug = $Debug/" \
    "$EmulatorConfig"

# === set gadget values based on emulation ===#
if [ "$Emulator" = "dectlk" ]; then
    PRODUCT_ID="0x0104"
    SERIAL="rpitalk-dectalk-001"
    PRODUCT="RPITalk DECtalk Emulator"
else
    PRODUCT_ID="0x0105"
    SERIAL="rpitalk-litetalk-001"
    PRODUCT="RPITalk LiteTalk Emulator"
fi

# === update gadget.conf ===#
ENABLE=0
test "$Port" = "ttyGS0" && ENABLE=1

sed -i \
    -e "s/^ENABLE_GADGET=.*/ENABLE_GADGET=$ENABLE/" \
    -e "s/^PRODUCT_ID=.*/PRODUCT_ID=$PRODUCT_ID/" \
    -e "s/^SERIAL=.*/SERIAL=\"$SERIAL\"/" \
    -e "s/^PRODUCT=.*/PRODUCT=\"$PRODUCT\"/" \
    "$GadgetConfig"

echo
echo "Configuration updated:"
echo "  emulate = ${Emulator}"
echo "  port    = ${Port}"
echo "  debug   = ${Debug}"
if [ "$ENABLE" = "1" ]; then
    echo "  PRODUCT_ID = ${PRODUCT_ID}"
else
    echo "Gadget mode is disabled"
fi
echo
echo "You may need to restart rpitalk or replug USB for changes to take effect."
# EOF
