# Library functions for the batch_queue library 
#
# File: batch_queue/lib/setup.tcl
#
# $Revision: 392517 $ on $Date: 2004-03-22 20:20:04 -0500 (Mon, 22 Mar 2004) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#---------------------------------------------------------------------
 
if {"" == [info commands mess]} {
    proc mess {text args} {
	puts -nonewline "$text"
    }
}

## Set up Tcl batch-queue package

proc batch_queue_lib_setup { which } {
    
    set bqpath [file dirname [info script]]
    
    set bqlibpath [file join $bqpath lib *.tcl]
    set filelist [glob -nocomplain $bqlibpath]
    if {"" == $filelist} {
	mess "Error: could not get files from the lib path \"$bqlibpath\"\n" red
	return 1
    }
    foreach f $filelist {
	# this could be made to be more reliable...
	#     and actually should not be necessary.   --jka
	if { [string match "icbqs_server_setup.tcl" [file tail $f]] } {
	    continue
	}
	source "$f"
    }
    
    # At last, everything is fine, we can provide the package. note that revision
    # is a cvs-updated rcs string, so the package version is the cvs rev of
    # the batch_queue/lib/batch_queue.tcl interface file --jka
    
    package provide batch_queue [batch_queue_get_version]
    
    return 0
}

## 

proc batch_queue_lib_test { which } {
    
    if [catch { source [file join tests ${which}.tcl] } res] {
	puts "FAILURE: testing batch_queue with ${which}.tcl:\n$res"
	return 1
    }
    return 0
}

# --> Main, of sorts

set setup         "lib"
set test          "" ;# "test_all"
set start_servers ""

foreach {n v} $argv {
    set [string range $n 1 end] $v
}

if {"" != $start_servers} { set setup "" ; set test "" }
if {"" != $test}          { set setup "lib" }

if {("" != $setup) && [batch_queue_lib_setup $setup]} {
    return 1
}

if {("" != $test) && [batch_queue_lib_test $test]} {
    return 1
}

if {"" != $start_servers} {
    if {"" == [info commands batch_queue_icbqs_client_setup_server]} {
	batch_queue_lib_setup lib
    }
    return [batch_queue_icbqs_client_setup_server $start_servers]
}

return 0
