#!/bin/bash

#
# File .......... swuk_terminal
# Author ........ Steve Haywood
# Website ....... http://www.spacewire.co.uk
# Project ....... Common (SpaceWire UK Tutorial)
# Date .......... 25 Jun 2025
# Version ....... 1.0
# Description ...
#   Launch the Minicom terminal emulator.
#

# Strict
#set -euo pipefail


################################################################################
# Main function.
# Arguments ...... ${@}
# Return ......... None
# Exit code ...... Status (0=success, 1=failure)
# Shared (In) .... None
# Shared (Out) ... None
main()
{
  # Declare constants
  local -r  c_opthelp="--help"       # Str: Help option name
  local -Ar c_options=(              # ARR: Options & associated help information
    [${c_opthelp}]="Display this help and exit."
  )
  local -ar c_optorder=(             # Arr: Help options display order
    ${c_opthelp}
  )
  local -ar argv=(${@})              # Arr: Get argument values (space-separated) into array
  local -r  c_device="/dev/ttyACM0"  # Str: Device
  local -r  c_rate="115200"          # Str: Baud rate

  # Declare variables
  local     arg                      # Str: Current argument from argv array
  local     option                   # Str: Current option from c_optorder array

  # Display help information
  if [[ " ${argv[*]} " =~ " ${c_opthelp} " ]]; then
    echo "Usage: $(basename ${0}) [OPTION]..."
    echo "Launch a terminal emulator to allow communication with a remote system via USB."
    echo
    for option in ${c_optorder[@]}
    do
      echo "      ${option} $(printf ' %.0s' {1..12} | head -c $((12-${#option}))) ${c_options[${option}]}"
    done
    echo
    exit 0
  fi

  # Get & check the arguments
  for arg in ${argv[@]}; do
    if [[ ${arg:0:2} == "--" ]]; then  # Option
      [[ ! -v c_options[${arg}] ]] &&
        echo "Option (${arg}) is not recognised!" >&2 &&
        exit 1
    else
      echo "Too many arguments found, expecting 0!" >&2
      exit 1
    fi
  done

  # Check for XFCE4 desktop environment
  xfce4-about --version > /dev/null 2>&1
  if [ ${?} -eq 0 ]; then
    # Launch termnal emulator in seperate window
    xfce4-terminal \
      --geometry 80x24 \
      --title "Debug console" \
      --working-directory="${swuk_tutorial}" \
      --command " \
        bash -c 'minicom -D ${c_device} -b ${c_rate} 2> /dev/null; \
        [ \${?} -ne 0 ] && \
          echo \"Terminal emulator failed to launch (${c_device} @ ${c_rate}), check connection!\" >&2; \
        bash \
      '"
  else
    # Launch termnal emulator in same window
    minicom -D ${c_device} -b ${c_rate} 2> /dev/null
    [ ${?} -ne 0 ] &&
      echo "Terminal emulator failed to launch (${c_device} @ ${c_rate}), check connection!" >&2 &&
      exit 1
  fi
}


################################################################################
# Opening gambit.
# Arguments ...... ${@}
# Return ......... None
# Exit code ...... Status (0=success, 1=failure)
# Shared (In) .... None
# Shared (Out) ... None
main ${@}
exit 0
