#!/bin/sh
# the next line restarts using wish \
exec wish $0 $@

source [ file join $env(TCLTOOLS_HOME) TclToolInits.tcl ]
TclToolInits UTILS

TUreadUDFrc udfPilot AnS

set dD  H
set dL  1000

for { set I 0 } { $I < $argc } { incr I 2 } {
   set sW [lindex $argv $I]
   set V  [lindex $argv [expr $I + 1]]
   switch -exact -- $sW {
      -D {  set dD $V }
      -L {  set dL $V }
   }
}
                                                                                
###########################################################
#   The UDF launcher program
###########################################################
                                                                                
source [ file join $env(TCLTOOLS_HOME) TclToolInits.tcl ]
TclToolInits GUI GUIUTILS UTILS 

if [string match window* $tcl_platform(platform)] {
   set NeedsWish 1
} else { set NeedsWish 0 }

#   VARIABLES pointing to the application home and the icon home directorys

set BHOME [file join $env(UDFTOOL_HOME) bin]
set IHOME [file join $env(UDFTOOL_HOME) UDFPilot]

# THIS is a list of the applications which can be lanunched. 
#    BiN is a list of applications we know about
#    Run is the program associated with each icon which may be changed
#       according to what is found on the system.
#    BnM is a list of names put under the udfPilot icons
#    IcoN is the list of icons 

set BiN [list dBAsk udfLibrarian ADump UDFplot SetGUIrc IdL aTool DocS VEdit QuiT]
set RuN [list dBAsk udfLibrarian CONTROL CONTROL SetGUIrc idl aTool DocS VEdit exit]
set BnM [list dBAsk Librarian ADump UDFplot SetGUIrc UDFtoIDL aTool Docs VEdit QUIT ]
set IcoN [list iconDBASK iconLIB iconDUMP iconUDFPLOT iconGUIRC iconUDFIDL \
               iconATOOL iconDOCS iconVEDIT iconQUIT ]

GUIprefsRead
GUIFrame .gmTOP -1 pack|top||x|true

set nB [llength $BiN]
set bI ""
set bN ""
set bE ""

for { set I 0 } { $I < $nB } { incr I } {

   set iTem [lindex $BiN $I]
   switch -exact -- $iTem {
      DocS    { 
         if [info exists AnS(BroWseR)] {
            set IndeX [file join $env(UDFDOC_HOME) Index.html]
            if [file exists $IndeX] { 
               set IndeX \{[file nativename $IndeX]\}
               lappend bI [file join $IHOME [lindex $IcoN $I]]
               lappend bE "exec $AnS(BroWseR) $IndeX &"
               lappend bN [lindex $BnM $I]
            }
         }
      }
      IdL    { 
         set IDL [file join $env(UDFTOOL_HOME) UDFToIDL]
         if ![info exists AnS(IDL)] { set AnS(IDL) NEEDSTERM|idl }
         if [file isdirectory $IDL] {
            set InPuT [split $AnS(IDL) "|"]
            set nE [llength $InPuT]
            set eXList ""
            if [string match NEEDSTERM [lindex $InPuT 0]] {
               if [info exists AnS(TerM)] {
                  for { set J 1 } { $J < $nE } { incr J } { 
                      lappend eXList [lindex $InPuT $J]
                  }
                  lappend bI [file join $IHOME [lindex $IcoN $I]]
                  lappend bE "exec $AnS(TerM) $eXList &"
                  lappend bN [lindex $BnM $I]
               }
            } else {
               for { set J 0 } { $J < $nE } { incr J } { 
                   lappend eXList [lindex $InPuT $J]
               }
               lappend bI [file join $IHOME [lindex $IcoN $I]]
               lappend bE "exec $eXList &"
               lappend bN [lindex $BnM $I]
            }
         }
      }

      QuiT    { 
         lappend bI [file join $IHOME [lindex $IcoN $I]]
         lappend bE [lindex $RuN $I]
         lappend bN [lindex $BnM $I]
      }

      default {
         if [file exists [file join $BHOME $iTem]] {
            set OK 1
            set eXFile \"[file join $BHOME [lindex $RuN $I]]\"
         } elseif [file exists [file join $BHOME $iTem.tcl]] {
            set OK 1
            if $NeedsWish {
               set eXFile "wish \"[file join $BHOME [lindex $RuN $I].tcl]\""
            } else { set eXFile \"[file join $BHOME [lindex $RuN $I].tcl]\" }
         } elseif [file exists [file join $BHOME $iTem.exe]] {
            set OK 1
            set eXFile \"[file join $BHOME [lindex $RuN $I].exe]\"
         } else { set OK 0 }

         if $OK {
            lappend bI [file join $IHOME [lindex $IcoN $I]]
            lappend bE "exec $eXFile &"
            lappend bN [lindex $BnM $I]
         }
      }
   }
}

set nB [llength $bI]
if [string match $dD "H"] {
   set nC $dL
   if { $nC > $nB } { set nC $nB }
   set nR [expr int($nB / $dL)]
   if { $nR == 0 } { set nR 1 }
   if { [expr $nC * $nR] < $nB } { incr nR }
} else {
   set nR [expr $dL * 2]
   if { $nR > $nB } { set nR $nB }
   set nC [expr int($nB / $dL)]
   if { $nC == 0 } { set nC 1 }
   if { [expr $nC * $nR] < $nB } { incr nC }
} 

set nR [expr $nR * 2]

set N 0
for { set I 0 } { $I < $nR } { incr I 2 } {
   for { set J 0 } { $J < $nC } { incr J ; incr N } {
      if { $N >= $nB } { continue } 
      set iC [lindex $bI $N]
      set lN [lindex $bN $N]
      GUIbuttonPush .gmTOP.b$N "" grid|$I|$J [lindex $bE $N] 
      GUIbuttonPush .gmTOP.lb$N $lN grid|[expr $I + 1]|$J [lindex $bE $N]
      image create photo icon$N -file $iC.ppm
      .gmTOP.b$N configure -image icon$N
   }
}
