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

#..........................#
#     GENERIC SERVER       #
#     STARTUP SCRIPT       #
#..........................#

# SET the paths to the package libraries which are used 

source [ file join $env(TCLTOOLS_HOME) TclToolInits.tcl ]
TclToolInits UTILS UDF UDFDB 
                                                                                
lappend auto_path [file join $env(UDFTOOL_HOME) Archive]
package require Server
                                                                                
# GET the server configuration parameters.  If there is no server
#   configuration parameters then use defaults. 

set sCFile [file join $env(UDF_HOME) Archives ServerConfig]

if [file exists $sCFile] { set nF 0 } else { set nF 1 } 
if !$nF {
   if [catch {open $sCFile r} fd] { set nF 1 }
}

if $nF {
   set sCFG(log) NO
   set sCFG(logFile) ""
   set sCFG(oMode) w
   set sCFG(error) NO
   set sCFG(errFile) ""
   set sCFG(errMode) w
} else {
   while { [gets $fd LiNe] > 0 } {
      set Flds [split $LiNe "|"]
      set sCFG([lindex $Flds 0]) [lindex $Flds 1]
      set sCFG(_[lindex $Flds 0]) [lindex $Flds 1]
   }
   close $fd
}

# OPEN up the Server Log file.  If none then set the output to stderr.
#   Use this only for real critical things so that not much goes there.

if [string match NO $sCFG(log)] {
   set sCFG(lfd) stderr 
} else {
   if [file exists $sCFG(logFile)]  {
      if [catch {open $sCFG(logFile) $sCFG(oMode)} sCFG(lfd)] {
        set sCFG(lfd) stderr
        puts stderr "CAN'T OPEN $sCFG(logFile) .. using stderr"
      } else { seek $sCFG(lfd) 0 start }
   } else { 
      if [catch {open $sCFG(logFile) w} sCFG(lfd)] {
        set sCFG(lfd) stderr
        puts stderr "CAN'T OPEN $sCFG(logFile) .. using stderr"
      } 
   }
}

if [string match NO $sCFG(error)] {
   set sCFG(efd) stderr 
} else {
   if [file exists $sCFG(errFile)]  {
      if [catch {open $sCFG(errFile) $sCFG(errMode)} sCFG(efd)] {
        set sCFG(efd) stderr
        puts stderr "CAN'T OPEN $sCFG(errFile) .. using stderr"
      } else { seek $sCFG(efd) 0 start }
   } else { 
      if [catch {open $sCFG(errFile) w} sCFG(efd)] {
        set sCFG(efd) stderr
        puts stderr "CAN'T OPEN $sCFG(errFile) .. using stderr"
      } 
   }
}

# READ in all of the defined archive codes 

SVreadACodes

# OBTAIN a list of all of the defined archives

set sFile [file join $env(UDF_HOME) Archives ServerInfo]

if [catch {open $sFile r} fd] {
  puts $sCFG(lfd) "CAN'T OPEN $sFile .. NO SERVER INFORMATION"
  exit
} else {
   gets $fd LiNe
   gets $fd LiNe
   set aN 0
   while { [gets $fd LiNe] >= 0 } { 
     scan $LiNe "%s%s%s" sDEF($aN,iD) sDEF($aN,Port) sDEF($aN,aDir) 
     incr aN
   }
   set sDEF(aN) $aN
   close $fd
}

# ACTIVATE the server for each of the defined archives

set openPort ""
set openRet ""

if { $aN > 0 } {
   SVautoIncoming $sDEF(0,aDir)
   set ReqCh(Ch0) [socket -server SVreqAccept $sDEF(0,Port) ]
   lappend openPort $sDEF(0,Port)
   lappend openRet $ReqCh(Ch0)
}

for { set I 1 } { $I < $aN } { incr I } {
  SVautoIncoming $sDEF($I,aDir)
  set oP [lsearch $openPort $sDEF($I,Port)]
  if { $oP < 0 } {
     set ReqCh(Ch$I) [socket -server SVreqAccept $sDEF($I,Port) ]
     lappend openPort $sDEF($I,Port)
     lappend openRet $ReqCh(Ch$I)
  } else { set ReqCh(Ch$I) [lindex $openRet $oP] }
}

if { $aN > 0 } { vwait forever }
