


webroot="http://www.inmet.gov.br/sonabra/"
webaddr=$webroot"pg_dspDadosCodigo_sim.php?QTIwMQ=="

#######################
# FIRST CALL - Returns COOKIE information and secret keys to make the request

#echo FIRS CALL %%%%%%%%%%%%%%%%%%%%5
#echo curl -c cookiejar $webaddr -o data1.html
curl -c cookiejar $webaddr -o data1.html

# Scan data for keys
h1='aleaValue'
h2='xaleaValue'
h3='xID'

v1='dtaini'   # this will be SET as start date
v2='dtafim'   # this will be SET as end date
v3='aleaNum'

xh1=`cat data1.html | sed "/\"$h1/p;d" | sed "s/.*value=\"\(.*\)\">/\1/;"`
xh2=`cat data1.html | sed "/\"$h2/p;d" | sed "s/.*value=\"\(.*\)\">/\1/;"`
xh3=`cat data1.html | sed "/\"$h3/p;d" | sed "s/.*value=\"\(.*\)\">/\1/;"`

echo $xh1 
echo $xh2 
echo $xh3

#20200110084916NDkxNg==
#123456789A****XXXXXXXX
#                  123456789A
code=`echo $xh2 | sed "s/..........\(....\).*/\1/"`
echo $code



x1="01/01/2020"
x2="10/01/2020"
x3=$code

###################
# SECOND CALL  - 
# This is a POST command with all the requested information and date.
# Also passes cookie information.
# This sets the server to produce the requested data


# POST Command

postcmd="$h1=$xh1&$h2=$xh2&$h3=$xh3&$v1=$x1&$v2=$x2&$v3=$x3"
echo $postcmd

echo SECOND CALL %%%%%%%%%%%%%%%%%%%
echo curl -c cookiejar -d $postcmd $webaddr -o data2.html
curl -b cookiejar -d $postcmd $webaddr -o data2.html


###################3
# THIRD CALL 
# Request CSV data
# Just need to pass the cookie information to the correct webpage/phpscript.

#h4='remove'
#h5='codEst' 
#xh4=`cat data2.html | sed "/\"$h4/p;d" | sed "s/.*value=\"\(.*\)\">/\1/;"`
#xh5=`cat data2.html | sed "/\"$h5/p;d" | sed "s/.*value=\"\(.*\)\">/\1/;"`


#v4='Submit'
#v5='Submit2'
#
#x5='Download de Dados'
#x5a='Download%20de%20Dados'

#echo POST COMMAND
#postcmd2="$h4=$xh4&$h5=$xh5&$v1=$x1&$v2=$x2&$v5=$x5a"
#echo $postcmd2


#### DOWNLOAD DATA - Request details are saved on the sarver and the cookie PHPSESID saved the session id.
echo curl -c cookiejar $webroot"pg_downDadosCodigo_sim.php" -o data3.html
curl -b cookiejar $webroot"pg_downDadosCodigo_sim.php" -o data3.html

exit

