# Library functions for the scripting library (scrib)
#
# File: scrib/lib/scriblib.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# to do list:
#   -
#
#--------------------------------------------------------------------
#
if ![catch {::Scriblib}] {
    puts "Scriblib has been initialized already: [info script]"
    return
}
#
# namespace ::ScribLib
#
namespace eval ::ScribLib {
    #
    # public procs start with scrib_*
    #
    namespace export get_exported_procs scrib_lib_source_files_get
    namespace export scrib_* scrib_lib_source_files_set ;#xxx
    #
    proc ::ScribLib::get_exported_procs {} {
	#
	# give the calling function access to all exported commands
	#
	uplevel \#0 namespace import -force ::ScribLib::*
    }
    #
    variable _initialized 0
    variable _lib_files   ""
    variable _this_path   ""
    #
    #
    proc ::ScribLib {{args ""}} {
	return [eval ScribLib::_init $args]
    }
    #--------------------------------------------------
    #
    # this proc if found in a lib/config.tcl file, set the files
    # to be sourced. otherwise [glob lib/*.tcl]
    #
    proc scrib_lib_source_files_set {filelist} {
	#putdebug "scrib_lib_source_files_set filelist: $filelist--\n"
	set ::ScribLib::_lib_files $filelist
	# then source later
    }
    proc scrib_lib_source_files_get {} {	
	if {!$::ScribLib::_initialized && ("" == $::ScribLib::_lib_files)} {
	    _read_config
	}
	return $::ScribLib::_lib_files
    }
}
#
#
#
proc ::ScribLib::_init {{args ""}} {
    global env
    
    variable _initialized
    
    if {1 == $_initialized} { return  0 }
    
    set ::ScribLib::_this_path [_get_lib_path]
    #
    # import some stuff from ScribApp firstxxxxxxxxxx
    #
    #if [namespace exists ::ScribApp] { this is tcl 8.4 }
    set scrib_app_ns_exists [expr {10 == [catch { ::ScribApp::get_exported_procs} res]}]
    #
    # read the config file, which sets the lib file list, etc
    #
    _read_config
    #
    # source all of the files into this namespace
    #
    #putdebug "sourcing lib files $::ScribLib::_lib_files"
    foreach f [scrib_lib_source_files_get] {
	set file [file join ${::ScribLib::_this_path} $f.tcl]
	uplevel \#0 "source {$file}"
    }
    
    set _initialized 1
    #puts "Setup the ScribLib directory from [file tail [info script]]: $_initialized"
    return $_initialized
}
#
#
#
if {"" == [info commands putdebug]} {
    proc ::ScribLib::putdebug {text} {
	if {![info exists _debug] || $_debug} { return }
	if {"" != [info commands mess]} {
	    mess "DEBUG: $text\n" green
	} else {
	    puts "DEBUG:$text"
	}
    }
}
#
#
#
proc ::ScribLib::_get_lib_path {} {
    global env
    
    if {("" != $::ScribLib::_this_path) && [file isdir $::ScribLib::_this_path]} {
        return $::ScribLib::_this_path
    }
    
    if [info exists env(ICEM_SCRIB)] {
        set ::ScribLib::_this_path [file join $env(ICEM_SCRIB) lib]
    } elseif [info exists env(ICEM_ACN)] {
        set alt_path [file join $env(ICEM_ACN) lib scrib]
        if [file isdirectory $alt_path] {
            set env(ICEM_SCRIB) $alt_path
            set ::ScribLib::_this_path [file join $env(ICEM_SCRIB) lib]
        } else {
            # CVS directory structure
            set cvs_path [file join $env(MED_ROOT) .. scrib]
            if [file exists [file join $cvs_path lib scriblib.tcl]] {
                set env(ICEM_SCRIB) $cvs_path
                set ::ScribLib::_this_path [file join $env(ICEM_SCRIB) lib]
            } else {
                puts "Error: cannot initialize ScribLib module without ICEM_ACN or ICEM_SCRIB!\n"
            }
        }
    } else {
        puts "Error: cannot initialize ScribLib module without ICEM_ACN or ICEM_SCRIB!\n"
        return -1
    }
}
#
#
#
proc ::ScribLib::_read_config {} {
    #
    # get a config file name
    #
    set libpath [_get_lib_path]
    
    set config_file [file join $libpath config.tcl]
    
    if {("" != $config_file) && [file isfile $config_file]} {
	#uplevel \#0 source $config_file
	source $config_file
    } else {
	set path_to_check [file join $libpath *.tcl]
	set ::ScribLib::_lib_files [glob -nocomplain $path_to_check]
    }
}
