#!/bin/bash


# Run fetchA201.sh every month

month=`date +%m`
year=`date +%Y`

#month=$1
#ear=$2

if [[ 10#$month -eq 1 ]]
then
  year=$((year-1))
  month=12
else
  month=$((10#$month-1))
  if [[ $month -lt 10 ]]
  then
    month='0'$month
  fi
fi


sdate="01/$month/$year"
if [[ 10#$month -eq 1 || 10#$month -eq 3 || 10#$month -eq 5 || 10#$month -eq 7 || 10#$month -eq 8 || 10#$month -eq 10 || 10#$month -eq 12 ]]
then 
  edate="31/$month/$year"
else
  if [[ 10#$month -ne 2 ]]
  then
    edate="30/$month/$year"
  else
    d4=$((year%4))
    if [[ $d4 -ne 0 ]]
    then 
      edate="28/02/$year"
    else
      d100=$((year%100))
      if [[ $d100 -ne 0 ]]
      then
        edate="29/02/$year"
      else
	d400=$((year%400))
	if [[ $d400 -eq 0 ]]
	then 
          edate="29/02/$year"
	else
          edate="28/02/$year"
	fi
      fi
    fi
  fi
fi

fname=belem_"$year"_"$month".csv

/gea/data/towers/inmet-belem/fetchA201.sh $sdate $edate $fname



  

