#!/bin/sh

Loginu=$1
Date=$2
Mailu=$3
Mot_iasi=$4
Sat=$5
Lati=$6
Long=$7
Indtm=$8
Indjn=$9


cd ${@:10:1} 

conf_file="conf.txt"
while read line
do
        ll=${#line}

        if [[ -n $(echo $line | grep PATH_PROG) ]] ; then
                chem=`expr $ll - 11`
                temp=`echo $line | cut -c11-$ll`
                PATH_PROG=$temp
        fi
        if [[ -n $(echo $line | grep PATH_LOGS) ]] ; then
                chem=`expr $ll - 11`
                temp=`echo $line | cut -c11-$ll`
                PATH_LOGS=$temp
        fi
        if [[ -n $(echo $line | grep PATH_SUN_WEB_SERVICES) ]] ; then
                chem=`expr $ll - 23`
                temp=`echo $line | cut -c23-$ll`
                PATH_SUN_WEB_SERVICES=$temp
        fi
        if [[ -n $(echo $line | grep PATH_Orbiasi_l1b) ]] ; then
                chem=`expr $ll - 18`
                temp=`echo $line | cut -c18-$ll`
                PATH_Orbiasi_l1b=$temp
        fi
	if [[ -n $(echo $line | grep PATH_TEMP) ]] ; then
                chem=`expr $ll - 11`
                temp=`echo $line | cut -c11-$ll`
                PATH_TEMP=$temp
        fi
	if [[ -n $(echo $line | grep PATH_RES) ]] ; then
                chem=`expr $ll - 10`
                temp=`echo $line | cut -c10-$ll`
                PATH_RES=$temp
        fi
	if [[ -n $(echo $line | grep WEB_SERVER) ]] ; then
                chem=`expr $ll - 12`
                temp=`echo $line | cut -c12-$ll`
                WEB_SERVER=$temp
        fi
	




done < $conf_file


echo "PATH_LOGS=$PATH_LOGS"
echo "PATH_PROG=$PATH_PROG"
echo "PATH_Orbiasi_l1b=$PATH_Orbiasi_l1b"
echo "PATH_TEMP=$PATH_TEMP"
echo "PATH_RES=$PATH_RES"
echo "WEB_SERVER=$WEB_SERVER"

DATE=$(date +%Y%m%d-%H%M)

file_logs="$PATH_LOGS/iasi_l1c_user_request_$Loginu_$DATE.txt"

echo "Start user request treatment" >> $file_logs 
echo "Creation of user tempory rep" >> $file_logs 

cd $PATH_PROG
./create_repu -l $Loginu -d $DATE -logs $file_logs
sleep 1
cd $PATH_TEMP/$Loginu/Order_$DATE
echo $Loginu > requete.txt
echo $Mailu >> requete.txt
echo $Date >> requete.txt
echo $Mot_iasi >> requete.txt
echo $Sat >> requete.txt
echo $Lati >> requete.txt
echo $Long >> requete.txt
echo $Indtm >> requete.txt
echo $Indjn >> requete.txt

if [ $Indtm -eq 0 ]; then
	tIndtm="Sea"
fi
if [ $Indtm -eq 1 ]; then
	tIndtm="Land"
fi
if [ $Indtm -eq 2 ];
then
	tIndtm="Land and sea"
fi

if [ $Indjn -eq 0 ]; then
        tIndjn="Night"
fi
if [ $Indjn -eq 1 ]; then
        tIndjn="Day"
fi
if [ $Indjn -eq 2 ]; then
        tIndjn="Night and day"
fi


echo "Copy of user request parameters in $PATH_TEMP/$Loginu/Order_$DATE/request.txt" >> $file_logs 

echo $PATH_TEMP/$Loginu/Order_$DATE >> requete.txt
echo $DATE >> requete.txt

cp requete.txt $PATH_PROG
chmod 755 requete.txt

echo "Request process" >> $file_logs
cp $PATH_PROG/treat_request.sh . 
cp $PATH_PROG/conf.txt . 

./treat_request.sh -f requete.txt -logs $file_logs

var=$?

if [ $var -eq 0 ]; then
	ssh $WEB_SERVER "mkdir $PATH_RES/$Loginu/Order_$DATE ; chmod 755 $PATH_RES/$Loginu/Order_$DATE"
	scp $PATH_TEMP/IASI_mots_all_spots_$Loginu_$DATE* $WEB_SERVER:$PATH_RES/$Loginu/Order_$DATE

        echo "The result of your request is in the repository Order_$DATE in your user space.

Date range : $Date
Channels : $Mot_iasi
Latitude range : $Lati
Longitude range : $Long
Land/sea index : $tIndtm
Night/day index : $tIndjn

Le service Ether" > $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
	$PATH_PROG/mailu $Mailu $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
	/bin/rm $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
	/bin/rm $PATH_TEMP/IASI_mots_all_spots_$Loginu_$DATE*
	/bin/rm -R $PATH_TEMP/$Loginu/Order_$DATE

else
	echo "Your request Order_$DATE has no result. 

Date range : $Date
Channels : $Mot_iasi
Latitude range : $Lati
Longitude range : $Long
Land/sea index : $tIndtm 
Night/day index : $tIndjn

Ether service
" > $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
	$PATH_PROG/mailu $Mailu $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
        /bin/rm $PATH_TEMP/mail_retour_$Loginu_$DATE.txt
fi

echo "var=$var"

exit $var




