# $Header: /usr/cvsroot/dotNET/GemEqApp/SecsServer.tcl,v 1.2 2004/09/10 13:59:09 hume Exp $
#
# startup a Tcl/Tk shell that can be used for instantiating
# host or equipment interfaces, with or without Hume GEM application
# logic
#
# $Log: SecsServer.tcl,v $
# Revision 1.2  2004/09/10 13:59:09  hume
# Added more tests, tweaked some comments and error handling.
#
# Revision 1.1.1.1  2004/09/07 19:03:13  hume
# First checkin.
#



# startup functions
# (*) be a DMH server, responding to message commands
# so client(s) can talk to us
# (*) be able to show Datahub table GUI, DMH status window for debugging
# (*) have GEM application directories on the auto_path in case
#  client wants to use GEM application procedures
# (*) create SQL table schema that would work for either Hume
#    GEM host or equipment application logic in case client
#    wants to use the GEM applications
# (*) tables can be customized by user to have additional
#     columns
# (*) security - groupname used by server
#     can be created dynamically and not known remotely
#
# assumptions -
#  (1) tcl/tk shell is spawned/exec'd by external code 
#      (-notk is used in production to hide the windows)
#     exec dmh_wish.exe -notk -f SecsServer.tcl -tclargs "ServerStart <group>" 
#  (2) this file is sourced one time at start
#  (3) external code makes sure there is not already a running
#     server for this group

#
# This procedure is similar to eq_server_init in gem/equip/eq_server.tcl.
# Its an updated version for .NET.  We have simplified the logic by
# moving Remote Commands, Processing State, and Terminal Displays
# completely into .NET code instead of having some logic in Tcl and
# some logic in .NET.
#
proc ServerStart {DMHGroupName} {
    global auto_path
    dmh_import  ;# bring ::dmh into main namespace 
    mbx server $DMHGroupName
    # respond to datahub SQL commands sent to mailbox SERVER_SQL
    mbx whenmsg SERVER_SQL mbx_SQL
    # respond to Tcl commands sent to mailbox SERVER_RPC
    mbx whenmsg SERVER_RPC mbx_RPC
    # have Tcl background errors sent as messages to DMH mailbox TCL_ERROR
    proc bgerror {msg} { mbx put TCL_ERROR "$msg\n$::errorInfo" }

    # set the auto_path so we can find Tcl procedures
    ei_auto_path
    # now you can use "eq_server_tabwin" to display the hub GUI
    # (see ...gem/equip/eq_server.tcl)
    # in case the Hume GEM applications are to be used, create
    # merged SQL tables that can be used for either host or equip
    merged_schema

    # if the Hume GEM equipment logic is used,
    # have spooling alerts sent as DMH messages
    # call auto_load so we replace the usual Tcl version and not have it replace our version
    auto_load eq_operator_alert
    proc eq_operator_alert {spname MSGID args} {
        mbx put ${spname}_ALERT [list $MSGID $args]
        }

    # if we are started without -notk, must be development not production
    # so show the SQL table GUI which has menu items for DMH Status and a console
    if { !$::dmh::notk } {
        eq_server_tabwin
        # but iconify it so its less distracting
        wm iconify .
        }
    }

proc ei_auto_path {} {
    # Add this directory and others to the path that is searched for
    # unknown Tcl procedures (see HTML help for Tcl Commands "library")
    #
    # If you need to customize any of the gem application source code, copy the file(s)
    # to this directory,  rebuild the tclIndex, and your version 
    # will be found and used instead of the HIS version.
    #
    global auto_path env
    # pwd == directory where this script is, not necessarily [pwd]
    set pwd [file dirname [info script]]
    if { $pwd == "." } { set pwd [pwd] }
    # assume Hume tools are installed with DMH_BIN defined or default dir
    if { [info exists env(DMH_BIN)] } {
        set gemdir [file join $env(DMH_BIN) .. gem]
        }\
    else {
        set gemdir /usr/local/gem
        }
    if { [lsearch -exact $auto_path $pwd] < 0 } {
        set path(pwd) $pwd
        foreach subdir {host equip lib custom} {
            set path($subdir) [file join $gemdir $subdir]
            if { ![file isdirectory $path($subdir)] } {
               error "Can't find \"$subdir\" GEM application subdirectory."
                }
           }

        # This script dir is the custom directory, add it to the auto_path first.
        # ignoring gem/custom directory
        foreach dir {pwd host equip lib} {
            # we only add dir to the auto_path if its properly indexed
            if { [file exists [file join $path($dir) tclIndex]]} {
                lappend auto_path $path($dir)
                }
            }
        }
    }



