#!/bin/bash

##################################################################################
# Initializing drivers and background processes for PE1RXF-APRS-server           #
#                                                                                #
# (C)2021 M.T. Konstapel https://meezenest.nl/mees                               #
#                                                                                # 
# This file is part of PE1RXF-APRS-server.                                       #
#                                                                                #
#    PE1RXF-APRS-server 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.                                         #
#                                                                                #
#    PE1RXF-APRS-server 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 PE1RXF-APRS-server  If not, see <https://www.gnu.org/licenses/>. #
#                                                                                #
##################################################################################

   ###########################
   # initializing soundmodem #
   ###########################
   echo "Starting direwolf"
   /usr/local/bin/direwolf -t 0 -c /home/marcel/direwolf.conf -p -q hd  > /dev/null 2>&1 &
   #Check if Direwolf is running
   /usr/bin/sleep 5
   if [ -z "`ps ax | grep -v grep | grep direwolf`" ]; then
      echo -e "\nERROR: Direwolf did not start properly and is not running, please review direwolf.conf"
         exit 1
   fi
   echo "Installing one KISS connection on PTY port /tmp/direwolf/kisstnc_aprx"
     mkdir -p /tmp/direwolf
     cp -a /tmp/kisstnc /tmp/direwolf/kisstnc_aprx
     /usr/sbin/mkiss -s 19200 -x 1 /tmp/direwolf/kisstnc_aprx > /tmp/unix98
   #This creates a PTS interface like "/dev/pts/3"
     export PTS0=`more /tmp/unix98 | grep -w /dev | cut -b -11`
     echo "PTS0 device: $PTS0"
     sudo /usr/sbin/kissattach $PTS0 ax0  > /tmp/ax25-config.tmp
     awk '/device/ { print $7 }' /tmp/ax25-config.tmp > /tmp/ax25-config1-tmp
     read Device < /tmp/ax25-config1-tmp

   # Start kissutil to send messages in files written to /home/marcel/ham/aprs_utils/aprs_files_to_transmit
     /usr/local/bin/kissutil -f /home/marcel/ham/aprs_utils/aprs_files_to_transmit > /dev/null 2>&1 &

   # Start filter for received messages to PE1RXF-0
   # Messages addressed to PE1RXF are stored in file /home/marcel/ham/aprs_utils/aprs_received_messages.log
#    tail /var/log/aprx/aprx-rf.log -F | grep --line-buffered "::PE1RXF-10" >> /home/marcel/ham/aprs_utils/aprs_received_messages/received_aprs_messages.log & 
    #tail /var/log/aprx/aprx-rf.log -F --lines=1 | grep --line-buffered "::PE1RXF-1" | /home/marcel/ham/aprs_utils/process_aprs_messages.sh &

    ###########################
    #initialize arduino modem #
    ###########################
    echo "Starting external interface"
    sudo /usr/sbin/kissattach /dev/ttyUSB0 ax1
    sudo /usr/sbin/kissparms -p ax1 -t 500 -s 200 -r 32 -l 100  -f n
    # Start kissutil to send messages in files written to /home/marcel/ham/aprs_utils/aprs_files_to_transmit_channel_B
    # kissutil -f /home/marcel/ham/aprs_utils/aprs_files_to_transmit_channel_B > /dev/null 2>&1 &


    # Start filter for received messages to PE1RXF (all sufixes)
    # Messages addressed to PE1RXF are stored in file /home/marcel/ham/aprs_utils/aprs_received_messages.log
    #tail /var/log/aprx/aprx-rf.log -F --lines=1 | grep --line-buffered "::PE1RXF" | /home/marcel/ham/aprs_utils/process_aprs_messages.sh &
    
    # Test log heard stations
    tail /var/log/aprx/aprx-rf.log -F --lines=1 | /home/marcel/ham/aprs_utils/process_traffic.sh &
    
    # Start mainloop maintenance routine
    /home/marcel/ham/aprs_utils/pe1rxf-aprs-server.sh
