#!/bin/bash
#
# Uncomment this statement for debug echos
#DEBUG=1

scriptname="`basename $0`"
SPLIT_CHANNEL_FILE="/etc/ax25/split_channel"
DIREWOLF_CFGFILE="/etc/direwolf.conf"

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

# ===== function is_direwolf
# Determine if direwolf is running

function is_direwolf() {
    # ardop will NOT work if direwolf or any other sound card program is running
    pid=$(pidof direwolf)
    retcode="$?"
    return $retcode
}

# ===== function is_pulseaudio
# Determine if pulse audio is running

function is_pulseaudio() {
    pid=$(pidof pulseaudio)
    retcode="$?"
    return $retcode
}

# ===== main

bardopc_running=false
bardop2_running=false

is_direwolf
if [ "$?" -eq 0 ] ; then
    # Direwolf is running, check for split channels
    if [ -e "$SPLIT_CHANNEL_FILE" ] ; then
        # Get 'left' or 'right' channel from direwolf config (last word in ADEVICE string)
        chan_lr=$(grep "^ADEVICE " $DIREWOLF_CFGFILE | grep -oE '[^-]+$')
        echo " == Direwolf is running with pid: $pid, Split channel is enabled, Direwolf controls $chan_lr channel only"
    else
        echo "== Direwolf is running with pid: $pid and controls both channels"
    fi
else
    echo "Direwolf is NOT running"
fi

is_pulseaudio
if [ "$?" -eq 0 ] ; then
    echo "== Pulse Audio is running with pid: $pid"
else
    echo "Pusle Audio is NOT running"
fi

# Check for .asoundrc & asound.conf ALSA configuration files
# Verify virtual sound device ARDOP
cfgfile="/home/$USER/.asoundrc"

if [ ! -e "$cfgfile" ] ; then
    echo "File: $cfgfile does not exist"
else
    grep -i "pcm.ARDOP" $cfgfile > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echo "No ARDOP entry in $cfgfile"
    else
        echo "Found ARDOP entry in $cfgfile"
    fi
fi

# Verify config file to define virtual devices for split channel operation
cfgfile="/etc/asound.conf"

if [ ! -e "$cfgfile" ] ; then
    echo "File: $cfgfile does not exist"
else
    echo "Found file: $cfgfile for split channel operation"
fi


progname="piardopc"
pid=$(pidof $progname)
if [ "$?" -eq 0 ] ; then
    echo "$progname is running with pid: $(pidof $progname)"
    bardopc_running=true
fi

progname="piardop2"
pid=$(pidof $progname)
if [ "$?" -eq 0 ] ; then
    echo "$progname is running with pid: $(pidof $progname)"
    bardop2_running=true
fi

if ! $bardopc_running && ! $bardop2_running ; then
    echo "ardop NOT running"
fi

# Verify programs are installed
PROGLIST="piARDOP_GUI piardop2 piardopc"

dbgecho "Verify required programs"
# Verify required programs are installed

for prog_name in `echo ${PROGLIST}` ; do
   type -P $prog_name &> /dev/null
   retcode="$?"
   if [ "$retcode" -ne 0 ] ; then
      echo "$scriptname: Need to Install $prog_name"
         NEEDPKG_FLAG=true
   else
       # Get last word of filename, break on under bar, only look at first 3 characters
       lastword=$(grep -oE '[^_]+$' <<< $prog_name | cut -c1-3)
       if [ "$lastword" != "GUI" ] ; then
           echo "Found program: $prog_name, $($prog_name -h | head -n 1)"
       else
           echo "Found program: $prog_name"
       fi
   fi
done

prog_name="arim"
type -P $prog_name &> /dev/null
if [ $? -ne 0 ] ; then
   echo "$scriptname: Need to Install $prog_name"
      NEEDPKG_FLAG=true
else
    echo "Found program: $prog_name, version: $($prog_name -v | head -n 1)"
fi

