#!/bin/bash

#set -x

#
# in ~/.ssh/config
#Host climato.be
#ControlMaster auto
#ControlPath ~/.ssh/sockets/%r@%h-%p
#Compression yes
#Host 139.165.29.17
#ControlMaster auto
#ControlPath ~/.ssh/sockets/%r@%h-%p
#Compression yes



date

ssh_options="-C -o ConnectTimeout=10 -o ConnectionAttempts=10"

export PATH="$PATH:/usr/bin:/bin:/sbin:/usr/local/bin:$HOME/bin:$HOME/util/bin:$HOME/MAR/bin"

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

. $HOME/MAR/bin/INI.ctr
. $HOME/MAR/bin/FUNCTIONS

area=$WLDreg

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

ping -q -c 2 $stock &> /dev/null

[ $? -ne 0 ] && echo "ERROR ping to $stock" && exit 1

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

mkdir ~/.ssh/sockets &> /dev/null

err=0

ssh -v ${ustock}@${stock} date &> $HOME/.toto
grep "Control socket connect" $HOME/.toto | grep "Connection refused"  &>/dev/null
[ $? -eq 0 ] && rm -f ~/.ssh/sockets/${ustock}@${stock}*

ssh -v ${ustock}@${stock} date &> $HOME/.toto
grep "socket" $HOME/.toto | grep "does not exist" &>/dev/null
[ $? -eq 0 ] && err=1

[ `date +%H` == "00" ] && err=1

while [ $err -ne 0 ] ; do

 pid=`ps ux | grep ssh | grep ${stock} | grep "ServerAliveInterval=" | grep "fN" | awk '{print $2}'`
 kill $pid    &>/dev/null
 sleep 2

 kill -9 $pid &>/dev/null
 sleep 1

 pid=`ps ux | grep ssh | grep ${stock} | grep "ServerAliveInterval=" | grep "fN" | awk '{print $2}'`
 kill -9 $pid &>/dev/null

 rm -f ~/.ssh/sockets/${ustock}@${stock}* &>/dev/null
 ssh -M -Y -C -o ServerAliveInterval=60 -fN ${ustock}@${stock} 
 err=$?
done

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

[ ! -z $FTPdir ] && [ -d $FTPdir ] && cd $FTPdir
[ ! -z $FTPout ] && [ -d $FTPout ] && cd $FTPout && find $FTPout/*CRASH*  -type f -atime +5  -mtime +5  -exec rm -f {} \; &>/dev/null

find  ${HOME}/MAR/tmp/sftp-* -type f -amin +30 -mmin +30 -exec rm -f {} \; &>/dev/null

rm -f ${WRKtmp}/sftp-FTPcheck-* &>/dev/null

[ -z $FTPout ] && exit

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

for file in ONE.*.nc.gz ICE.*.nc.gz MAR.*.nc.gz ; do
 if [ -f $file ] ; then
  sim=${file:4:3}
  dom=${area}${file:4:1}
  yyyy=${file:8:4}
 
  size1=`du -sk $file | awk '{print $1}'`
  sleep 2
  size2=`du -sk $file | awk '{print $1}'`

  file2="*${sim}.${yyyy}.*.nc.gz"

  if [ $size1 -eq $size2 ] ; then
   sftpwait
   touch ${WRKtmp}/sftp-FTPcheck-$file
   ssh $ssh_options        ${ustock}@${stock} mkdir -p $STKmar/$dom/$sim/$yyyy &>/dev/null
   scp $ssh_options $file2 ${ustock}@${stock}:$STKmar/$dom/$sim/$yyyy
   [ $? -eq 0 ] && rm -vf $file2
   rm -f ${WRKtmp}/sftp-FTPcheck-$file   
  fi
 fi
done

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

for file in MARsim_* ; do
 if [ -f $file ] ; then
  sim=${file:7:3}
  dom=${area}${file:7:1}
  yyyy=${file:11:4}
  file2="MARsim_${sim}.${yyyy}.*"
  sftpwait
  touch ${WRKtmp}/sftp-FTPcheck-$file
  ssh $ssh_options        ${ustock}@${stock} mkdir -p $STKmar/$dom/input/MARsim/$sim/$yyyy  &>/dev/null
  scp $ssh_options $file2 ${ustock}@${stock}:$STKmar/$dom/input/MARsim/$sim/$yyyy
  [ $? -eq 0 ] && rm -vf $file2
  rm -f ${WRKtmp}/sftp-FTPcheck-$file  
 fi
done

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


for file in MAR_*\.log\.* ; do
 if [ -f $file ] ; then
  sim=${file:4:3}
  dom=${area}${file:4:1}
  file2="MAR_${sim}.log.* MAR.log.${sim}.*"
  sftpwait
  touch ${WRKtmp}/sftp-FTPcheck-$file
  ssh $ssh_options        ${ustock}@${stock} mkdir -p $STKmar/$dom/$sim/log  &>/dev/null
  scp $ssh_options $file2 ${ustock}@${stock}:$STKmar/$dom/$sim/log
  [ $? -eq 0 ] && rm -vf $file2
  rm -f ${WRKtmp}/sftp-FTPcheck-$file
 fi
done

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

exit

if [ ! -z $FTPin ] && [ -d $FTPin ] ; then

 cd $FTPin

 for file in *.exe ; do
  if [ -f $file ] ; then
   sim=${file:4:3}
   dom=${area}${file:4:1}
   sftpwait
   touch ${WRKtmp}/sftp-FTPcheck-$file
   rsync -auPL ${ustock}@${stock}:$STKmar/$dom/$sim/code/$file $file 
   rm -f ${WRKtmp}/sftp-FTPcheck-$file
  fi
 done

fi

exit 0

