#!/bin/bash

# /usr/local/sbin/update-ham-details.sh
# 07/11/13 - Added TNC-PI params tools
# 06/30/13 - Original version
#
# NOTE: Can localize all this via the $LANG=en_US.UTF-8 variable 


if [ $UID -ne 0 ]; then
  echo -e "\nError: you must be root to run this command"
  exit 1
fi

#Clean out any old data
CALLSIGN=""
AMPRIP=""
UPPERCALL=""
LOWERCALL=""
I=""

echo -en "\nPlease enter in your callsign: "
read CALLSIGN

echo -en "\nPlease enter in your AMPR IP [hit enter if you don't have one]: "
read AMPRIP

if [ -z $AMPRIP ]; then
   #No AMPR address assigned, let's assign one from the AMPR test subnet for now"
   AMPRIP="44.128.10.10"
fi

echo -e "\n$CALLSIGN, are you ready to customize your RPi with this new information?"
echo -e "\n   Callsign: $CALLSIGN         (case doesn't matter)"
echo -e "   AMPR IP:  $AMPRIP     (AMPR test IP will be used if one not provided)"
echo -e "\nPress any key to continue or control-c to abort"
read ANYKEY

UPPERCALL="`echo $CALLSIGN | tr '[:lower:]' '[:upper:]'`"
LOWERCALL="`echo $CALLSIGN | tr '[:upper:]' '[:lower:]'`"

PATHS="/etc/ax25 /usr/local/bin /spider/connect"

echo -e "\nNOTE: some 'sed: no input files' warnings are expected\n"

for I in $PATHS; do
  echo "    processing $I:"
  cd $I
  grep -rl F6GAL * | xargs sed -i "s/F6GAL/$UPPERCALL/g"
  grep -rl f6gal * | xargs sed -i "s/f6gal/$LOWERCALL/g"
  grep -rl 44.151.94.1 * | xargs -I '{}' sed -i "s/44.151.94.1/$AMPRIP/g" {}
done

echo -e "Fetching TNC-Pi tools into /usr/local/src"
mkdir -p /usr/local/src/tnc-pi/i2ckiss
mkdir /usr/local/src/tnc-pi/parms
cd /usr/local/src/tnc-pi/i2ckiss
wget www.tnc-x.com/i2ckiss.zip
cd ../parms
wget www.tnc-x.com/parms.zip

echo -e "\nAutomated customization complete but dont forget to:\n"
echo -e "    - Update your required serial ports in /etc/ax25/ax25-up - currently uses /dev/ttyAMA0"
echo -e "    - update your GPS coordinates in /etc/ax25/aprslist.conf"
echo -e "    - update private LAN IP addresses in /etc/ax25/ax25ipd.conf"
echo -e "    - update the heirarchical addressing,QRA, and other items in /etc/ax25/fbb.conf"
echo -e "    - update your location, password, etc in /etc/ax25/fpac.conf"
echo -e "    - update your city and grid square in /etc/ax25/fpac.info"

echo -e "\nDone\n"

