#!/bin/bash
#
# Provide the name of a PDR file staged on oceandata.sci.gsfc.nasa.gov 
#
# This script will retreive that PDR, modify it to reflect locations on samoa,
# and pull the files over to samoa
#

PDR=$1
#wget -q --post-data="dtype=misc&addurl=0&results_as_file=1&search=Q2*SOILM*met" -O - http://oceandata.sci.gsfc.nasa.gov/search/file_search.cgi > $LIST

wget -N --timeout=10 --wait=0.5  http://oceandata.sci.gsfc.nasa.gov/Recent/Aquarius/NSIDC/$PDR 
 
if [[ ! -a $PDR ]]; then
    echo "$PDR does not exist!"
    exit 1
fi

sed -f pdr.sed $PDR > /ftp/pub/NSIDC/PDR/$PDR

LIST=/tmp/SOILM_file_list.txt

grep FILE_ID $PDR |awk -F' = ' '{print $2}'|sed -e 's/;//' > $LIST

COUNTER=1
for FILE in $(cat $LIST)
     do
       YEAR=`echo $FILE|cut -c2-5`
       DOY=`echo $FILE|cut -c6-8`
       mkdir -p /ftp/pub/NSIDC/$YEAR/$DOY 
       CHK=$(($COUNTER % 29))
       wget -N --timeout=10 --wait=0.5 http://oceandata.sci.gsfc.nasa.gov/cgi/getfile/${FILE} --server-response -o ${FILE}.headers -P /ftp/pub/NSIDC/$YEAR/$DOY --content-disposition
       STATUS=$?
       echo "Grabbing $FILE..." 
       if [ $CHK -eq 0 ]; then
          if [ $COUNTER -gt 1 ]; then
	      echo "pausing a few seconds to take a breath..."
              sleep 3
          fi
       fi
       if [ $STATUS -ne 0 ]; then

           ERRCODE=`grep "HTTP/" ${FILE}.headers | awk '{print $2}'`

           if [ $ERRCODE -eq 503 ]; then
               while [ $STATUS -gt 0 ] 
               do
	          echo "pausing 10 seconds to take a breath..."
                  sleep 10
                  wget -N --timeout=10 --wait=0.5 http://oceandata.sci.gsfc.nasa.gov/cgi/getfile/${FILE} --limit-rate=1000k --server-response -o ${FILE}.headers -P /ftp/pub/NSIDC/$YEAR/$DOY --content-disposition
                   STATUS=$?
                   if [ $STATUS -ne 0 ]; then
                     ERRCODE=`grep "HTTP/" ${FILE}.headers | awk '{print $2}'`
                     if [ $ERRCODE -ne 503 ]; then
                      STATUS=-1
		     else
                      STATUS=1
		     fi
                   fi
               done 
           fi
       fi
       if [ $STATUS == 0 ]; then
           rm -rf ${FILE}.headers
       fi
       let COUNTER=COUNTER+1
done
