#!/usr/bin/sh
#\
exec tclsh "$0" ${1+"$@"}

##
# @file
# @ingroup ngcdcsSeqTcl
# @copyright
#   SPDX-FileCopyrightText: 2026 European Southern Observatory (ESO)
#   SPDX-License-Identifier: LGPL-3.0-only
#

##
# Add application specific library
#
proc SETLIB {libList} {
    global auto_path env gvar
    
    if {[catch {set VLTROOT $env(VLTROOT)}]} {set VLTROOT undefined}
    if {[catch {set INTROOT $env(INTROOT)}]} {set INTROOT undefined}
    if {[catch {set MODROOT $env(MODROOT)}]} {set MODROOT undefined}
    if {[catch {set HOME $env(HOME)}]} {set HOME undefined}
    
    foreach ele $libList {
	set p "lib$ele\.tcl"
	if {[file exists ../lib/$p] && [file isdirectory ../lib/$p]} {
	    set keepWd [pwd]
	    cd ../lib/$p
	    set dir [pwd]
	    cd $keepWd
	    unset keepWd
	} elseif {[file exists $MODROOT/lib/$p] && 
		  [file isdirectory $MODROOT/lib/$p]} {
	    set dir $MODROOT/lib/$p
	} elseif {[file exist $INTROOT/lib/$p] && 
		  [file isdirectory $INTROOT/lib/$p]} {
	    set dir $INTROOT/lib/$p
	} elseif {[file exists $VLTROOT/lib/$p] && 
		  [file isdirectory $VLTROOT/lib/$p]} {
	    set dir $VLTROOT/lib/$p
	} else {
	    puts stderr "Tcl library $p not found"; 
	    exit 1;
	}
	
	if {[lsearch -exact $auto_path $dir] == -1} {
	    set auto_path [linsert $auto_path 0 $dir]
	}
    }
}

##
# Send debug message
#
proc DEBUG {msg} {
    global seq_dbg
    if {$seq_dbg} {
	puts "* $msg"
	flush stdout
    }
}

##
# Initialize: get parameters, subroutine execution times and
#             pattern execution times
#
proc ngcseqInit {} {
    global argv gvar svar time_r time_p tcl_precision seq_idx seq_dbg

    fconfigure stdout -translation lf
    set tcl_precision 17

    set seq_idx [lindex $argv 0]
    set seq_dbg [lindex $argv 1]
    if {$seq_dbg == ""} {
	set seq_dbg 0
    }

    puts "!"
    flush stdout

    set str ""
    while {$str != "!" && $str != "*"} {
	set str [gets stdin]
	if {[eof stdin]} {
	    puts stderr "unexpected end of script"
	    exit 1
	}
	if {$str == "*"} {
	    exit 0
	}
	if {[string index $str 0] == "#"} continue
	if {$str != "!"} {
	    set name [scan $str "%s"]
	    set l [string length $name]
	    incr l
	    set value [string range $str $l end]
	    if {$value != ""} {
		set svar($name) $value
	    }
	}
    }

    set str ""
    while {$str != "!" && $str != "*"} {
	set str [gets stdin]
	if {[eof stdin]} {
	    puts stderr "unexpected end of script"
	    exit 1
	}
	if {$str == "*"} {
	    exit 0
	}
	if {[string index $str 0] == "#"} continue
	if {$str != "!"} {
	    if {[scan $str "%s %f" name value] == 2} {
		set time_r($name) $value
	    }
	}
    }

    set str ""
    while {$str != "!" && $str != "*"} {
	set str [gets stdin]
	if {[eof stdin]} {
	    puts stderr "unexpected end of script"
	    exit 1
	}
	if {$str == "*"} {
	    exit 0
	}
	if {[string index $str 0] == "#"} continue
	if {$str != "!"} {
	    if {[scan $str "%s %f" name value] == 2} {
		set time_p($name) $value
	    }
	}
    }
}

##
# Receive and execute application script
#
proc ngcseqScript {} {
    global gvar svar time_r time_p seq_idx
    set str ""
    set thisScript ""
    while {$str != "!"} {
	set str [gets stdin]
	if {[eof stdin]} {
	    puts stderr "unexpected end of script"
	    exit 1
	}
	if {$str != "!"} {
	    set thisScript "$thisScript$str\n"
	}
    }
    
    eval $thisScript
}

##
# Return updated parameters to the calling application
#
proc ngcseqReturn {} {
    global gvar svar time_r time_p

    foreach i [lsort [array names svar]] {
	puts "$i $svar($i)" 
    }
    puts "!"
    flush stdout
}

ngcseqInit
ngcseqScript
ngcseqReturn
exit 0

#
# ___oOo___
