## fake.pro : globals and support macros : -*- sh -*-
## this file contains the tasks for running the phase 0 stage
## these tasks use the book fakePendingImfile

# test for required global variables
check.globals

book init fakePendingImfile

macro fake.status
  book listbook fakePendingImfile
end

macro fake.reset
  book init fakePendingImfile
end

macro fake.on
  task fake.imfile.load
    active true
  end
  task fake.imfile.run
    active true
  end
end

macro fake.off
  task fake.imfile.load
    active false
  end
  task fake.imfile.run
    active false
  end
end

# this variable will cycle through the known database names
$fakeImfile_DB = 0

# select images ready for fake analysis
# new entries are added to fakePendingImfile
# skip already-present entries
task	       fake.imfile.load
  host         local

  periods      -poll $LOADPOLL
  periods      -exec $LOADEXEC
  periods      -timeout 30
  npending     1

  stdout NULL
  stderr $LOGDIR/fake.imfile.log

  task.exec
    if ($DB:n == 0)
      option DEFAULT
      command faketool -pendingimfile -limit 20
    else
      # save the DB name for the exit tasks
      option $DB:$fakeImfile_DB
      command faketool -pendingimfile -limit 20 -dbname $DB:$fakeImfile_DB
      $fakeImfile_DB ++
      if ($fakeImfile_DB >= $DB:n) set fakeImfile_DB = 0
    end
  end

  # success
  task.exit    0
    # convert 'stdout' to book format
    ipptool2book stdout fakePendingImfile -key fake_id:class_id -uniq -setword dbname $options:0 -setword pantaskState INIT
    if ($VERBOSE > 2)
      book listbook fakePendingImfile
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup fakePendingImfile
  end

  # locked list
  task.exit    default
    showcommand failure
  end

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

# run the fake_imfile.pl script on pending images
task	       fake.imfile.run
  periods      -poll $RUNPOLL
  periods      -exec $RUNEXEC
  periods      -timeout 60

  task.exec
    book npages fakePendingImfile -var N
    if ($N == 0) break
    if ($NETWORK == 0) break
    
    # look for new images in fakePendingImfile (pantaskState == INIT)
    book getpage fakePendingImfile 0 -var pageName -key pantaskState INIT
    if ("$pageName" == "NULL") break

    book setword fakePendingImfile $pageName pantaskState RUN
    book getword fakePendingImfile $pageName camera -var CAMERA
    book getword fakePendingImfile $pageName exp_id -var EXP_ID
    book getword fakePendingImfile $pageName exp_tag -var EXP_TAG
    book getword fakePendingImfile $pageName fake_id -var FAKE_ID
    book getword fakePendingImfile $pageName workdir -var WORKDIR_TEMPLATE
    book getword fakePendingImfile $pageName class_id -var CLASS_ID
    book getword fakePendingImfile $pageName chip_path_base -var CHIPROOT
    book getword fakePendingImfile $pageName cam_path_base -var CAMROOT
    book getword fakePendingImfile $pageName dbname -var DBNAME
    book getword fakePendingImfile $pageName reduction -var REDUCTION

    # specify choice of local or remote host based on camera and fake (class_id)
    set.host.for.camera $CAMERA $CLASS_ID

    # set the WORKDIR variable
    set.workdir.by.camera $CAMERA $CLASS_ID $WORKDIR_TEMPLATE $default_host WORKDIR

    ## generate outroot specific to this exposure (& chip)
    sprintf outroot "%s/%s/%s.fk.%s" $WORKDIR $EXP_TAG $EXP_TAG $FAKE_ID

    stderr $LOGDIR/fake.imfile.log
    stderr $LOGDIR/fake.imfile.log

    $run = fake_imfile.pl --exp_id $EXP_ID --fake_id $FAKE_ID --class_id $CLASS_ID --chiproot=$CHIPROOT --camroot=$CAMROOT --camera $CAMERA --outroot $outroot
    if ("$REDUCTION" != "NULL")
      $run = $run --reduction $REDUCTION
    end
    add_standard_args run

    # save the pageName for future reference below
    options $pageName

    # create the command line
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

  # default exit status
  task.exit    default
    process_exit fakePendingImfile $options:0 $JOB_STATUS
  end

  # operation timed out?
  task.exit    timeout
    showcommand timeout
    book setword fakePendingImfile $options:0 pantaskState TIMEOUT
  end
end


