#! /usr/local/bin/tclsh 
#==========================================================================
# Installation program for TclVSrpt v1.0
#
# Copyright (c) 1995, Steven B. Wahl
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#==========================================================================

# -- find the installed Tcl library location and if we can write to it.
if {[file exists [info library]]} {
	if {![file writable [info library]]} {
		error \
			"ERROR -- You do not have permission to write into [info library]"
	}
	set path [info library]
} else {
	if {[info exists env(TCL_LIBRARY)]} {
		if {![file writable $env(TCL_LIBRARY)]} {
			error \
				"ERROR -- You do not have permission to write into $env(TCL_LIBRARY)"
		}
		set path $env(TCL_LIBRARY)
	} else {
		error \
			"ERROR -- set TCL_LIBRARY environment variable and try again."
	}
}
# -- make a backup copy of tclIndex
if {[catch "exec /bin/cp $path/tclIndex $path/tclIndex.bak"]} {
	error \
		"ERROR -- could not copy $path/tclIndex to $path/tclIndex.bak"
}
# -- if the TclVS subdirectory does not exist, create it
if {![file exists $path/TclVS]} {
	if {[catch "exec /bin/mkdir $path/TclVS"]} {
		error \
			"ERROR -- could not create directory: $path/TclVS"
	}
	# -- change the permission of TclVS
	if {[catch "exec /bin/chmod 755 $path/TclVS"]} {
		error \
			"ERROR -- could not \"chmod 755 $path/TclVS\""
	}
} else {
	# -- is TclVS directory writable?
	if {![file writable $path/TclVS]} {
		error \
			"ERROR -- You cannot write into directory $path/TclVS"
	}
}
# -- make the fontwidth directory and copy the files into it
if {[file exists $path/TclVS/fontwidths]} {
    if {[catch "exec /bin/rm -r $path/TclVS/fontwidths"]} {
		puts stderr \
			"WARN -- failed attempt to remove TclVSrpt-1.0 fontwidths directory"
	}
}
if {![file exists $path/TclVS/afm]} {
	if {[catch "exec /bin/mkdir $path/TclVS/afm"]} {
		error \
			"ERROR -- could not create directory: $path/TclVS/afm"
	}
	if {[catch "exec /bin/chmod 755 $path/TclVS/afm"]} {
		error \
			"ERROR -- could not \"chmod 755 $path/TclVS/afm\""
	}
} else {
	if {![file writable $path/TclVS/afm]} {
		error \
			"ERROR -- You cannot write into $path/TclVS/afm"
	}
}
set fontfiles [lsort [glob ./afm/*]]
puts stderr "Installing Adobe Font Metrics and encoding files...."
foreach font $fontfiles {
	if {[catch "exec /bin/cp $font $path/TclVS/afm"]} {
		error \
			"ERROR -- Could not install $font into $path/TclVS/afm"
	}
	set fn [exec /bin/basename $font]
	if {[catch "exec /bin/chmod 444 $path/TclVS/afm/$fn"]} {
		error \
			"ERROR -- Could not change permissions on $path/TclVS/afm/$fn"
	}
}
puts stderr "Installing TclVSrpt.tcl and license.terms...."
if {[catch "exec /bin/cp ./TclVSrpt.tcl $path/TclVS/TclVSrpt.tcl"]} {
	error \
		"ERROR -- Could not copy TclVSrpt.tcl to $path/TclVS/TclVSrpt.tcl"
}
if {[catch "exec /bin/chmod 644 $path/TclVS/TclVSrpt.tcl"]} {
	error \
		"ERROR -- Could not \"chmod 644 $path/TclVS/TclVSrpt.tcl"
}
if {[catch "exec /bin/cp ./license.terms $path/TclVS/license.terms"]} {
	error \
		"ERROR -- Could not copy license.terms to $path/TclVS/license.terms"
}
if {[catch "exec /bin/chmod 444 $path/TclVS/license.terms"]} {
	error \
		"ERROR -- Could not \"chmod 444 $path/TclVS/license.terms"
}
puts stderr "Building auto_index..."
set here [pwd]
cd $path
if {[catch "auto_mkindex . TclVS/*.tcl"]} {
	error \
		"ERROR -- Could not create $path/tclIndex"
}
# -- merge this with the old tclIndex
set ofd [open xxx w]
set ifd [open tclIndex.bak r]
# -- copy the comment from the original file
while {![eof $ifd]} {
	set line [gets $ifd]
	if {[llength $line] > 0} {
		puts $ofd $line
	} else {
		break
	}
}
puts $ofd ""
close $ifd
# -- build the auto_index array from these files
set dir $path
global auto_index
source tclIndex.bak
source tclIndex
foreach pname [lsort [array names auto_index]] {
	puts $ofd "set auto_index($pname) \"$auto_index($pname)\""
}
close $ofd
exec /bin/mv ./xxx ./tclIndex
cd $here
puts stderr "TclVSrpt v1.1 Installation complete."

