# Script for the K9AN WSPR decoder, by DJ0ABR
# ===========================================
# use the k9an decoder to get the spots out of the wav sound file
# the file 'spots' is used for storing all the spots and debug information
# the file wsprd.out contains the current spots in the format for wsprnet.org
# wsprdsum.out is used as a temporary storage if the upload fails

echo decoding >> spots
./k9an-wsprd -f 0.4742 /home/bananapi/wsprcan-master/wav/wspr_${1}.wav >>spots

# the wav file is processed, delete it (it should be in a ram disk folder if an SD card is used !)
rm /home/bananapi/wsprcan-master/wav/wspr_${1}.wav

# check if spots are available
FILESIZE=$(stat -c%s "/home/bananapi/wsprcan-master/wsprd.out")
echo data size= $FILESIZE >> spots
if [ $FILESIZE -ne 0 ] ; then

        # add the spots to a temporary file used for uploading to wsprnet.org
        echo add to wsprdsum.out >> spots
        cat /home/bananapi/wsprcan-master/wsprd.out >> /home/bananapi/wsprcan-master/wsprdsum.out

        # upload the spots
        echo upload by curl >> spots
        # ping helps curl to contact the DNS server under various conditions, i.e. if the internet connection was lost
        ping -W 2 -c 1 wsprnet.org > /dev/null;
        curl -m 8 -F allmept=@/home/bananapi/wsprcan-master/wsprdsum.out -F call=DJ0ABR -F grid=JN68NT http://wsprnet.org/meptspots.php > /dev/null;
        RESULT=$?

        # check if curl uploaded the data successfully
        # delete only if uploaded
        if [ $RESULT -eq 0 ] ; then
                # data uploaded, delete them
                echo Upload OK, deleting >> spots
                rm wsprdsum.out
        fi
        echo curl result: $RESULT , done. >> spots
fi
