#!/bin/sh
#\
exec seqWish -f "$0" ${1+"$@"}
#*******************************************************************************
# E.S.O. - VLT project
# 
# guidecamMain
# 
# Created by vltMakeTclScript: forsmgr 08.06.12 22:02:17 
# 
# 
#        !!!!!!!!!!!!!!  DO NOT MANUALLY EDIT THIS FILE  !!!!!!!!!!!!!!
# 
#-------------------------------------------------------------------------------

#
#  Get directory root of VLT, integration, current module.
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}

#
#  Adjust auto_path for each library
#  (this permits to find the resp. tclIndex files)

# 
# >>>guidecam<<<
if {[file exists ../lib/libguidecam.tcl] && [file isdirectory ../lib/libguidecam.tcl]} {
    # the current relative path can be affected by any change of working
    # directory within the application. 
    # To prevent possible mess, the RELATIVE path is changed into absolute
    set keepWd [pwd]
    cd ../lib/libguidecam.tcl
    set dir [pwd]
    cd $keepWd
    unset keepWd
} elseif {[file exists $MODROOT/lib/libguidecam.tcl] && [file isdirectory $MODROOT/lib/libguidecam.tcl]} {
    set dir $MODROOT/lib/libguidecam.tcl
} elseif {[file exist $INTROOT/lib/libguidecam.tcl] && [file isdirectory $INTROOT/lib/libguidecam.tcl]} {
    set dir $INTROOT/lib/libguidecam.tcl
} elseif {[file exists $VLTROOT/lib/libguidecam.tcl] && [file isdirectory $VLTROOT/lib/libguidecam.tcl]} {
    set dir $VLTROOT/lib/libguidecam.tcl
} else {
    puts stderr  "guidecamMain - ERROR:"; 
    puts stderr  "   Tcl library libguidecam.tcl not found. Unable to continue."; 
    puts stderr  ""; 
    exit 1;
}
if {[lsearch -exact $auto_path $dir] == -1} {
    set auto_path [linsert $auto_path 0 $dir]
}

# 
# >>>panPublic<<<
if {[file exists ../lib/libpanPublic.tcl] && [file isdirectory ../lib/libpanPublic.tcl]} {
    # the current relative path can be affected by any change of working
    # directory within the application. 
    # To prevent possible mess, the RELATIVE path is changed into absolute
    set keepWd [pwd]
    cd ../lib/libpanPublic.tcl
    set dir [pwd]
    cd $keepWd
    unset keepWd
} elseif {[file exists $MODROOT/lib/libpanPublic.tcl] && [file isdirectory $MODROOT/lib/libpanPublic.tcl]} {
    set dir $MODROOT/lib/libpanPublic.tcl
} elseif {[file exist $INTROOT/lib/libpanPublic.tcl] && [file isdirectory $INTROOT/lib/libpanPublic.tcl]} {
    set dir $INTROOT/lib/libpanPublic.tcl
} elseif {[file exists $VLTROOT/lib/libpanPublic.tcl] && [file isdirectory $VLTROOT/lib/libpanPublic.tcl]} {
    set dir $VLTROOT/lib/libpanPublic.tcl
} else {
    puts stderr  "guidecamMain - ERROR:"; 
    puts stderr  "   Tcl library libpanPublic.tcl not found. Unable to continue."; 
    puts stderr  ""; 
    exit 1;
}
if {[lsearch -exact $auto_path $dir] == -1} {
    set auto_path [linsert $auto_path 0 $dir]
}
catch {unset dir}
###########################################################
#                                                         #
# if the current shell is including TK, i.e. option       #
# command is known, then set-up X resources               #
#                                                         #
###########################################################

if {[info command option] != ""} {
    
    #
    # Load resources:
    # merge possible Xresource files in the following order:
    #           - /vlt/JAN2006/CCSLite/app-defaults  
    #           - /vlt/FORS2/INTROOT/app-defaults  
    #           - /app-defaults  
    #           - /home/forsmgr/                 
    #           - ../app-defaults        
    #
    if {[file exists $VLTROOT/app-defaults/XguidecamMain]} {
        option readfile $VLTROOT/app-defaults/XguidecamMain 
    }
    if {[file exists $INTROOT/app-defaults/XguidecamMain]} {
        option readfile $INTROOT/app-defaults/XguidecamMain 
    }
    if {[file exists $MODROOT/app-defaults/XguidecamMain]} {
        option readfile $MODROOT/app-defaults/XguidecamMain 
    }
    if {[file exists $HOME/XguidecamMain]} {
        option readfile $HOME/XguidecamMain 
    }
    if {[file exists ../app-defaults/XguidecamMain]} {
        option readfile ../app-defaults/XguidecamMain 
    }
}
#-------------------------------------------------------------------
#                    END OF STANDARD PROLOGUE 
#-------------------------------------------------------------------

#vvvvvvvvvvvvvvvvv  src/guidecamMain.tcl  vvvvvvvvvvvvvvvvvvvvvv
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: guidecamMain.tcl 228968 2012-05-30 17:26:16Z pbaksai $"
#
# who       when        what
# --------  ----------  --------------------------------------------------------
# pbaksai   2012/05/30  Reduced dependency on VLT environment.
# pbaksai   2012/05/19  Added curly braces to expressions and if
# mcomin    21/11/1996  Created
#
#*******************************************************************************
#   NAME
# 
#   SYNOPSIS
# 
#   DESCRIPTION
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS     
#
#-------------------------------------------------------------------------------
#
#puts "DBG> Main argv = $argv"
#
# Take care of VLT versions and name
#
set Id dummy
set myVersion "@(#) $Id: guidecamMain.tcl 228968 2012-05-30 17:26:16Z pbaksai $"

if { [lcontain $argv -camera] } {
    set gvar(lite) 0
} else {
    set gvar(lite) 1
}

set par(RELEASE)    [string trim [lindex $myVersion 3] "v +"] 
set par(MY_NAME)    [file tail $argv0]

if {$gvar(lite)} {
    set par(TITLE)      "Guidecam PILMOS - Release $par(RELEASE)"
} else {
    set par(TITLE)      "Guidecam - Release $par(RELEASE)"
}

# Set some dummy variables to be able to use panel procedures
panWrapAround

if {[info exists env(SKYCATDIR)]} {

   # Kept here for backwards compatibility

   lappend auto_path $env(SKYCATDIR)/lib/tclutil
   lappend auto_path $env(SKYCATDIR)/lib/rtd
   lappend auto_path $env(SKYCATDIR)/lib/cat
} else {
   lappend auto_path $env(VLTROOT)/lib/tclutil
   lappend auto_path $env(VLTROOT)/lib/rtd
   lappend auto_path $env(VLTROOT)/lib/cat
}

lappend auto_path $env(VLTROOT)/lib/libuifClass.tcl

# Import package namespaces

catch {package require Rtd} tmp 
puts "Using version $tmp of module rtd"
catch {package require Cat} tmp 
puts "Using version $tmp of module cat"

if {$tcl_version >= 8.0 } {
   # Patch in order to use private version of AstroCat widgets
   namespace forget "::cat"
   namespace forget "::cat::AstroCat"
}

#
# Check if other env. variables exist
#

set tmp ""
if {! $gvar(lite)} {
    if {![info exists env(CCDDID)]  }   { lappend tmp CCDDID  }
    if {![info exists env(INS_ROOT)] }  { lappend tmp INS_ROOT }
    if {![info exists env(INS_USER)] }  { lappend tmp INS_USER }
}

if {![lempty $tmp]} {
   set msg "Define the following Env. Variables\n$tmp"
   tk_dialog .dialog "Dialog Box" $msg info 0 OK
   exit 1
}

#-------------------------------------------------------------------
# create the skycat logo :
#
# create a dummy rtdimage to make sure the colormap is initialized
# before the logo is created to avoid color problems...
#-------------------------------------------------------------------

if { [set filePath [findVLTFile bitmaps/skycat-logo.gif]] == "" } {
    puts "DBG> logo image cannot be found"
} else {
    if { [info exists env(SKYCAT_LOGO)] } {
        set filePath $env(SKYCAT_LOGO)
    } else {
        set filePath "bitmaps/skycat-logo.gif"
    }
}

if { [catch {
          image delete [image create rtdimage]
          set skycat_logo [image create photo -format GIF -file $filePath]
     } msg] } {
    puts "skycat logo image cannot be found"
    exit
}

#-------------------------------------------------------------------
# Interface to VLT
#-------------------------------------------------------------------

set home [pwd]

if {! $gvar(lite)} {
    if { [catch {seq_ccsInit $par(MY_NAME) } ret] } {
	cd $home;
	set tmp "Cannot initialize $par(MY_NAME): $ret\n"
	seq_logData $tmp ;  # Log Error
	puts $tmp; exit 0
    }
}

#
# Variable Initialization to suport the generation of setup files
#

set gvar(setup.0.DET.WIN1.REFX) ""
set gvar(setup.1.DET.WIN1.REFY) ""
set gvar(setup.2.DET.WIN1.ST)   ""
set gvar(setup.3.DET.WIN1.STRX) ""
set gvar(setup.4.DET.WIN1.NX)   ""
set gvar(setup.5.DET.WIN1.STRY) ""
set gvar(setup.6.DET.WIN1.NY)   ""
set gvar(setup.7.DET.WIN1.BINX) ""
set gvar(setup.8.DET.WIN1.BINY) ""

set tcs(checkCmd) CHECK_ALL

# Load dictionary


if { !$gvar(lite) && [slxtclDictionary CCDDCS] == "FAILURE"} {
   tk_dialog .dialog "Dialog Box" "CANNOT LOAD dictionary CCDDCS" info 0 OK
   exit
}
    
#
# Read VLT app-defaults
#

if {[file exists $VLTROOT/app-defaults/Xpanel.common] == 1} {
   option readfile $VLTROOT/app-defaults/Xpanel.common
}

tk appname $par(MY_NAME)

if {[set idx [lsearch $argv "-tcsEnvName"]] != -1} {
   set tcsEnvName  [lindex $argv [expr {$idx +1}]]
   set argv [lreplace $argv $idx [expr {$idx +1}]]
   incr argc -2
}

if {[set idx [lsearch $argv "-agProcName"]] != -1} {
   set agProcName  [lindex $argv [expr {$idx +1}]]
   set argv [lreplace $argv $idx [expr {$idx +1}]]
   incr argc -2
}

if {[set idx [lsearch $argv "-tcsProc"]] != -1} {
   set tcsProc  [lindex $argv [expr {$idx +1}]]
   set argv [lreplace $argv $idx [expr {$idx +1}]]
   incr argc -2
}

if { !$gvar(lite) } {
    puts "tcsEnvName = $tcsEnvName"
    puts "agProcName = $agProcName"
    puts "tcsProc    = $tcsProc"
}

#lappend argv -center 0 
#incr argc 2

#puts "DBG> argv = $argv"

# start the application
::util::TopLevelWidget::start guidecam

#
#___oOo___
#^^^^^^^^^^^^^^^^^  src/guidecamMain.tcl  ^^^^^^^^^^^^^^^^^^^^^^

#--------------------------------------------------------------
#                       End of guidecamMain
#--------------------------------------------------------------
# ___oOo___ 
