#!/bin/bash
#
# Script to start up direwolf & AX.25 services
# The script enables & starts the services

scriptname="`basename $0`"

USER=
AX25_CFGDIR="/usr/local/etc/ax25"
PORT_CFG_FILE="$AX25_CFGDIR/port.conf"
SERVICE_LIST="direwolf.service ax25dev.path ax25dev.service ax25-mheardd.service ax25d.service"
SYSTEMCTL="systemctl"

function dbgecho { if [ ! -z "$DEBUG" ] ; then echo "$*"; fi }

# ===== function start_service

function start_service() {
    service="$1"
    echo "Starting: $service"

    systemctl is-enabled "$service" > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echo "ENABLING $service"
        $SYSTEMCTL enable "$service"
        if [ "$?" -ne 0 ] ; then
            echo "Problem ENABLING $service"
            exit
        fi
    fi

    $SYSTEMCTL --no-pager start "$service"
    if [ "$?" -ne 0 ] ; then
        echo "Problem starting $service"
        systemctl status $service
        exit
    fi
}

# ===== function get_user

function get_user() {
   # Check if there is only a single user on this system
   if (( `ls /home | wc -l` == 1 )) ; then
      USER=$(ls /home)
   else
      echo -n "Enter user name ($(echo $USERLIST | tr '\n' ' ')), followed by [enter]"
      read -ep ": " USER
   fi
}

# ==== function check_user
# verify user name is legit

function check_user() {
   userok=false
   dbgecho "$scriptname: Verify user name: $USER"
   for username in $USERLIST ; do
      if [ "$USER" = "$username" ] ; then
         userok=true;
      fi
   done

   if [ "$userok" = "false" ] ; then
      echo "User name ($USER) does not exist,  must be one of: $USERLIST"
      exit 1
   fi

   dbgecho "using USER: $USER"
}

# ===== function change_icon
# If an icon is present change it
function change_icon() {

    ax25_desktop_file="/home/$USER/Desktop/ax25-startstop.desktop"

    if [ -e "$ax25_desktop_file" ] ; then
        icon_action="start"
        if [ "$1" == "off" ] ; then
            icon_action="stop"
        fi
        # change icon
        if [ "$(stat -c "%U" $ax25_desktop_file)" != "$USER" ] ; then
            sudo chown $USER:$USER "$ax25_desktop_file"
        fi

        cp /home/$USER/bin/ax25-${icon_action}.desktop "$ax25_desktop_file"
        echo "changed icon to $1"
    fi
}

# ===== main

# Check if direwolf is already running.
pid=$(pidof direwolf)
if [ $? -eq 0 ] ; then
    echo "Direwolf already running with a pid of $pid ... exiting."
    exit 1
fi

# Get list of users with home directories
USERLIST="$(ls /home)"
USERLIST="$(echo $USERLIST | tr '\n' ' ')"

# Check if running as root
if [[ $EUID != 0 ]] ; then
    echo "set sudo"
    SYSTEMCTL="sudo systemctl"
    USER=$(whoami)
else
    get_user
    check_user
fi

# If no port config file found create one
if [ ! -f $PORT_CFG_FILE ] ; then
    echo "No port config file: $PORT_CFG_FILE found, copying from repo."
    sudo cp $HOME/n7nix/ax25/port.conf $PORT_CFG_FILE
fi

ax25_udr0_baud="not configured"
ax25_udr1_baud="not configured"

if [ -e $PORT_CFG_FILE ] ; then
    ax25_udr0_baud=$(sed -n '/\[port0\]/,/\[/p' $PORT_CFG_FILE | grep -i "^speed" | cut -f2 -d'=')
    ax25_udr1_baud=$(sed -n '/\[port1\]/,/\[/p' $PORT_CFG_FILE | grep -i "^speed" | cut -f2 -d'=')
    dbgecho "AX.25: udr0 speed: $ax25_udr0_baud, udr1 speed: $ax25_udr1_baud"
else
    echo "Port config file: $PORT_CFG_FILE NOT found."
fi

echo
echo "STARTING AX.25/Direwolf: port 0: $ax25_udr0_baud & port1: $ax25_udr1_baud baud modems."

for service in `echo ${SERVICE_LIST}` ; do
    start_service $service
done

change_icon "off"
