#!/bin/bash
# console-config/src/sbin-scripts/config
#
#  Copyright: ©2014, Güralp Systems Ltd.
#  Author: Kelly Dunlop <kdunlop@guralp.com>
#  License: GPLv3

#
# configure - Allow console configuring of the system.  This detects whether
#             to call gconfig or a section of the console-config package
#             depending on what the parameters are.
#             We can add to this as more items get added into console-config.
#

DIALOG_RESULTFILE="`mktemp`"
trap "rm -f \"${DIALOG_RESULTFILE}\"" EXIT



#
# Menu definition
#

# Don't overwhelm the user by putting everything on the front page
MENULIST=(
            "hostname"  "Station/host name" \
            "net"       "Network devices and IP addresses" \
            "timing"    "Digitiser timing" \
            "rec"       "Storage and data recording" \
            "serial"    "Serial port functions" \
            "svc"       "Data conversion and transmission services" \
            "other"     "Other settings"
         )

# All the less-important stuff ends up here
MENULIST2=(
            "dirclean"  "Directory cleaner tasks" \
            "mail"      "Email parameters" \
            "gpio"      "GPIO labels and power switch settings" \
            "ioassert"  "Status I/O line assertion" \
            "old"       "Old configuration interface" \
          )

usage() {
    cat <<EOF
Usage:

    $0
        displays configuration section dialog

    $0 "item"
        jumps straight to configuration of the given item

Items that can be configured:
EOF

    nl=0
    for I in "${MENULIST[@]}" "${MENULIST2[@]}"
    do
        if [ "${nl}" -eq 0 ]
        then
            nl=1
            printf "    %-10s - " "${I}"
        else
            nl=0
            echo "${I}"
        fi
    done
}



#
# Deal with commandline parameters
#
case "$#" in
0)
    # No parameters
    dialog --title "Configure system" \
           --menu "Select an item to configure" \
           0 60 0 \
           "${MENULIST[@]}" 2> "${DIALOG_RESULTFILE}"
    if [ $? -ne 0 ]
    then
        clear
        echo "Cancelled"
        exit 1
    fi

    option="`cat "${DIALOG_RESULTFILE}"`"
    ;;

1)
    option="$1"
    ;;

*)
    echo "$0: expecting 0 or 1 parameter only. See --help" >&2
    exit 1
esac



#
# Deal with selected option
#
#  process_option() return values:
#   0 - call again
#   1 - unrecognised option
process_option() {
    case "$1" in
    "hostname")
        exec /usr/sbin/hostname-setup
        ;;

    "net")
        exec /usr/sbin/net-setup
        ;;

    "timing")
        exec /usr/sbin/timing-setup
        ;;

    "rec")
        exec /usr/sbin/gconfig "rec-top"
        ;;

    "serial")
        exec /usr/sbin/serial-port-setup
        ;;

    "svc")
        exec /usr/sbin/gconfig "servicestop"
        ;;

    "other")
        dialog --title "Configure system" \
               --menu "Select an item to configure" \
               0 60 0 \
               "${MENULIST2[@]}" 2> "${DIALOG_RESULTFILE}"
        if [ $? -ne 0 ]
        then
            clear
            echo "Cancelled"
            exit 1
        fi

        option="`cat "${DIALOG_RESULTFILE}"`"
        return 0
        ;;

    "dirclean")
        exec /usr/sbin/gconfig "dirclean"
        ;;

    "mail")
        exec /usr/sbin/gconfig "mta"
        ;;

    "gpio")
        exec /usr/sbin/gconfig "misc-ioline"
        ;;

    "ioassert")
        exec /usr/sbin/gconfig "statustop"
        ;;

    "old")
        exec /usr/sbin/gconfig
        ;;

    "--help" | "-h" | "help")
        usage
        exit 0
        ;;

    esac

    return 1
}

while process_option "${option}"
do
    option="`cat "${DIALOG_RESULTFILE}"`"
done

echo "$0: unrecognised option \"${option}\". See --help" >& 2
exit 1

# vim: ts=4:sw=4:expandtab
