## survey.pro : tasks related to automation of the PS1 survey : -*- sh -*-

# test for required global variables
check.globals

# there is a bug in 'getbook', use a different method below
# book getbook SURVEY_DIFF -var isBook
# if ("$isBook" != "SURVEY_DIFF")
#   book create SURVEY_DIFF
# end
# book getbook SURVEY_MAGIC -var isBook
# if ("$isBook" == "NULL")
#   book create SURVEY_MAGIC
# end
# book getbook SURVEY_DESTREAK -var isBook
# if ("$isBook" == "NULL")
#   book create SURVEY_DESTREAK
# end
# book getbook SURVEY_DIST -var isBook
# if ("$isBook" == "NULL")
#   book create SURVEY_DIST
# end

if (not($?haveSurveyBooks))
 book create SURVEY_DIFF
 book create SURVEY_DIFF_WARPSTACK
 book create SURVEY_DIFF_STACKSTACK
 book create SURVEY_MAGIC
 book create SURVEY_DESTREAK
 book create SURVEY_DIST
 book create SURVEY_ADDSTAR   
 $haveSurveyBooks = TRUE
end

$SURVEY_DIFF_DB = 0
$SURVEY_DIFF_WARPSTACK_DB = 0
$SURVEY_DIFF_STACKSTACK_DB = 0
$SURVEY_MAGIC_DB = 0
$SURVEY_DESTREAK_DB = 0
$SURVEY_DIST_DB = 0
$SURVEY_ADDSTAR_DB = 0

$SURVEY_EXEC = 120
$SURVEY_POLL = 10
$SURVEY_TIMEOUT = 600

macro survey.on
  task survey.diff
    active true
  end
  task survey.warpstack.diff
    active true
  end
  task survey.stackstack.diff
    active true
  end
  task survey.magic
    active true
  end
  task survey.addstar
    active true
  end
  task survey.destreak
    active true
  end
  task survey.dist
    active true
  end
end

macro survey.off
  task survey.diff
    active false
  end
  task survey.warpstack.diff
    active false
  end
  task survey.stackstack.diff
    active false
  end
  task survey.magic
    active false
  end
  task survey.addstar
    active false
  end
  task survey.destreak
    active false
  end
  task survey.dist
    active false
  end
end

# user functions to manipulate diff labels
macro survey.add.diff
  if ($0 != 4)
    echo "USAGE: survey.add.diff (label) (dist_group) (workdir base)"
    break
  end
  book newpage SURVEY_DIFF $1
  book setword SURVEY_DIFF $1 DIST_GROUP $2
  book setword SURVEY_DIFF $1 WORKDIR $3
  book setword SURVEY_DIFF $1 STATE PENDING
end

macro survey.del.diff
  if ($0 != 2)
    echo "USAGE: survey.del.diff (label)"
    break
  end
  book delpage SURVEY_DIFF $1
end

macro survey.show.diff
  if ($0 != 1)
    echo "USAGE: survey.show.diff"
    break
  end
  book listbook SURVEY_DIFF
end

# user functions to manipulate warp/stack diff labels
macro survey.add.WSdiff
  if ($0 != 5)
    echo "USAGE: survey.add.WSdiff (warp label) (stack label) (dist_group) (workdir base)"
    break
  end
  book newpage SURVEY_DIFF_WARPSTACK $1
  book setword SURVEY_DIFF_WARPSTACK $1 STACK_LABEL $2
  book setword SURVEY_DIFF_WARPSTACK $1 DIST_GROUP $3
  book setword SURVEY_DIFF_WARPSTACK $1 WORKDIR $4
  book setword SURVEY_DIFF_WARPSTACK $1 STATE PENDING
end

macro survey.del.WSdiff
  if ($0 != 2)
    echo "USAGE: survey.del.WSdiff (warp label)"
    break
  end
  book delpage SURVEY_DIFF_WARPSTACK $1
end

macro survey.show.WSdiff
  if ($0 != 1) 
    echo "USAGE: survey.show.WSdiff"
    break
  end
  book listbook SURVEY_DIFF_WARPSTACK
end

# user functions to manipulate stack/stack diff labels
macro survey.add.SSdiff
  if ($0 != 5)
    echo "USAGE: survey.add.SSdiff (stack label) (template label) (dist_group) (workdir base)"
    break
  end
  book newpage SURVEY_DIFF_STACKSTACK $1
  book setword SURVEY_DIFF_STACKSTACK $1 STACK_LABEL $2
  book setword SURVEY_DIFF_STACKSTACK $1 DIST_GROUP $3
  book setword SURVEY_DIFF_STACKSTACK $1 WORKDIR $4
  book setword SURVEY_DIFF_STACKSTACK $1 STATE PENDING
end

macro survey.del.SSdiff
  if ($0 != 2)
    echo "USAGE: survey.del.SSdiff (stack label)"
    break
  end
  book delpage SURVEY_DIFF_STACKSTACK $1
end

macro survey.show.SSdiff
  if ($0 != 1) 
    echo "USAGE: survey.show.SSdiff"
    break
  end
  book listbook SURVEY_DIFF_STACKSTACK
end

# user functions to manipulate magic labels
macro survey.add.magic
  if ($0 != 3)
    echo "USAGE: survey.add.magic (label) (workdir base)"
    break
  end
  book newpage SURVEY_MAGIC $1
  book setword SURVEY_MAGIC $1 WORKDIR $2
  book setword SURVEY_MAGIC $1 STATE PENDING
end

macro survey.del.magic
  if ($0 != 2)
    echo "USAGE: survey.del.magic (label)"
    break
  end
  book delpage SURVEY_MAGIC $1
end

macro survey.show.magic
  if ($0 != 1)
    echo "USAGE: survey.show.magic"
    break
  end
  book listbook SURVEY_MAGIC
end

macro survey.add.addstar
  if ($0 != 3)
    echo "USAGE: survey.add.addstar (label) (dvodb)"
    break
  end
  book newpage SURVEY_ADDSTAR $1
  book setword SURVEY_ADDSTAR $1 DVODB $2
  book setword SURVEY_ADDSTAR $1 STATE PENDING
end

macro survey.del.addstar
  if ($0 != 2)
    echo "USAGE: survey.del.addstar (label)"
    break
  end
  book delpage SURVEY_ADDSTAR $1
end

macro survey.show.addstar
  if ($0 != 1)
    echo "USAGE: survey.show.addstar"
    break
  end
  book listbook SURVEY_ADDSTAR
end

# user functions to manipulate destreak labels
macro survey.add.destreak
  if ($0 != 3)
    echo "USAGE: survey.add.destreak (label) (workdir base)"
    break
  end
  book newpage SURVEY_DESTREAK $1
  book setword SURVEY_DESTREAK $1 WORKDIR $2
  book setword SURVEY_DESTREAK $1 STATE PENDING
end

macro survey.del.destreak
  if ($0 != 2)
    echo "USAGE: survey.del.destreak (label)"
    break
  end
  book delpage SURVEY_DESTREAK $1
end

macro survey.show.destreak
  if ($0 != 1)
    echo "USAGE: survey.show.destreak"
    break
  end
  book listbook SURVEY_DESTREAK
end

# user functions to manipulate dist labels
macro survey.add.dist
  if ($0 != 2)
    echo "USAGE: survey.add.dist (label)"
    break
  end
  book newpage SURVEY_DIST $1
  book setword SURVEY_DIST $1 STATE PENDING
end

macro survey.del.dist
  if ($0 != 2)
    echo "USAGE: survey.del.dist (label)"
    break
  end
  book delpage SURVEY_DIST $1
end

macro survey.show.dist
  if ($0 != 1)
    echo "USAGE: survey.show.dist"
    break
  end
  book listbook SURVEY_DIST
end


task survey.diff
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.diff.log
  stderr $LOGDIR/survey.diff.log

  # generate diff warp-warp runs
  task.exec
    book npages SURVEY_DIFF -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_DIFF 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_DIFF $i -var label
	book setword SURVEY_DIFF $label STATE NEW
      end
      book getpage SURVEY_DIFF 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_DIFF_DB ++
      if ($SURVEY_DIFF_DB >= $DB:n) set SURVEY_DIFF_DB = 0
    end

    book setword SURVEY_DIFF $label STATE DONE
    book getword SURVEY_DIFF $label WORKDIR -var workdir
    book getword SURVEY_DIFF $label DIST_GROUP -var dist_group

    $year = `date +%Y`
    $month = `date +%m`
    $day = `date +%d`

    $run = difftool -definewarpwarp -distance 0.1 -good_frac 0.1 -timediff 3600
    $run = $run -input_label $label
    $run = $run -template_label $label
    $run = $run -set_dist_group $dist_group
    $run = $run -set_label $label
    $run = $run -set_workdir $workdir/$label/$year/$month/$day

    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run -dbname $DB:$SURVEY_DIFF_DB
      option $DB:$SURVEY_DIFF_DB
    end
    
#    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

task survey.warpstack.diff
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.WSdiff.log
  stderr $LOGDIR/survey.WSdiff.log

  # generate diff warp-stack runs
  task.exec
    book npages SURVEY_DIFF_WARPSTACK -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_DIFF_WARPSTACK 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_DIFF_WARPSTACK $i -var label
	book setword SURVEY_DIFF_WARPSTACK $label STATE NEW
      end
      book getpage SURVEY_DIFF_WARPSTACK 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_DIFF_WARPSTACK_DB ++
      if ($SURVEY_DIFF_WARPSTACK_DB >= $DB:n) set SURVEY_DIFF_WARPSTACK_DB = 0
    end

    book setword SURVEY_DIFF_WARPSTACK $label STATE DONE
    book getword SURVEY_DIFF_WARPSTACK $label STACK_LABEL -var stack_label
    book getword SURVEY_DIFF_WARPSTACK $label WORKDIR -var workdir
    book getword SURVEY_DIFF_WARPSTACK $label DIST_GROUP -var dist_group

    $year = `date +%Y`
    $month = `date +%m`
    $day = `date +%d`

# Do we need anything else here? 
    $run = difftool -definewarpstack -good_frac 0.2
    $run = $run -warp_label $label
    $run = $run -stack_label $stack_label
    $run = $run -set_dist_group $dist_group
    $run = $run -set_label $label
    $run = $run -set_workdir $workdir/$label/$year/$month/$day
    $run = $run -available
    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run -dbname $DB:$SURVEY_DIFF_WARPSTACK_DB
      option $DB:$SURVEY_DIFF_WARPSTACK_DB
    end
    
#    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

task survey.stackstack.diff
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  trange        13:00:00 14:00:00
  npending      1

  stdout $LOGDIR/survey.SSdiff.log
  stderr $LOGDIR/survey.SSdiff.log

  # generate diff stack-stack runs
  task.exec
    book npages SURVEY_DIFF_STACKSTACK -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_DIFF_STACKSTACK 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_DIFF_STACKSTACK $i -var label
	book setword SURVEY_DIFF_STACKSTACK $label STATE NEW
      end
      book getpage SURVEY_DIFF_STACKSTACK 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_DIFF_STACKSTACK_DB ++
      if ($SURVEY_DIFF_STACKSTACK_DB >= $DB:n) set SURVEY_DIFF_STACKSTACK_DB = 0
    end

    book setword SURVEY_DIFF_STACKSTACK $label STATE DONE
    book getword SURVEY_DIFF_STACKSTACK $label STACK_LABEL -var stack_label
    book getword SURVEY_DIFF_STACKSTACK $label WORKDIR -var workdir
    book getword SURVEY_DIFF_STACKSTACK $label DIST_GROUP -var dist_group

    $year = `date +%Y`
    $month = `date +%m`
    $day = `date +%d`
# Do we need anything else here? 
    $run = difftool -definestackstack -good_frac 0.2 
    $run = $run -input_label $label
    $run = $run -template_label $stack_label
    $run = $run -set_dist_group $dist_group
    $run = $run -set_label $label
    $run = $run -set_workdir $workdir/$label/$year/$month/$day
#    $run = $run -available
    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run -dbname $DB:$SURVEY_DIFF_STACKSTACK_DB
      option $DB:$SURVEY_DIFF_STACKSTACK_DB
    end
    
    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end



task survey.magic
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.magic.log
  stderr $LOGDIR/survey.magic.log

  # generate magic warp-warp runs
  task.exec
    book npages SURVEY_MAGIC -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_MAGIC 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_MAGIC $i -var label
	book setword SURVEY_MAGIC $label STATE NEW
      end
      book getpage SURVEY_MAGIC 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_MAGIC_DB ++
      if ($SURVEY_MAGIC_DB >= $DB:n) set SURVEY_MAGIC_DB = 0
    end

    book setword SURVEY_MAGIC $label STATE DONE
    book getword SURVEY_MAGIC $label WORKDIR -var workdir
  
    $run = magictool -rerun -definebyquery -label $label -diff_label $label -workdir $workdir/$label

    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run -dbname $DB:$SURVEY_MAGIC_DB
      option $DB:$SURVEY_MAGIC_DB
    end
    
    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

task survey.addstar
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.addstar.log
  stderr $LOGDIR/survey.addstar.log

  # generate magic warp-warp runs
  task.exec
    book npages SURVEY_ADDSTAR -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_ADDSTAR 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_ADDSTAR $i -var label
	book setword SURVEY_ADDSTAR $label STATE NEW
      end
      book getpage SURVEY_ADDSTAR 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_ADDSTAR_DB ++
      if ($SURVEY_ADDSTAR_DB >= $DB:n) set SURVEY_ADDSTAR_DB = 0
    end

    book setword SURVEY_ADDSTAR $label STATE DONE
    book getword SURVEY_ADDSTAR $label DVODB -var dvodb
  
 #   $run = addtool -definebyquery -destreaked -label $label -set_dvodb $dbodb
    $run = addtool 
    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run -dbname $DB:$SURVEY_ADDSTAR_DB
      option $DB:$SURVEY_ADDSTAR_DB
    end
    
    $run = $run -definebyquery -destreaked -label $label -set_dvodb $dvodb
    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end


task survey.destreak
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.destreak.log
  stderr $LOGDIR/survey.destreak.log

  # generate destreak runs
  task.exec
    book npages SURVEY_DESTREAK -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_DESTREAK 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_DESTREAK $i -var label
	book setword SURVEY_DESTREAK $label STATE NEW
      end
      book getpage SURVEY_DESTREAK 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_DESTREAK_DB ++
      if ($SURVEY_DESTREAK_DB >= $DB:n) set SURVEY_DESTREAK_DB = 0
    end

    book setword SURVEY_DESTREAK $label STATE DONE
    book getword SURVEY_DESTREAK $label WORKDIR -var workdir
  
    $run = magic_destreak_defineruns.pl --label $label --workdir $workdir/$label

    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run --dbname $DB:$SURVEY_MAGIC_DB
      option $DB:$SURVEY_MAGIC_DB
    end
    
#    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

task survey.dist
  host local
 
  periods      -poll $SURVEY_POLL
  periods      -exec $SURVEY_EXEC
  periods      -timeout $SURVEY_TIMEOUT
  npending     1

  stdout $LOGDIR/survey.dist.log
  stderr $LOGDIR/survey.dist.log

  # generate distribution runs
  task.exec
    book npages SURVEY_DIST -var N
    if ($N == 0)
#      echo "No labels for processing"
      break
    endif

    book getpage SURVEY_DIST 0 -var label -key STATE NEW
    if ("$label" == "NULL")
      # All labels have been done --- reset
#      echo "Resetting labels"
      for i 0 $N
        book getpage SURVEY_DIST $i -var label
	book setword SURVEY_DIST $label STATE NEW
      end
      book getpage SURVEY_DIST 0 -var label -key STATE NEW

      # Select different database
      $SURVEY_DIST_DB ++
      if ($SURVEY_DIST_DB >= $DB:n) set SURVEY_DIST_DB = 0
    end

    book setword SURVEY_DIST $label STATE DONE
  
    # note workdir is set by the script based on site.config
    $run = dist_defineruns.pl --label $label

    if ($DB:n == 0)
      option DEFAULT
    else
      $run = $run --dbname $DB:$SURVEY_DIST_DB
      option $DB:$SURVEY_DIST_DB
    end
    
#    echo $run
    command $run
  end

  # success
  task.exit    0
#    echo "Success"
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  task.exit    crash
    showcommand crash
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end
