#!/bin/bash

###################################################################################

#arrays of blanks/hyphens/plus          

i=1 ; tmp1="" ; tmp2="" ; tmp3=""
while [ $i -le 120 ] ; do
  tmp1="$tmp1"' '
  blank[$i]="$tmp1"
  tmp2="$tmp2"'-'
  hyphen[$i]="$tmp2"
  tmp3="$tmp3"'+'
  plus[$i]="$tmp3"
  i=$(( $i + 1 ))
done

###################################################################################

#titles and errors functions            

it1=0 && it2=0

          #$1 = error message to display        
          #$2 = error extension for the msg file
DAMNED () { echo ; echo '@&?%! '"${1}" ; echo "*${0#$MARbin/} aborted*" ;
            [ ${#2} -gt 0 ] && [ -f $msg ] && mv -f $msg $msg.?$2 || touch $msg.?$2 ;}

ERROR ()  { echo '@&?%! '"${1} (y/n) (n=abort)" ;
            read answer ; abort="*${0#$MARbin/} aborted*" ;
            case $answer in (y) echo ;; (n|*) echo "$abort" && echo && exit ;;
            esac ; }

CHOICE () { echo ; echo "${1} (y/n)" ; answer=o ; read answer ;
            case $answer in ([!yn]) DAMNED "wrong answer!" ;; esac ; }

TITLE0 () { echo ; echo "${plus[80]}" ;
            echo "${plus[39-${#1}/2]} ${1} ${plus[39-${#1}/2]}" ;
            echo "${plus[80]}" ; echo ; }

TITLE1 () { it1=$(( $it1 + 1 )) ; it2=0 ;
            echo ; echo "- $it1 - ${1} ${hyphen[73-${#1}]}" ; 
}
TITLE2 () { it2=$(( $it2 + 1 )) ;
            echo ; echo "- $it1.$it2 - ${1} ${hyphen[31-${#1}]}" ; echo ; }

REMARK () { echo "-> ${1}..." ; }

case $cluster in
(nic|idris|foehn|ceci|nasa|froggy|obelix)
  END_EXIT () { exit ; } ;;
(linux)
  if [ $job_pbs == "n" ] ; then
  END_EXIT () { mv -f  $CMDfile.log  $msg.eo ; exit ; }
  else
  END_EXIT () { 
    [ -f $HOME/${PBS_JOBID%%\.*}.`hostname`.ER ] &&  \
    File=$HOME/${PBS_JOBID%%\.*}.`hostname`.ER ;
    [ -f /var/spool/torque/spool/${PBS_JOBID%%\.*}.`hostname -f`.ER ] &&  \
    File=/var/spool/torque/spool/${PBS_JOBID%%\.*}.`hostname -f`.ER ;
    [ ${#msg} -eq 0 ] && msg=$WRKmsg/$PBS_JOBNAME ;
    mv -f $File $msg.eo 2>/dev/null ;
    exit ; }
  fi ;;
esac

###################################################################################

#leap/normal year function              

set_time_arrays () {
  testyr=$1
  leapyr=1   #assumed = normal year
  if (( testyr % 4 == 0 )) ; then
    if (( testyr % 100 == 0 )) ; then
      if (( testyr % 400 == 0 )) ; then
        leapyr=0  #leap year
      fi
    else
      leapyr=0    #leap year
    fi
  fi
  if   [ $leapyr -eq 0 ] ; then
   day=(x 31 29 31 30 31 30 31 31 30 31 30 31)
  elif [ $leapyr -eq 1 ] ; then
   day=(x 31 28 31 30 31 30 31 31 30 31 30 31)
  fi

  if [ ${#yr_360} -gt 0 ] && [ $yr_360 == "y" ] ; then
   day=(x 30 30 30 30 30 30 30 30 30 30 30 30)
  fi

  if [ ${#yr_365} -gt 0 ] && [ $yr_365 == "y" ] ; then
   day=(x 31 28 31 30 31 30 31 31 30 31 30 31)
  fi

  month[1]=Jan ; month[2]=Feb ; month[3]=Mar ; month[4]=Apr ; month[5]=May
  month[6]=Jun ; month[7]=Jul ; month[8]=Aug ; month[9]=Sep ; month[10]=Oct
  month[11]=Nov ; month[12]=Dec
  return $leapyr ;
}

###################################################################################

#functions to write in job command file 

  #instruction
BCH ()  { echo "$1" >> $CMDfile ; }
  #title
wt=0
BCHt () { wt=$(( $wt + 1 ))
          echo                                             >> $CMDfile
          echo "#${hyphen[79]}"                            >> $CMDfile
          echo "# $wt - $1"                                >> $CMDfile
          echo "#${hyphen[79]}"                            >> $CMDfile
          echo 'echo ; echo "---- '$wt' ---- '$1'" ; echo' >> $CMDfile
          echo                                             >> $CMDfile ; }
          #                                               
          ##79-                                           
          ## 1 - Titre                                    
          ##79-                                           
          #echo ; echo "-------- 1 -------- Titre" ; echo 
          #                                               

  #comment
BCHc () { echo       >> $CMDfile
          echo '#'$1 >> $CMDfile
          echo       >> $CMDfile ; }
          #                                               
          ##comment                                       
          #     

###################################################################################

#stock function: automatised ftp on the mass storage system

  #usage: aftp put [local-dir/]local-file   [[remote-dir/]remote-file]
  #       aftp get [remote-dir/]remote-file [[local-dir/]local-file]  
  #       aftp mkdir remode-dir                                       
  #       ...                                                         

aftp () { (
ftp -n -v <<EOF
open $stock
user $USER `tail $MARbin/util/password | awk '{print $1}'`
bin
prompt
$@
bye
EOF
) > $$
case $1 in
  (put) [ `cat $$ | grep -c "226 File receive OK."` -ne 1 ] && return 99 || rm -f $$ ;;
  (get) [ `cat $$ | grep -c "226 File send OK."`    -ne 1 ] && return 99 || rm -f $$ ;;
  (ls)  cp $$ aftp$$ ;;
esac
rm -f $$ ; }

###################################################################################

#tar function: tar a file in a directory

  #$1 = directory of the file(s) to tar           
  #$2 = name      of the file(s) to tar           
  #$3 = name      of the tared file (default = $2)

  #tarX returns 0  if successfully completed
  #             99 if an error occurred     
      
tarX () { cd $1
          nam="$2"
          [ ${#3} -ne 0 ] && nam="$3"
          tar cvf $nam.tar $2
          if [ $? -eq 0 ] ; then
            echo "tar: $1/$2 -> $1/$nam.tar"
            return 0
          else
            echo "'@&?%! 'tar: $1/$2 -> $1/$nam.tar (not tared)"
            return 99
          fi ; }

###################################################################################

#gzip function: gzip a file in a directory

  #$1 = directory of the file to gzip
  #$2 = name      of the file to gzip

  #gzipX returns 0  if successfully completed
  #              99 if an error occurred            

gzipX () { cd $1
           gzip -8 $1/$2
           if [ $? -eq 0 ] ; then
             echo "gzip: $1/$2 -> $1/$2.gz"
             return 0
           else
             echo "'@&?%! 'gzip: $1/$2 (not gzipped)"
             return 99
           fi ; }

###################################################################################

#decompressing function: decompress a file (tar and/or gzipped)

  #$1 = directory of the file to decompress              
  #$2 = name      of the file to decompress              
  #$3 = tar/gzip extension of the file [tar/gz/tar.gz]   
  #$4 = name of a file get after decompression (optional)

  #ucX returns 0 if successfully completed
  #            1 if an error occurred     

ucX () { cd $1
         if [ -f $1/$2$3 ] ; then
           #md5sum $1/$2$3
           [ "${3}" = ".gz"      ] && gunzip  -f "$2$3"
           [ "${3}" = ".bz2"     ] && bunzip2 -f "$2$3"
           [ "${3}" = ".tar"     ] && tar xf     "$2$3"   
           [ "${3}" = ".tar.gz"  ] && gunzip  -f "$2$3" && tar xf "$2.tar"   
           [ "${3}" = ".tar.bz2" ] && bunzip2 -f "$2$3" && tar xf "$2.tar"   
           [ "${3}" = ".tgz"     ] && tar zxf    "$2$3"
           [ "${3}" = ".tbz"     ] && tar jxf    "$2$3"
           if [ ${#4} = "" ] ; then
           #the tar file was not splitted into several file
             if [ -f $1/$2 -o -d $1/$2 ] ; then
               echo "uc: $1/$2$3"
               [ -f $1/$2.tar ] && rm -f $1/$2.tar
               return 0
             else
               DAMNED "uc: $1/$2$3 (not uncompressed)" "uc"
               return 99
             fi
           else
           #the tar file was splitted into several files and exists anymore    
           #=> success test is made on one of the file it's supposed to contain
             if [ -f $1/$4 -o -d $1/$4 ] ; then
               echo "uc: $1/$2$3"
               [ -f $1/$2.tar ] && rm -f $1/$2.tar
               return 0
             else
               DAMNED "uc: $1/$2$3 (not decompressed)" "uc"
               return 99
             fi
           fi
         else
           DAMNED "decompressing: $1/$2$3 non-existent" "stk"
           return 99
         fi ; }


###################################################################################

sftpwait () {
 nbr=`ls -1 $WRKtmp/sftp-* 2>/dev/null | wc -l`


 while [ $nbr -ge 1 ] ; do
  sleep 15
  nbr=`ls -1 $WRKtmp/sftp-* 2>/dev/null | wc -l`
 done  ;

#dd=`date`
#echo $dd $0 >> $HOME/sftpwait.txt

 }

###################################################################################

#stock-get function: get a file on the mass storage system

#$1 = directory of the file on stock
#$2 = name	  of the file on stock
#$3 = destination directory on home 

#smget returns  0 if successfully completed
#	       99 if an error occurred

if [ ${cluster} == "linux" ] || [ ${cluster} == "nic" ] || [ ${cluster} == "ceci" ]  ; then

   smget () {  err=9

            if [ ${#FTPin} -ne 0 ] && [  -f "$FTPin/$2" ] ; then
             mkdir $3 &>/dev/null &
             cp -f $FTPin/$2 $3/$2 ; err=$?
             echo "WARNING: $3/$2 from $FTPin"
            fi

            if [ -f "$1/$2" ] && [ $err -ne 0 ] ; then
             mkdir $3 &>/dev/null &
             cp -f $1/$2     $3/$2 ; err=$?
            fi

            [ -f $3/$2 ] && err=0   

            if  [ $stock != "none" ] && [ $err -ne 0 ] ; then

              file=$2
              if [ ${file:0:6} == "MARlbc" ] ; then
               file="MAR*_${file:7:33}"
              fi

              if [ ${#gateway} -gt 0 ] ; then

               i=0 ; err=1
               while [ $err -ne 0 ] && [ $i -le 100 ]; do
                j=$(( $i * 2 )) ; [ $j -ge 60 ] && j=60
                sleep $j        ; i=$(( $i + 1 ))

                [ ${#FTPwait} -gt 0 ] && sftpwait
                [ ${#FTPwait} -gt 0 ] && touch ${WRKtmp}/sftp-get-$2
                $gateway "scp -q $optionssh $ustock@$stock:$1/$file $FTPdir/"
                [ ${#FTPwait} -gt 0 ] && rm -f ${WRKtmp}/sftp-get-$2

                mkdir $3 &>/dev/null
                
                if [ ${#FTPin} -ne 0 ] && [ ${FTPin} == ${FTPdir} ] ; then
                 cp $FTPdir/$file $3/ ; err=$?
                else 
                 mv $FTPdir/$file $3/ ; err=$?
                fi
               done

              else

              i=0 ; err=1
              while [ $err -ne 0 ] && [ $i -le 100 ]; do
               j=$(( $i * 2 )) ; [ $j -ge 60 ] && j=60
               sleep $j        ; i=$(( $i + 1 ))
               mkdir $3 &>/dev/null
               [ ${#FTPwait} -gt 0 ] && sftpwait
               [ ${#FTPwait} -gt 0 ] && touch ${WRKtmp}/sftp-get-$2
               scp -q $optionssh $ustock@$stock:$1/$file $3/  ; err=$?
               [ ${#FTPwait} -gt 0 ] && rm -f ${WRKtmp}/sftp-get-$2
              done

              fi
             fi

             if [ $err -eq 0 ] ; then
              echo "smget: $1/$2 -> $3/$2"
              if [ ${#FTPin} -ne 0 ] && [ ! -f "$FTPin/$2" ] ; then
               cp $3/$file $FTPin/ &
              fi
              return 0
             else
              DAMNED "smget: $1/$2 not -> $3/$2" "stk"
              return 99
             fi ; }
fi

if [[ ${cluster} == "obelix" ]] ; then
  smget () { cd $3 ;
             ln -s $1/$2 . ;
             if [ $? -eq 0 ] ; then
              echo "smget: ln -s $1/$2 -> $3/$2"
              return 0
             else
              DAMNED "smget: $1/$2 not -> $3/$2" "stk"
              return 99
             fi ; }
fi

if [ ${cluster} == "idirs" ] ; then
  smget () { cd $3 ; mfget  $1/$2 ;
             if [ $? -eq 0 ] ; then
             echo "smget: $1/$2 -> $3/$2"
             return 0
             else
             DAMNED "smget: $1/$2 not -> $3/$2" "stk"
             return 99
             fi ; }
fi

if [ ${cluster} == "foehn" ] || [ ${cluster} == "froggy" ] ; then
  smget () { cd $3 ; iget -v -f  $1/$2 ;
             if [ $? -eq 0 ] ; then
             echo "smget: $1/$2 -> $3/$2"
             return 0
             else
             DAMNED "smget: $1/$2 not -> $3/$2" "stk"
             return 99
             fi ; }
fi


if [ ${cluster} == "nasa" ] ; then
  smget () { cp -f  $1/$2 $3/$2 ;
             if [ $? -eq 0 ] ; then
             echo "smget: $1/$2 -> $3/$2"
             return 0
             else
             DAMNED "smget: $1/$2 not -> $3/$2" "stk"
             return 99
             fi ; }
fi

###################################################################################

#stock-put function: put a file on the mass storage system

#$1 = directory of the file on home
#$2 = name	  of the file on home
#$3 = destination directory on stock

#smput returns  0 if successfully completed
#	       99 if an error occurred


if [ ${cluster} == "linux" ] || [ ${cluster} == "nic" ] || [ ${cluster} == "ceci" ] || [ ${cluster} == "obelix" ] ; then

 smput () { err=9

            chmod go+r $1/$2

            if [ ${#FTPout} -ne 0 ] && [ -d ${FTPout} ] ; then
             cp -f $1/$2 $FTPout/$2 ; err=$?
             echo "smput WARNING: $1/$2 to $FTPout"
            fi

            if [ -d $3 ] && [ $err -ne 0 ] ; then
             cp -f $1/$2 $3/$2      ; err=$?
            fi

            if [ $stock != "none" ] && [ $err -ne 0 ]; then

              RAND=$RANDOM

             if [ ${#gateway} -gt 0 ] ; then

              cp -f $1/$2               $FTPdir/$2-$RAND

              i=0 ; err=1
              while [ $err -ne 0 ] && [ $i -le 100 ] ; do
               j=$(( $i * 2 )) ; [ $j -ge 60 ] && j=60
               sleep $j        ; i=$(( $i + 1 )) 
               [ ${#FTPwait} -gt 0 ] && sftpwait
               [ ${#FTPwait} -gt 0 ] && touch ${WRKtmp}/sftp-put-$2
               [ $i -le 1 ] && $gateway     "ssh $optionssh $ustock@$stock mkdir -p $3" 
               $gateway "scp -q $optionssh $FTPdir/$2-$RAND $ustock@$stock:$3/$2" ; err=$?
               [ ${#FTPwait} -gt 0 ] && rm -f ${WRKtmp}/sftp-put-$2
              done
  
              rm -f                     $FTPdir/$2-$RAND
               
             else
  
              i=0 ; err=1
              while [ $err -ne 0 ] && [ $i -le 100 ] ; do
              j=$(( $i * 2 )) ; [ $j -ge 60 ] && j=60
               sleep $j        ; i=$(( $i + 1 ))
               [ ${#FTPwait} -gt 0 ] && sftpwait
               [ ${#FTPwait} -gt 0 ] && touch ${WRKtmp}/sftp-put-$2
               [ $i -le 1 ] && ssh $optionssh          $ustock@$stock "mkdir -p $3"
               scp                 $optionssh -q $1/$2 $ustock@$stock:$3/$2  ; err=$?
               [ ${#FTPwait} -gt 0 ] && rm -f ${WRKtmp}/sftp-put-$2
              done

             fi
            fi

            if [ $err -eq 0 ] ; then
             echo "smput: $1/$2 -> $3/$2"
             return 0
            else
             DAMNED "smput: $1/$2 not -> $3/$2" "stk"
             cp -f $1/$2 $FTPdir
             return 99
            fi ; }
fi

if [ ${cluster} == "idris" ] ; then
  smput () { cd $1 ; mfput  $2  $3/$2 ;
             if [ $? -eq 0 ] ; then
              echo "smput: $1/$2 -> $3/$2"
              return 0
             else
              DAMNED "smput: $1/$2 not -> $3/$2" "stk"
              return 99
             fi ; }
fi

if [ ${cluster} == "foehn" ] || [ ${cluster} == "froggy" ] ; then
  smput () { cd $1 ; iput -f -v  $2  $3/$2 ;
             if [ $? -eq 0 ] ; then
              echo "smput: $1/$2 -> $3/$2"
              return 0
             else
              DAMNED "smput: $1/$2 not -> $3/$2" "stk"
              return 99
             fi ; }
fi

if [ ${cluster} == "nasa" ] ; then
  smput () { mkdir   -p $3 ;
             cp -f  $2  $3/$2 ;
             if [ $? -eq 0 ] ; then
              echo "smput: $1/$2 -> $3/$2"
              return 0
             else
              DAMNED "smput: $1/$2 not -> $3/$2" "stk"
              return 99
             fi ; }
fi

###################################################################################

MARdcl_mod () {

. INI.ctr

dir=$1
MARdcl_inc=$2

pwd=$PWD
cd $dir

# MARdcl_mod tar name
Z=${MARdcl_inc##*.inc}
MARdcl_inc=${MARdcl_inc%$Z}

MARdcl=${MARdcl_inc%.inc}
MARdcl=${MARdcl##*MARdcl_}
MARdcl_mod=MARdcl_mod_${MARdcl}.mod.tar

# untar MARdcl_inc
[[ ! -f $MARdcl_inc$Z ]] && echo '@&?%! '"[MARdcl_mod.sh] $MARdcl_inc$Z is missing" && exit
ucX "$dir" "$MARdcl_inc" "$Z"

# MARdim.inc
VMX=$(awk -v FS="[^0-9]*" 'FNR==2 {print $2}' MARdim.inc)
VMY=$(awk -v FS="[^0-9]*" 'FNR==2 {print $3}' MARdim.inc)
VIP11=$(awk -v FS="[^0-9]*" 'FNR==2 {print $5}' MARdim.inc)
VJP11=$(awk -v FS="[^0-9]*" 'FNR==2 {print $7}' MARdim.inc)
VMZ=$(awk -v FS="[^0-9]*" 'FNR==4 {print $2}' MARdim.inc)
VMX1=$(awk -v FS="[^0-9]*" 'FNR==10 {print $3}' MARdim.inc)
VMX2=$(awk -v FS="[^0-9]*" 'FNR==10 {print $5}' MARdim.inc)
VMY1=$(awk -v FS="[^0-9]*" 'FNR==12 {print $3}' MARdim.inc)
VMY2=$(awk -v FS="[^0-9]*" 'FNR==12 {print $5}' MARdim.inc)
VMZABSO=$(awk -v FS="[^0-9]*" 'FNR==20 {print $2}' MARdim.inc)
VN6=$(awk -v FS="[^0-9]*" 'FNR==31 {print $3}' MARdim.inc)
VN7=$(awk -v FS="[^0-9]*" 'FNR==31 {print $5}' MARdim.inc)
VMW=$(awk -v FS="[^0-9]*" 'FNR==38 {print $2}' MARdim.inc)
# MAR_SV.inc
VKLONV=$(awk -v FS="[^0-9]*" 'FNR==2 {print $2}' MAR_SV.inc)
VNSOL=$(awk -v FS="[^0-9]*" 'FNR==2 {print $3}' MAR_SV.inc)
VNSNO=$(awk -v FS="[^0-9]*" 'FNR==2 {print $4}' MAR_SV.inc)
VNBWRI=$(awk -v FS="[^0-9]*" 'FNR==4 {print $2}' MAR_SV.inc)

[[ -f mardim_mod.f90 ]] && rm -f mardim_mod.f90
CMDfile="mardim_mod.f90"
BCH "module mardim"
BCH "      implicit none"
BCH "      integer, parameter :: mx = $VMX"
BCH "      integer, parameter :: my = $VMY"
BCH "      integer, parameter :: ip11 = $VIP11"
BCH "      integer, parameter :: jp11 = $VJP11"
BCH "      integer, parameter :: mz = $VMZ"
BCH "      ! mzir1 may be chosen much larger than mz,"
BCH "      ! if the model vertical domain covers a small part of the air column"
BCH "      integer, parameter :: mzir1 = mz + 1"
BCH "      integer, parameter :: mzir = mz + 2"
BCH "      integer, parameter :: mx1 = $VMX1"
BCH "      integer, parameter :: mx2 = $VMX2"
BCH "      integer, parameter :: my1 = $VMY1"
BCH "      integer, parameter :: my2 = $VMY2"
BCH "      integer, parameter :: myd2 = 1 + my / 2"
BCH "      integer, parameter :: mz1 = mz - 1"
BCH "      integer, parameter :: mzz = mz + 1"
BCH "      integer, parameter :: i_2 = mx - mx1 + 1"
BCH "      integer, parameter :: j_2 = my - my1 + 1"
BCH "      integer, parameter :: mzabso = $VMZABSO"
BCH "      integer, parameter :: mzhyd = mzabso + 1"
BCH "      ! if #NV removed (NO vectorization) then klon = 1"
BCH "      integer, parameter :: klon = 1"
BCH "      integer, parameter :: klev = mz"
BCH "      integer, parameter :: kdlon = klon"
BCH "      integer, parameter :: kflev = klev"
BCH "      ! n6 et n7 determine a relaxation zone towards lateral boundaries"
BCH "      ! (large scale values of the variables)."
BCH "      ! This zone extends over n6-1 points."
BCH "      ! Davies (1976) propose 5 points (i.e. n6=6 and n7=7)"
BCH "      integer, parameter :: n6 = $VN6"
BCH "      integer, parameter :: n7 = $VN7"
BCH "      ! mw is the total number of mosaics"
BCH "      integer, parameter :: mw = $VMW"
BCH "end module mardim"

[[ -f mar_sv_mod.f90 ]] && rm -f mar_sv_mod.f90
CMDfile="mar_sv_mod.f90"
BCH "module mar_sv"
BCH "    implicit none"
BCH "    integer, parameter :: klonv = $VKLONV"
BCH "    integer, parameter :: nsol = $VNSOL"
BCH "    integer, parameter :: nsno = $VNSNO"
BCH "    integer, parameter :: nb_wri = $VNBWRI"
BCH "end module mar_sv"

rm -f MARdim.inc MARdim.inc_old MAR_SV.inc MAR_SV.inc_nv MAR_TV.inc_old MAR_LB.inc_old

# tar cvf $MARdcl_mod mardim_mod.f90 mar_sv_mod.f90
# rm -f mardim_mod.f90 mar_sv_mod.f90

cd $pwd


}
