#!/bin/bash
# 	Script: download_orbit
#
#
# Authors: Edgar Lenhof, François Beauducel
# Created: 2019/07/31 at OVPF, La Réunion Island
# Updated: 2019/10/03

if [ $# -lt 2 ]; then
	echo "       Syntax: download_orbit DAYS DEST [options]"
	echo "  Description: downloads best avalaible orbit for chosen dates from the JPL secured web server (see https://sideshow.jpl.nasa.gov/pub/JPL_GNSS_Products)"
	echo "  Arguments:"
	echo "           DAYS = number of days to process (from start day)"
	echo "           DEST = directory where orbits shall be saved"
	echo "	Optional arguments:"
	echo "           -o ORBIT = type of orbit (Ultra, Rapid or Final)"
	echo "           -d STARTDAY = days to start retrieving (YYYY/mm/dd)"
        echo "           -r DAYS = remove orbit files that have been last modified DAYS days ago"
	echo "		 -v = verbose mode"
	echo ""
	exit 0;
fi

DAYS=$1
DEST=$2
DAYLIST=$(for d in $(seq $DAYS -1 0);do date -d "$d day ago" +%Y/%m/%d;done)
ORBIT=("Final" "Rapid" "Ultra")
delete="FALSE"
verbose="FALSE"

# Optionnal parameters
for (( i=1; i<=$#; i++)); do

    case ${!i} in
	

	-d)
	k=$((i+1))
	IFS=',' read -ra arrayDay <<< "${!k}"
	declare STARTDAYS=${arrayDay[@]}
	DAYLIST= $()
	for start in $STARTDAYS;do
		declare L=$(for d in $(seq $DAYS -1 0);do date -d "$d day ago $start" +%Y/%m/%d;done)
		DAYLIST=("${DAYLIST[@]}" "${L[@]}")	
	done
	DAYLIST=($(printf "%s\n" "${DAYLIST[@]}" | sort -u | tr '\n' ' '))
	declare DAYLIST=${DAYLIST[@]};; #converting to proper format



	-o)
	k=$((i+1))
	declare ORBIT=( "${!k}" )
	# Check correct orbit input
	if [ "$ORBIT" != "Ultra" ]; then 
		if [ "$ORBIT" != "Rapid" ]; then
			if [ "$ORBIT" != "Final" ]; then
				echo "Error : Please enter orbit as Ultra, Rapid or Final"
				exit 0
			fi
		fi
	fi
	;;

	-r)
	k=$((i+1))
	declare delete="TRUE"
        declare nb_delete=( "${!k}" );;

	-v)
	declare verbose="TRUE"


	esac
done


for day in $DAYLIST; do

	year=${day:0:4}
	month=${day:5:2}
	day=${day:8:2}

	for orbit in "${ORBIT[@]}"; do

		if [[ ! -e "$DEST/$orbit/$year/$year-$month-$day.eo.gz" || "$orbit" == "Ultra" ]]; then

			echo -n "   Downloading orbit $orbit for $day-$month-$year..."



			if [ $verbose == "TRUE" ]; then
				wget -N -P $DEST/$orbit/$year -r -l1 -nd https://sideshow.jpl.nasa.gov/pub/JPL_GNSS_Products/$orbit/$year/ -A $year-$month-$day*
			else
				wget -q -N -P $DEST/$orbit/$year -r -l1 -nd https://sideshow.jpl.nasa.gov/pub/JPL_GNSS_Products/$orbit/$year/ -A $year-$month-$day*
			fi

		
			if [ -e "$DEST/$orbit/$year/$year-$month-$day.eo.gz" ]; then # if download succeed
				echo " OK."
				break
			
			
			else		
				echo " not yet avalaible!"
			fi

		else
			echo "   Orbit $orbit for $day-$month-$year is locally available."
		fi

	done

done


# deleting too ancient orbits
if [ $delete == "TRUE" ]; then

        files=$(find $DEST -type f -mtime +$nb_delete)
        for f in $files; do
                rm $f
                if [ $verbose == "TRUE" ]; then
                        echo "removed $f"
                fi
        done
fi
