#!/bin/bash

#set -x

DIRrun=$HOME/MAR/SIMdir/GRa/run
DIRctr=$HOME/MAR/SIMdir/GRa/ctrl

cd /home/astr/fettweis/MAR/SIMdir/GRa/run

[ ${#1} -eq 0 ] && echo "MAR_yr RUN YYYY" && exit
[ ${#2} -eq 0 ] && echo "MAR_yr RUN YYYY" && exit
[ $2 -le 1000 ] || [ $2 -ge 3000 ]        && exit

[ $2 -le 1999 ] && YY=$(( $2 - 1900 ))
[ $2 -ge 2000 ] && YY=$(( $2 - 2000 )) && YY="0$YY"

[ ! -d "$DIRrun/$1" ] || [ ! -d "$DIRctr/$1" ] && exit

MARbin=MAR2

MARctr=`ls -1t $HOME/MAR/SIMdir/GRa/ctrl/$1/MAR.ctr* | head -1`

rm -f $DIRrun/$1/MAR_*$1*.$YY.*.cmd

i=1
until [ $i -eq 13 ] ; do

                 j=$i
 [ $i -le 9 ] && j="0$i"

 echo "MAR_yr $1 $2 $j"

 nice $MARbin -again $MARctr $1 $2 $j c >/dev/null &
 nice $MARbin -again $MARctr $1 $2 $j d >/dev/null &
 nice $MARbin -again $MARctr $1 $2 $j e >/dev/null &

 i=$(( $i + 1 ))

done

time wait

file="$DIRrun/MAR_yr_$2_$1.cmd"

echo "#!/bin/bash                                                 "  > $file
echo "#$ -N MAR$YY$1                                              " >> $file
echo "#$ -V                                                       " >> $file
echo "#$ -l h_vmem=300M                                           " >> $file
echo "#$ -l h_rt=200000                                           " >> $file
echo "#$ -l p=2                                                   " >> $file 
echo "#$ -l scratch=2G                                            " >> $file 
echo "#$ -j y                                                     " >> $file
echo "#$ -cwd                                                     " >> $file
echo "#$ -o \$HOME/MAR/msg/\$JOB_NAME.log                         " >> $file
echo "#$ -pe mpich 12                                             " >> $file

echo "for host in \`cat \$TMPDIR/machines\`; do                   " >> $file
echo " /usr/bin/rsh -n \$host \"mkdir  /scratch/$USER      &>$HOME/.\${JOB_NAME}_test.txt\" " >> $file
echo " /usr/bin/rsh -n \$host \"mkdir  /scratch/$USER/test &>$HOME/.\${JOB_NAME}_test.txt\" " >> $file
echo " /usr/bin/rsh -n \$host \"rm -rf /scratch/$USER/test 2>/dev/null\"  " >> $file
echo " tmp1=\`cat $HOME/.\${JOB_NAME}_test.txt | grep \"Read-only\"\`     " >> $file
echo "      rm -f $HOME/.\${JOB_NAME}_test.txt 2>/dev/null                " >> $file
echo " tmp2=\`/usr/bin/rsh -n \$host  \"df -k /scratch/$USER | tail -1\"\`" >> $file
echo " tmp2=\`echo \$tmp2 | awk '{print(\$4)}'\`                 " >> $file
echo " if [ \${#tmp1} -gt 0 ] | [ \$tmp2 -le 2000000 ] ; then    " >> $file
echo "  /usr/bin/rsh -n lemaitre qsub $file                               " >> $file
echo "  sleep 300 ; exit 3                                       " >> $file
echo " fi                                                        " >> $file
echo "done                                                       " >> $file


echo "i=1                                                         " >> $file
echo "for host in \`cat \$TMPDIR/machines\`; do                   " >> $file

echo " j=\$i ; [ \$i -le 9 ] && j=\"0\$i\"                        " >> $file

echo " fileC=\`ls -1tm $DIRrun/$1/*.$YY.\$j.01-*.cmd | tail -1\`  " >> $file
echo " fileD=\`ls -1tm $DIRrun/$1/*.$YY.\$j.11-*.cmd | tail -1\`  " >> $file
echo " fileE=\`ls -1tm $DIRrun/$1/*.$YY.\$j.21-*.cmd | tail -1\`  " >> $file
echo " fileL=\$HOME/MAR/msg/MAR_GR$1.$2.\$j.log                   " >> $file

echo " /usr/bin/rsh -n \$host \"\$fileC >> \$fileL ; \$fileD >> \$fileL ; \$fileE >> \$fileL ; echo \$fileL ; date\"  & " >> $file

echo " i=\$(( \$i + 1 ))                                          " >> $file

echo "done ; wait                                                 " >> $file

chmod +x $file

