#!/bin/bash

# This script reads NMEA sentances from a GPS device on the serial port and 
# filters out all but the sentence "GPGGA" and the 3rd, 5th, 8th and 9th
# fields which correspond to Lat, Lon, number of satellites, and altitude

# set serial port to 4800 baud rate
stty -F/dev/ttyS0 4800

count=0

# begin infinate loop to read the data

while true; do

count=$(( $count + 1 ))

#Read GPS data from serial port and throw away lines that don't begin with GPGGA
GPGGAstring="`head -n26 /dev/ttyS0 | grep GPGGA`" 

# Pick out individual numbers for Time, Lat, Long, Satellites, and Altitude
GPStimeStamp="`echo $GPGGAstring | cut -d"," -f2`"
Latitude="`echo $GPGGAstring | cut -d"," -f3`"
Longitude="`echo $GPGGAstring | cut -d"," -f5`"
NumberOfSatellites="`echo $GPGGAstring | cut -d"," -f8`"
Altitude="`echo $GPGGAstring | cut -d"," -f9`"

# Trim Lat and Long to a charactor size that is compatible with APRS servers
Latitude=$(expr substr "${Latitude}" 1 7)
Longitude=$(expr substr "${Longitude}" 1 8)

# Add the North and West to the end of the numbers
Latitude=`echo $Latitude"N"`
Longitude=`echo $Longitude"W"`

# Print data to screen
echo "Count = " $count
date
echo "Latitude = " $Latitude
echo "Longitude = " $Longitude
echo "Altitude = " $Altitude
echo "Satellites = " $NumberOfSatellites
echo $GPGGAstring  


echo "$((5 * 328))" | sed 's/..$//'
feet=$(echo $Altitude*$conversion | bc)
decpos=`expr index "$feet" .`
let "wholesize = $decpos - 1"
feet=$(expr substr "${feet}" 1 $wholesize)
echo "feet = " $feet


echo
echo

if [ $count -gt 30 ]  ; then 

############### APRS internet reporting script ########################

count=0

echo
echo "REPORT POSITION TO APRS SERVERS ON THE INTERNET!"
echo

OBJ_NAME="KE5AKC   "		# MUST be 9 character long
SYMBOL="k"
NODE_STATUS_CHAR="/"        # / = primary symbol table   \ = alternate symbol table
COMMENT="  Satellites in view = $NumberOfSatellites  ;  Altitude = $Altitude Meters  "  # charactors 46-80
LAT=$Latitude
LONG=$Longitude
APRS_CALL=KE5AKC
APRS_PASS=12345
TIMESTAMP=`date -u +%d%H%M`z		# Format = DDHHMMz
OBJECT=";${OBJ_NAME}*${TIMESTAMP}${LAT}${NODE_STATUS_CHAR}${LONG}${SYMBOL}${COMMENT}"
BTEXT="$APRS_CALL>APVR30:${OBJECT}"
LOGIN="User $APRS_CALL pass $APRS_PASS vers IRLP-interface 1"
NC=/usr/bin/nc
echo -e "${LOGIN}\n${BTEXT}" | $NC -w 10 rotate.aprs2.net 14580    # &>/dev/null


fi

done
