#!/bin/bash
#A script to help locate winlink gateways
#ARDOP files must be download first using getardoplist
#getardoplist should be a daily cron job
#so the lists stay current. 
#See notes at top of getardoplist
#This script is provided AS IS
#Feel free to mod to suit your needs
#20181217 km4ack
# 20191023 KD5PQJ changed to shared folder on desktop/HAM/ardop-list/
######## USER CONFIG ####################

#Path to ARDOP files.
#Path below and path in getardoplist MUST MATCH
MYPATH=~/Desktop/HAM/ardop-list/

#Path for getardoplist script
SCRIPT_PATH=/usr/local/bin/getardoplist

#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'ardoplist.txt' ]; then
DL20=$(head -n 1 $MYPATH'20mardoplist.txt')
DL30=$(head -n 1 $MYPATH'30mardoplist.txt')
DL40=$(head -n 1 $MYPATH'40mardoplist.txt')
DL80=$(head -n 1 $MYPATH'80mardoplist.txt')
fi
clear
#Check that files have been downloaded before
if      [ -f $MYPATH'ardoplist.txt' ]; then
        echo
else
	echo
	echo
	echo
	echo
	printf "\033[1;31mPLEASE download files before proceeding\033[0m\n"
	printf "\033[1;31mType getardoplist 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) 40M'
echo '3) 80M'
echo '4) 30M'
echo '5) 20M'
echo '6) 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 & 6\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 ardop 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 getardoplist script
		echo
		echo "Done"
		echo
		#get new download times
		DL20=$(head -n 1 $MYPATH'20mardoplist.txt')
		DL30=$(head -n 1 $MYPATH'30mardoplist.txt')
		DL40=$(head -n 1 $MYPATH'40mardoplist.txt')
		DL80=$(head -n 1 $MYPATH'80mardoplist.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 40M 30M 20M 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 & 6\033[0m\n"
fi


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

#Find and return results of grid search 20M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '20M' ]
	then
        FILE=$MYPATH'20mardoplist.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'30mardoplist.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'40mardoplist.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 80M
if [[ -z "$name" ]]; then
   echo
else
	if [ $name == '80M' ]
	then
	FILE=$MYPATH'80mardoplist.txt'
	clear
	echo
	echo
	echo $DL80
	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
