#!/bin/sh
# This is a trick to re-execute this script using gtcl, but adding the
# path of the current script to $PATH first.  It works because backslashes
# are recognized by Tcl at the end of comment lines but not by shell.
# \
exec env PATH=$PATH:`dirname $0` gtcl $0 "$@"

# The rest of the script is Tcl

# 2016-04-28: The combined .mdl, .scl, and .ppf files are now kept in
#             the project directory.
# 2016-04-22: Worked on making it use rttrack to update the project so
#             that it can handle the combined files.  Deferred this for
#             now due to a bug in rttrack.
# 2016-03-09: Converted to Tcl
#             Combined .pdf, .bdf, and .lmt files are now kept in the
#             project directory.
#             No longer uses combine_problem_setup script
# 2016-01-13: Changed to use .find_script
# 2015-10-08: Added -nosubjectids option

source [.find_script probsetup_util.tcl]
source [.find_script process_util.tcl]

proc basename {path ext} {
  set filename [file tail $path]
  if {[file extension $filename]==$ext} {
    return [file rootname $filename]
  } else {
    return $filename
  }
}

proc generate_files {probsetup_path problem_name} {
  # Load the problem setup data
  if {![file exists $probsetup_path]} {
    puts stderr "Missing file $probsetup_path"
    exit 1
  }
  if {[catch {
    read_problem_setup_data $probsetup_path charmaps target_anims
  } message]} {
    puts stderr "$message"
    exit 1
  }

  # Prevent charmaps and target anims from being combined
  global problem_setup_dialog_vars
  set problem_setup_dialog_vars(combined_charmap_name) ""
  set problem_setup_dialog_vars(combined_target_anim_name) ""

  set list_only 0
  set force 0
  problem_setup_dialog_generate_files\
    $problem_name combined_files $force $list_only
}


proc main {argv} {
  global env

  if {[llength $argv]==0} {
    puts stderr ""
    puts stderr {Usage: update_project <prj> [...]}
    puts stderr ""
    exit 1
  }

  foreach prj $argv {
    set name [basename $prj .prj]
    set psf [file join [file dirname $prj] $name $name.psf]
    if {![file exists $psf]} {
      puts stderr "Missing psf: $psf"
      exit 1
    }
    set tempdir [file join [pwd] $prj.temp[pid]]
    file mkdir $tempdir
    set result_code [
      catch {
        set env(BIO_RTTRACK_DIR) $tempdir
        set env(GIANT_RTTRACK_DIR) $tempdir
        generate_files $psf temp
        set params [file join $env(BIO_RTTRACK_DIR) params temp]
        set probdef [exec tagfile -get PROJECT_PROBDEF_FILE $params]
        set limits [exec tagfile -get PROJECT_LIMITS_FILE $params]
        set bodydef [exec tagfile -get PROJECT_BODYDEF_FILE $params]
        set searchparams [exec tagfile -get PROJECT_SEARCH_PARAMS_FILE $params]
        set gfxmodel [exec tagfile -get PROJECT_GFXMODEL_FILE $params]
        set gfxscaling [exec tagfile -get PROJECT_GFXSCALING_FILE $params]
        set pattern [exec tagfile -get PROJECT_PATTERN_FILE $params]
        exec tagfile -set PROBLEM_DEF $probdef $prj $prj
        exec tagfile -set BODY_DEF $bodydef $prj $prj
        exec tagfile -set LIMITS $limits $prj $prj
        exec tagfile -set SEARCH_PARAMS $searchparams $prj $prj
        exec tagfile -set GFX_MODEL $gfxmodel $prj $prj
        exec tagfile -set GFX_SCALING $gfxscaling $prj $prj
        exec tagfile -set PATTERN $pattern $prj $prj
        set prjdir [file join [file dirname $prj] $name]

        process_copycombinedfiles $prjdir $name

        # We need to be using this instead of process_copycombinedfiles
        # but there was a bug in rttrack that made it crash in version 0.20.1
        #exec rttrack -n -params $tempdir/params/temp -updateprj $prj
      } message
    ]
    file delete -force $tempdir
    if {$result_code!=0} {
      puts stderr "Error: $message"
      exit 1
    }
  }
}

main $argv
