#---------------------------------------------------------------------------
# sea initialization script
#---------------------------------------------------------------------------

if {[info exists serverport]} {

# called by sea.tcl which defines
# serverport, instrument, logbase and logger2

} else {

# called directly

  set serverport 8641
  set homedir [split $env(HOME) /]
  set instrument [lindex $homedir [expr [llength $homedir] - 1]]
  set logbase $env(HOME)/sea
  exe echo 1
}

if {[sicsdescriptor loglisten] eq "loglisten"} {
  loglisten commandlog 1000
  loglisten timestamps m
}
logconfig logtemplate $logbase/log/log
#mongoconfig open mongodb://logwriter:sinqsics@mpc1965:27017/?authSource=admin sea_$instrument
#parhistconfig sea_$instrument

ServerOption LogFileBaseName $logbase/stdlog
ServerOption LogFileDir $logbase/log
#LogFileDir is the directory where the command log is going

ServerOption LoggerDir  $logbase/logger
#where the logger files should be written

ServerOption HistoryDir  $logbase/history
#where the FRAPPY-like history files should be written

ServerOption LogReaderPath $logbase/logger:$env(HOME)/sicslogger
#where the logger files chould be read from

set statusfile $logbase/status/seastatus.tcl
ServerOption statusfile $statusfile

ServerOption ServerPort $serverport
# the port number the server is going to listen at. The client MUST know
# this number in order to connect.

#interrupt port not needed for SEA, but required for SICS, subtract 100
ServerOption InterruptPort [expr $serverport - 100]

# Telnet options, subtract 200
ServerOption TelnetPort [expr $serverport - 200]
ServerOption TelWord    sealogin

# allow to call the unix script safeplugin
allowexec [pwd]/plugin/safeplugin
allowexec ping
allowexec dig
allowexec frappy

definealias tem temperature

ServerOption overwriteOwner 1
# allows to run or drive when another client is already running,
# taking over ownership of the executor

SicsUser lnsmanager lnsSICSlns  1
SicsUser seamanager seager      1
SicsUser seauser    seaser      2
SicsUser Spy        007         3

VarMake Instrument Text Internal
Instrument $instrument

MakeDrive
MakeTclInt save_env_object

#SerialInit
#Publish serialport user
#Publish p1 user
#Publish p2 user
#Publish p3 user

#commandlog auto
#commandlog compact 60

makeobject obj_list array
makeobject cfg_env array
makeobject desc_env array
makeobject device_layout array
makeobject graph_shown array
makeobject graph_label array
makeobject graph_units array
makeobject graph_color array
makeobject graph_order array
makeobject device array logged
makeobject seaclient_list array

makeobject vars string
makeobject addon_list array
makeobject obj_dependencies array
makeobject ignore_msg array
makeobject instconfig array

catch {
  source instconfig/${instrument}.tcl
}

# the following items could be moved to the array "device"
makeobject deviceDesc string
makeobject stickDesc string

if {$instrument eq "seaman"} {
  makeobject busy_list array
  makeobject request_list array
  makeobject device_list array
  makeobject hostport_list array
  makeobject rack_list array
  makeobject instr_list array
  makeobject samenv_list array
  makeobject web_ports array
  source seacom.tcl
  source seamancom.tcl
# only for test
  source remob.tcl
} else {
  makeobject ts_busy array
  makeobject device_on array
  source stdsct.tcl
  source seacom.tcl
  source remob.tcl
  stdConfig::make -name seaman -driver seaclient -port samenv:3001 -permanent 1
  startAutodeviceCron
  if {[silent 0 result instconfig stickrot] == 1} {
    # enable stickrot:
    # in sea/tcl/instconfig/<instrument>.tcl add the following line:
    # instconfig makeitem stickrot 1
    source stickrot.tcl
  }
}

restore $statusfile
backup

array_init device name unknown changetime 0 stick_menu {} stick_name {} newdevice {} olddevice {} rack {}
array_init device frappy_u_config 0 frappy_u_stick 0 frappy_u_addon 0 frappy_main {} frappy_stick {} frappy_addons {}

connect_sics
