#!/bin/bash
# Bash script to print International Beacon Project beacons as they occur.
# You will need to set the PC clock accurately or lock to NTP.
# License: Free to use and modify but please retain the credit.

# Get the command line parameter and enforce a default value of 0
band=${1:-0}

echo "International Beacon Project announcement utility"
echo "V1.11 By Tim Howe G0ETP, May-Dec 2014"
echo
echo "Comand line parameter 1 is the band offset:"
echo "0=14.100 1=18.110 2=21.150 3=24.930 4=28.200"
echo
echo "Using band offset $band"
echo

# List of beacon callsigns and information
# The trailing tabs are there so that the 'next' column lines up
# Feel free to edit these strings as required (e.g. to add an indication
# of current beacon status)
callsign[0]="4U1UN\tUnited Nations\t"
callsign[1]="VE8AT\tCanada\t\t"
callsign[2]="W6WX\tUSA\t\t"
callsign[3]="KH6WO\tHawaii\t\t"
callsign[4]="ZL6B\tNew Zealand\t"
callsign[5]="VK6RBP\tAustralia\t"
callsign[6]="JA2IGY\tJapan\t\t"
callsign[7]="RR9O\tRussia\t\t"
callsign[8]="VR2B\tHong Kong\t"
callsign[9]="4S7B\tSri Lanka\t"
callsign[10]="ZS6DN\tSouth Africa\t"
callsign[11]="5Z4B\tKenya\t\t"
callsign[12]="4X6TU\tIsrael\t\t"
callsign[13]="OH2B\tFinland\t\t"
callsign[14]="CS3B\tMadeira\t\t"
callsign[15]="LU4AA\tArgentina\t"
callsign[16]="OA4B\tPeru\t\t"
callsign[17]="YV5B\tVenezuela\t"


while true
do

	# Get the date and split it into separate fields
    # (Patched 2014-12-07 by SM0RWO; function was dependent on local system date format)
    now=(`date +"%M %S"`)

	# Numbers with leading 0 are considered as octal and cause problems
	# Suppress any leading zero using parameter expansion:
	min=${now[0]#0}
	sec=${now[1]#0}

	# Check for 10 sec boundaries (beacon start times)
	if [ $((sec % 10)) -eq 0 ]
	then
		# Calculate a 1-dimensional 'beacon id' based on the minute and 10 sec interval
		min_mod=$((min % 3))
		sec_div=$((sec / 10))
		b_id=$((sec_div + min_mod * 6))

		# Apply the band offset and re-apply modulo
		b_id_shifted=$(( (b_id + 18 - band) % 18))
		b_id_shifted_next=$(( (b_id + 19 - band) % 18))
		#echo $min_mod : $sec_div : $b_id : $b_id_shifted

		#echo -e ${callsign[$b_id_shifted]}
		echo -e ${callsign[$b_id_shifted]} "Next: "${callsign[$b_id_shifted_next]}

		sleep 9.6
	fi

	sleep 0.1
done


