#!/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-03/09: Updated to work with newer logic for handling combined files.
#             Forces the old behavior of storing them in combined directories
#             instead of the rttrack directory.
# 2016-01/13: Changed to use .find_script
# 2011-09/13: Made -list option also show target anim
# 2008-09-26: Added -list option
# 2008-03-05: Changed to take psf instead of prj
# 2007-01-09: Added -mkproblem option
#             Fixed bug where it always created "test" problem.

source [.find_script probsetup_util.tcl]

set problem_name ""
set force 0
set argnum 0
set list_only 0

while {$argnum<$argc} {
  set option [lindex $argv $argnum]
  if {$option=="-mkproblem"} {
    incr argnum
    set problem_name [lindex $argv $argnum]
  } elseif {$option=="-force"} {
    set force 1
  } elseif {$option=="-list"} {
    set list_only 2
    # 2 means show both project and target anim
  } else {
    break
  }
  incr argnum
}

if {$argc-$argnum!=1} {
  puts ""
  puts {Usage: combine_problem_setup [<options>] <psf>}
  puts ""
  puts {Options:}
  puts {  -force             combine even if existing file is newer}
  puts {  -mkproblem <name>  create rttrack params and reference files}
  puts {  -list              list results but do not create any files}
  puts ""
  exit 1
}

proc generate_files {probsetup_path problem_name force list_only} {
  # 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) ""

  problem_setup_dialog_generate_files $problem_name combined_files $force $list_only
}

# If we're using this script, we probably don't want the files stored in
# the rttrack directory.
set env(GIANT_USE_COMBINED_DIR) 1

generate_files [lindex $argv $argnum] $problem_name $force $list_only
