## pantasks.pro : globals and support macros : -*- sh -*-

# globals used to control system behavior : these should be in uppercase
$NEBULOUS = 0
$NETWORK = 1
$PARALLEL = 1
$VERBOSE = 1
$LOGDIR = `pwd`
$LOGDIR = $LOGDIR/pantasks_logs
mkdir $LOGDIR
$KEEP_FAILURES = 0

$LOADPOLL = 0.25
$LOADEXEC = 5
$RUNPOLL = 0.5
$RUNEXEC = 1

$EXIT_SUCCESS     = 0
$EXIT_UNKNOWN_ERR = 1
$EXIT_SYS_ERR     = 2
$EXIT_CONFIG_ERR  = 3
$EXIT_PROG_ERR    = 4
$EXIT_DATA_ERR    = 5

# DB lists the database names in use; the default ipprc database
# is only used if DB:n is 0
$DB:n = 0

# very basic values: set these with init.copy.mhpcc
$default_host     = anyhost
$workdir_template = `pwd`

# user function to add databases by name (avoids duplicates)
macro add.database
  if ($0 != 2)
    echo "USAGE: add.database (db)"
    break
  end
  if ($?DB:n == 0)
    list DB -add $1
    return
  end

  $found = 0
  for i 0 $DB:n
    if ($DB:$i == $1) 
      $found = 1
      echo "$DB:$i set"
      last
    end
  end
  
  if ($found == 0)
    list DB -add $1
  end
end

macro del.database
  if ($0 != 2)
    echo "USAGE: del.database (db)"
    break
  end
  if ($?DB:n == 0)
    return
  end

  list DB -del $1
end

macro init.isp
  list DB -add isp
end

macro init.essence
  list DB -add essence_v2
end

macro init.simtest.local
  list DB -add simtest
  $PARALLEL = 0
  controller exit true
end

macro init.simtest.default.db
  $host = `hostname`
  if ($PARALLEL) 
    controller exit true
    controller host add $host
  end
end

macro init.simtest
  list DB -add simtest

  $host = `hostname`
  if ($PARALLEL) 
    controller exit true
    controller host add $host
  end
end

# need the ability to activate or deactivate specific tasks...

macro detrend.on
  detproc.on
  detstack.on
  detnorm.on
  detresid.on
  detreject.on
end

macro detrend.off
  detproc.off
  detstack.off
  detnorm.off
  detresid.off
  detreject.off
end

macro detrend.reset
  detproc.reset
  detstack.reset
  detnorm.reset
  detresid.reset
  detreject.reset
end

macro all.on
  register.on
  detrend.on
  chip.on
  camera.on
  fake.on
  warp.on
  diff.on
  stack.on
end

macro all.off
  register.off
  detrend.off
  chip.off
  camera.off
  fake.off
  warp.off
  diff.off
  stack.off
end

macro module.tasks
  module register.pro
  module detrend.process.pro
  module detrend.stack.pro
  module detrend.norm.pro
  module detrend.resid.pro
  module detrend.reject.pro
  module chip.pro
  module camera.pro
  module fake.pro
  module warp.pro
  module diff.pro
  module stack.pro
end

macro showcommand
  local n 

  if ($VERBOSE)
    $command = $taskarg:0
    for n 1 $taskarg:n
      $command = $command $taskarg:$n
    end
    if ($0 == 2)
      echo ""
      echo "$1 for: $command"
      echo "job exit status: $JOB_STATUS"
      echo "job host: $JOB_HOSTNAME"
      echo "job dtime: $JOB_DTIME"
    else
      echo "command: $command"
    end    
  end
end

macro add_standard_args
  if ($0 != 2)
    echo "Must pass in the command of interest, and this function will supplement"
    stop
  end

  local command
  $command = $$1

  if ($?DBNAME && "$DBNAME" != "DEFAULT")
    $command = $command --dbname $DBNAME
  end
  if ($?NOOP != 0) 
    if ($NOOP != 0)
      $command = $command --no-op
    end
  end
  if ($?NOUPDATE != 0) 
    if ($NOUPDATE != 0)
      $command = $command --no-update
    end
  end
  $$1 = $command --verbose
end


macro process_exit
  if ($0 != 4)
    echo "USAGE: process_exit (bookName) (pageName) (exitCode)"
    break
  end

  $bookName = $1
  $pageName = $2
  $exitCode = $3

  if ($VERBOSE > 4)
    echo "*** exit status ***"
    echo "JOB_STATUS: $JOB_STATUS"
    echo "JOB_HOSTNAME: $JOB_HOSTNAME"
    echo "JOB_DTIME: $JOB_DTIME"
    echo "*** stdout ***"
    queueprint stdout
    echo "*** stderr ***"
    queueprint stderr
  end

  # success
  if ($exitCode == $EXIT_SUCCESS) 
    # the handler scripts update DB the tables; here we just update the page
    book setword $bookName $pageName pantaskState DONE
    return
  end

  # failure related to the data files
  # jobs which result in DATAERR must have db pantaskState updated 
  if ($exitCode == $EXIT_DATA_ERR)
    showcommand failure
    book setword $bookName $pageName pantaskState DATA_ERR
    return
  end

  if ($VERBOSE)
    echo "*** stdout ***"
    queueprint stdout
    echo "*** stderr ***"
    queueprint stderr
  end

  # failure related to the data files
  if ($exitCode == $EXIT_SYS_ERR)
    # stop
    showcommand "system failure"
    book setword $bookName $pageName pantaskState SYS_ERR
    return
  end

  # failure related to the data files
  if ($exitCode == $EXIT_CONFIG_ERR)
    # stop
    showcommand "config error"
    book setword $bookName $pageName pantaskState CONFIG_ERR
    return
  end

  # failure related to the data files
  if ($exitCode == $EXIT_PROG_ERR)
    # stop
    showcommand "programming error"
    book setword $bookName $pageName pantaskState PROG_ERR
    return
  end

  # any other exit status
  showcommand failure
  book setword $bookName $pageName pantaskState UNKNOWN_ERR
end

## XXX for the moment, remove all errors
macro process_cleanup
  if ($0 != 2)
    echo "USAGE: process_cleanup (bookname)"
    break
  end

  book delpage $1 -key pantaskState DONE

  if (not($KEEP_FAILURES))
    book delpage $1 -key pantaskState SYS_ERR
    book delpage $1 -key pantaskState DATA_ERR
    book delpage $1 -key pantaskState PROG_ERR
    book delpage $1 -key pantaskState CONFIG_ERR
    book delpage $1 -key pantaskState UNKNOWN_ERR
  end
end

macro set.verbosity
  if ($0 != 2)
    echo "USAGE: set.verbosity (level)"
    break
  end
  $VERBOSE = $1
end

macro show.variables
  ??
end

macro set.workdir.by.camera
  if ($0 != 6)
    echo "USAGE: set.workdir.by.camera (camera) (class_id) (template) (default) (varname)"
    break
  end

  local host default template camera classID varname
  $camera = $1
  $classID = $2
  $template = $3
  $default = $4
  $varname = $5

  if ("$template" == "NULL")
    $varname = $workdir_template
    echo "WARNING: WORKDIR template not set.  Defaulting to $workdir_template"
    break
  end

  # missing camera and/or chiphosts table results in host = NULL
  book getword chiphosts $camera $classID -var host

  if ("$host" == "NULL")
    strsub $template @HOST@ $default -var $varname
  else
    strsub $template @HOST@ $host -var $varname
  end
end

macro set.volume.by.camera
  if ($0 != 3)
    echo "USAGE: set.volume.by.camera (camera) (class_id) (template) (default) (varname)"
    echo " uses global variable NEBULOUS to determine if nebulous is active"
    break
  end

  local host default template camera classID varname
  $camera = $1
  $classID = $2
  $template = $3
  $default = $4
  $varname = $5

  # missing camera and/or chiphosts table results in host = NULL
  book getword chiphosts $camera $classID -var host

  if (not($NEBULOUS))
    $$varname = "NULL"
    return
  end

  if ("$host" == "NULL") 
    $$varname = $default
    return
  end

  strsub $template @HOST@ $host -var $varname
end

macro set.host.for.camera
  if ($0 != 3)
    echo "USAGE: set.host.for.camera (camera) (class_id)"
    break
  end

  if (not($PARALLEL))
    host local
    return
  end

  # missing camera and/or chiphosts table results in host = NULL
  book getword chiphosts $1 $2 -var host

  if ("$host" == "NULL")
    host anyhost
  else
    host $host
  end
end

macro get.host.for.camera
  if ($0 != 3)
    echo "USAGE: get.host.for.camera (camera) (class_id)"
    break
  end

  if (not($PARALLEL))
    echo local
    return
  end

  # missing camera and/or chiphosts table results in host = NULL
  book getword chiphosts $1 $2 -var host

  if ("$host" == "NULL")
    echo anyhost
  else
    echo $host
  end
end

macro check.globals
  if ($?NETWORK == 0)
    echo "NETWORK not defined: load pantasks.pro first"
    break
  end
  if ($?PARALLEL == 0)
    echo "PARALLEL not defined: load pantasks.pro first"
    break
  end
  if ($?VERBOSE == 0)
    echo "VERBOSE not defined: load pantasks.pro first"
    break
  end
  if ($?LOGDIR == 0)
    echo "LOGDIR not defined: load pantasks.pro first"
    break
  end
  if ($?KEEP_FAILURES == 0)
    echo "KEEP_FAILURES not defined: load pantasks.pro first"
    break
  end
end
