#!/bin/bash
#A script to help locate winlink gateways
#PACKET files must be download first using getpacketlist
#getpacketlist should be a daily cron job
#so the lists stay current. 
#See notes at top of getpacketlist
#This script is provided AS IS
#Feel free to mod to suit your needs
#20181217 km4ack
# moded by KD5PQJ for packet 20191023

######## USER CONFIG ####################

#Path to packet files.
#Path below and path in getpacketlist MUST MATCH
MYPATH=~/Desktop/HAM/packet-list/

#Path for getPACKETlist script
SCRIPT_PATH=/usr/local/bin/getpacketlist

#path for map pdf
MAP_PATH=~/Desktop/HAM/grid-map.pdf

#Select the pdf app on your system
#default is evince
#you can also use xpdf if you have it installed
#PDF_APP=xpdf
PDF_APP='evince -w'

######### END USER CONFIG ################


#Read date of last download in each file
if	[ -f $MYPATH'packetlist.txt' ]; then
DL80=$(head -n 1 $MYPATH'80mpacketlist.txt')
DL40=$(head -n 1 $MYPATH'40mpacketlist.txt')
DL30=$(head -n 1 $MYPATH'30mpacketlist.txt')
DL20=$(head -n 1 $MYPATH'20mpacketlist.txt')
DL10=$(head -n 1 $MYPATH'10mpacketlist.txt')
DL06=$(head -n 1 $MYPATH'6mpacketlist.txt')
DL02=$(head -n 1 $MYPATH'2mpacketlist.txt')
DL140=$(head -n 1 $MYPATH'140cmpacketlist.txt')
DL70=$(head -n 1 $MYPATH'70cmpacketlist.txt')
fi
clear
#Check that files have been downloaded before
if      [ -f $MYPATH'packetlist.txt' ]; then
        echo
else
	echo
	echo
	echo
	echo
	printf "\033[1;31mPLEASE download files before proceeding\033[0m\n"
	printf "\033[1;31mType getpacketlist to download the files\033[0m\n"
	echo
	echo 
	exit
fi


#Header for results
print_header () {
echo 'Call        Grid   Dist    Azmuth               Dial Freq      Center Freq  Shortcut'
echo '======================================================================================================='
}

#menu to print at bottom of results page
print_menu2 () {
echo
echo 'Choose which band you wish to use'
echo
echo '1) Change Grid'
echo '2) 80M'
echo '3) 60M'
echo '4) 40M'
echo '5) 30M'
echo '6) 20M'
echo '7) 10M'
echo '8) 6M'
echo '9) 2M'
echo '10) 140cM'
echo '11) 70cM'
echo '12) Quit'
echo
}

#Begin First Menu
echo 
echo
echo
printf "\033[1;31mFor Best View, Maximize This Screen\033[0m\n"
echo
echo Set the grid to be searched. Refer to the map if needed.
echo
firstnames='Set-Grid View-Map Download-List Quit'
PS3='Enter an Option Number: '
select firstname in $firstnames
do

if [[ -z "$firstname" ]]; then
   echo 
   printf "\033[1;31mPLEASE SELECT A NUMBER BETWEEN 1 & 12\033[0m\n"
fi

if [[ -z "$firstname" ]]; then
   echo
else
	if [ $firstname == 'Quit' ]
	then
	exit
	fi
fi

if [[ -z "$firstname" ]]; then
   echo
else
	if [ $firstname == 'Set-Grid' ]
	then
	echo
	echo 
	read -p 'Please enter the first two letters of the grid to be searched: ' lett
	clear
		if [[ -z "$lett" ]]; then
		echo
		echo "YOU SHOULD SET THE GRID"
		echo "OR THE SEARCH WON'T WORK"
		fi
	echo
	printf "\033[1;31mGrid to be searched is $lett\033[0m\n"
	echo
	break
	fi
fi

if [[ -z "$firstname" ]]; then
   echo 
else
	if [ $firstname == 'View-Map' ]
	then
	$PDF_APP $MAP_PATH
	fi
fi
#############################################################
if [[ -z "$firstname" ]]; then
	echo
else
	if [ $firstname == 'Download-List' ] #Downloads new PACKET list
	then
	echo "Please wait while we check for an internet connection"
	echo "This may take up to 30 seconds"
	wget -q --tries=5 --timeout=20 --spider http://google.com #checks internet connection
		if [[ $? -eq 0 ]]; then #0 indicates internet connection
		echo
		echo "Connected"
		echo
		echo "Please wait....."
		echo "This may take several minutes to complete"
		echo "Depending on your internet speed"
		OUTPUT=$("$SCRIPT_PATH") #Calls getPACKETlist script
		echo
		echo "Done"
		echo
		#get new download times
		DL80=$(head -n 1 $MYPATH'80mpacketlist.txt')
		DL60=$(head -n 1 $MYPATH'60mpacketlist.txt')
		DL40=$(head -n 1 $MYPATH'40mpacketlist.txt')
		DL30=$(head -n 1 $MYPATH'30mpacketlist.txt')
		DL20=$(head -n 1 $MYPATH'20mpacketlist.txt')
		DL10=$(head -n 1 $MYPATH'10mpacketlist.txt')
		DL06=$(head -n 1 $MYPATH'6mpacketlist.txt')
		DL02=$(head -n 1 $MYPATH'2mpacketlist.txt')
		DL140=$(head -n 1 $MYPATH'140cmpacketlist.txt')
		DL70=$(head -n 1 $MYPATH'70cmpacketlist.txt')
		else
		echo
		echo "YOU ARE NOT ONLINE"
		echo "PLEASE CONNECT AND TRY AGAIN"
		echo
		fi
	fi
fi
done
#Begin Second Menu
echo
echo Now choose the band you wish to use
echo
names='Change-Grid 80M 60M 40M 30M 20M 10M 6M 2M 140cM 70cM Quit'

PS3='Enter an Option Number: '

select name in $names
do

if [[ -z "$name" ]]; then
   echo 
   printf "\033[1;31mPLEASE SELECT A NUMBER BETWEEN 1 & 12\033[0m\n"
fi


if [[ -z "$name" ]]; then
   echo
else
	if [ $name == 'Quit' ]
	then
	break
	fi
fi

#Find and return results of grid search 2M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '2M' ]
	then
        FILE=$MYPATH'2mpacketlist.txt'
        clear
        echo
        echo
	echo $DL02
        print_header
        cat $FILE | egrep -i $lett[[:digit:]]{2}
        print_menu2
	fi
fi
#Find and return results of grid search 6M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '6M' ]
	then
        FILE=$MYPATH'6mpacketlist.txt'
        clear
        echo
        echo
	echo $DL06
        print_header
        cat $FILE | egrep -i $lett[[:digit:]]{2}
        print_menu2
	fi
fi
#Find and return results of grid search 10M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '10M' ]
	then
	FILE=$MYPATH'10mpacketlist.txt'
	clear
	echo
	echo
	echo $DL10
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 20M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '20M' ]
	then
	FILE=$MYPATH'20mpacketlist.txt'
	clear
	echo
	echo
	echo $DL20
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 30M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '30M' ]
	then
	FILE=$MYPATH'30mpacketlist.txt'
	clear
	echo
	echo
	echo $DL30
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 40M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '40M' ]
	then
	FILE=$MYPATH'40mpacketlist.txt'
	clear
	echo
	echo
	echo $DL40
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 60M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '60M' ]
	then
	FILE=$MYPATH'60mpacketlist.txt'
	clear
	echo
	echo
	echo $DL60
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 80M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '80M' ]
	then
	FILE=$MYPATH'80mpacketlist.txt'
	clear
	echo
	echo
	echo $DL80
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
	print_menu2
	fi
fi
#Find and return results of grid search 70cM 440
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '70cM' ]
	then
	FILE=$MYPATH'70cmpacketlist.txt'
	clear
	echo
	echo
	echo $DL70
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
    	print_menu2
	fi
fi
#Find and return results of grid search 140cM 220
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '140cM' ]
	then
	FILE=$MYPATH'140cmpacketlist.txt'
	clear
	echo
	echo
	echo $DL140
	print_header
	cat $FILE | egrep -i $lett[[:digit:]]{2}
    	print_menu2
	fi
fi
#Give option to change the grid being searched
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == 'Change-Grid' ]
	then
	read -p 'Enter the grid to be searched: ' lett
	clear
	if [[ -z "$lett" ]]; then
        echo
        echo "YOU SHOULD SET THE GRID"
        echo "OR THE SEARCH WON'T WORK"
        fi
	echo
	printf "\033[1;31mGrid to be searched is $lett\033[0m\n"
	echo
	print_menu2
	fi
fi


done
echo Bye
