#!/bin/sh
#

source /home/operator/condor/jobsourcefile

notcount=0
while (( $notcount < 100 ))
do
   for (( i=0 ; i < ${#scripts[@]} ; i++ ))
   do 
      date
      echo "sleeping $sleeptime..."
      sleep $sleeptime
      dayofweek=`date +%u`
      hour=`date +%k`
      source /home/operator/condor/jobsourcefile
      echo "script: ${scripts[$i]}"
      echo " day: $dayofweek hour: $hour"
#      running=`/usr/local/bin/condor_q -global | grep -c ' R '`
      running=`/usr/local/bin/condor_q -global | grep operator | grep -c ' R '`
      running=`setzero $running`
      echo " found $running jobs running"
#      queued=`/usr/local/bin/condor_q -global -format "job\n" ProcId | wc -l`
      queued=`/usr/local/bin/condor_q -global -format "%s\n" Owner | grep -c operator`
      queued=`setzero $queued`
      echo " found $queued jobs in the queue (incl. running jobs)"
#      runningscript=`/usr/local/bin/condor_q -global | grep ${scripts[$i]} | grep -c ' R '`
      runningscript=`/usr/local/bin/condor_q -global | grep operator | grep ${scripts[$i]} | grep -c ' R '`
      runningscript=`setzero $runningscript`
      echo " found $runningscript ${scripts[$i]} running"
#      queuedscript=`/usr/local/bin/condor_q -global | grep -c ${scripts[$i]}`
      queuedscript=`/usr/local/bin/condor_q -global | grep operator | grep -c ${scripts[$i]}`
      queuedscript=`setzero $queuedscript`
      echo " found $queuedscript ${scripts[$i]} in the queue (incl. running jobs)"

      #unguenstig, da es ja f versch scripte untersch sein kann
#      if [ "$runningscriptold" == "$runningscript" ]
#      then
#         echo "$runningscriptold = $runningscript"
#         sleeptime=`echo "2 * $sleeptime" | bc`
#         echo "new sleeptime: $sleeptime"
#      fi
#      runningscriptold=$runningscript
      pnototal=${pnototal[$hour]}
      echo " pnototal=$pnototal (allowed no of jobs)"
      if [ "$pnototal" -lt "$running" ]
      then
         echo " pnototal=$pnototal < $running (running) => continue"
         echo ""
         continue 
      fi
      #choose array according to the day of the week
      case $dayofweek in
         0 | 6)  pnos=( ${pnoswe[@]} ) ;;
             *)  pnos=( ${pnosweek[@]} ) ;;
      esac
      num=`echo "((( $i + 1 ) * 24 ) + ( $hour + 1 ) ) - 24 - 1 " | bc `
      pnoscript=${pnos[$num]}
      echo " => num=$num => pnoscript=$pnoscript (i.e. $pnoscript ${scripts[$i]} allowed)"
      
      echo " queued: $queued - pnototal: $pnototal"
      if [ "$queued" -lt "$pnototal" ]
      then
#         echo "$queued -lt $pnototal"
#         if [ "$queuedscript" -lt "$queued" ]
#         then 
#            echo "$queuedscript -lt $queued"
#            queuedscript=$queued
#         fi
         echo " queued ${scripts[$i]}: $queuedscript - pno: $pnoscript"
         if [ "$queuedscript" -lt "$pnoscript" ]
         then
            pno=`echo " $pnoscript - $queuedscript " | bc`
         fi
      fi

      if [ "$pno" == "0" ]
      then
         echo " pno = $pno -> continue"
         echo ""
         continue
      fi

      date=`date +\%Y/\%m/\%d`
      condordir=/magic/datacenter/autologs/condor/$date
      if [ ! -d $condordir ]
      then
        mkdir -pv $condordir
        if [ ! -d $condordir ]
        then
           echo "could not make dir "$condordir
           exit
        fi
      fi
      
      echo "  ---> starting $pno ${scripts[$i]}..."
      for (( j=1 ; j <= $pno ; j++ )) 
      do 
         echo "$i - $pno -> sleeping...."
         sleep 2
         if ! /usr/local/bin/condor_submit -a dir=$scriptsdir -a prog=${scripts[$i]} -a date=$date -a hour=`date +\%H` /home/operator/condor/run.condor 2>$errorfile
         then 
            nail -s 'condor problem'  $adrs < $errorfile
            echo "condor is not working -> exit"
            exit
         fi
      done
      echo ""
   done
done
