#!/bin/env tclsh

##############################################################
#
# tcl_vars.tcl: script for extracting the values of variables 
# 		used by Tcl/Tk. Used by the setools top-level
#		makefile. Current variables extracted are:
#
#			$tcl_version, $tcl_pkgPath
#
# Copyright (C) 2004 Tresys Technology, LLC
# see file 'COPYING' for use and warranty information 
#
##############################################################

global tcl_version
global tcl_pkgPath
global tcl_library
global argv, argc

proc search_libs {} {
	global tcl_pkgPath
	
	set shared_lib_conf_file "/etc/ld.so.conf"
	set ret_dir ""
	
	# Check the parent dir of the package path directory for tcl.
	set pkgPath_include_dir "[file dirname $tcl_pkgPath]/include"
	
	if {[file exists $pkgPath_include_dir] && [file isdirectory $pkgPath_include_dir] && \
	    [file exists "$pkgPath_include_dir/tcl.h"] && [file readable "$pkgPath_include_dir/tcl.h"] && \
	    [file exists "$pkgPath_include_dir/tk.h"] && [file readable "$pkgPath_include_dir/tk.h"]} {
		set ret_dir $pkgPath_include_dir
	} elseif {[file exists $shared_lib_conf_file] && [file readable $shared_lib_conf_file]} {
		# If we can read the shared lib config file (/etc/ld.so.conf) then parse this file next.
		set rt [catch {set f [open $shared_lib_conf_file]} err]
		if {$rt != 0} {
			tk_messageBox -icon error -type ok -title "Error" \
				-message "Cannot open $shared_lib_conf_file file ($rt: $err)"
		} else {
			gets $f line
			set tline [string trim $line]
			while {[eof $f] == 0 && $tline != ""} {
				set dir "[file dirname $tline]/include"
				if {[file exists $dir] && [file isdirectory $dir] && \
				    [file exists "$dir/tcl.h"] && [file readable "$dir/tcl.h"] && \
				    [file exists "$dir/tk.h"] && [file readable "$dir/tk.h"]} {
					set ret_dir $dir
					break
				}
				gets $f line
				set tline [string trim $line]
			}
			::close $f
		}
	} else {
		# If we get to this point, then look in standard system header location(s)
		set sys_standard_dir "/usr/include"
		set local_standard_dir "/usr/local/include"
		
		if {[file exists $sys_standard_dir] && [file isdirectory $sys_standard_dir] && \
		    [file exists "$sys_standard_dir/tcl.h"] && [file readable "$sys_standard_dir/tcl.h"] && \
		    [file exists "$sys_standard_dir/tk.h"] && [file readable "$sys_standard_dir/tk.h"]} {
			set ret_dir $sys_standard_dir 
		} elseif {[file exists $local_standard_dir] && [file isdirectory $local_standard_dir] && \
		    [file exists "$local_standard_dir/tcl.h"] && [file readable "$local_standard_dir/tcl.h"] && \
		    [file exists "$local_standard_dir/tk.h"] && [file readable "$local_standard_dir/tk.h"]} {
			set ret_dir $local_standard_dir 
		} else {
			set ret_dir none
		}
	} 
	
	return $ret_dir	
}

if {[string equal [lindex $argv 0] "version"]} {
	puts $tcl_version
} elseif {[string equal [lindex $argv 0] "pkgPath"]} {
	puts $tcl_pkgPath
} elseif {[string equal [lindex $argv 0] "search_tcl_libs"]} {
	puts [search_libs]
} elseif {[string equal [lindex $argv 0] "tcl_library"]} {
	puts $tcl_library
} else {
	puts ""
}

# End of tcl script 