#!/usr/local/bin/tclsh 

# Setup installation directories

set base_dir /usr/local
set binary_dir bin
set man_dir man
set tess_lib lib

# Setup defaults and configuration

# Set the path to the wish executable
set wish_path /usr/local/bin/wish

# Set the default device
set default_device /dev/fd0

# Set the default Owner permissions
set default_owner_perms 1

# Set the default for updating the modification time
set default_update_mod_time 0

# Set the directory in which temporary files will be created
set temp_dir "/usr/tmp"

# Set the micro-hyper-tools flag
set use_mht 1

#-------------- No changes should be made below this line --------------

# Set the version of tess
# set tess_ver 3.1

# Determine if gzip is available

puts -nonewline "Checking for gzip utilities..."
set retcode [catch {exec gunzip -c tess > /dev/null} compcode]
if { $retcode == 1 } {
    if { [string match "* not in gzip format" $compcode] == 1 } {
        # It looks like the gzip utilities are installed
	set have_gzip 1
	puts "gzip utilities found"
    } else {
	set have_gzip 0
	puts "gzip utilities NOT found"
    }
} else {
    # Somethings very wrong here.
}
 
# Determine if zcat is gzcat

puts -nonewline "Checking for gzcat..."
set retcode [catch {exec gzcat tess > /dev/null} compcode]
if {$retcode == 1 } {
    if { [string match "* not in gzip format" $compcode] == 1 } {
	# It looks like the gzcat command is gzcat
	set have_gzcat 1 
	puts "found"
    } else {
	# zcat is probibly used 
	set have_gzcat 0
	puts "Not found"
    }
} else {
    # Somethings very wrong here.
}

# Make replacements in tess, tkcd, and tkcf

# Create sed script
set sed_script [open sedshmed w]

set safe_path [join [split $wish_path /] \\/]
set wish_path_pat %wish-path%
puts $sed_script [format {s/%s/%s/} $wish_path_pat $safe_path]

set tess_path [join [split $base_dir /] \\/]
set tess_path_pat %tess-path%
puts $sed_script [format {s/%s/%s\/lib\/tess-%s/} \
			$tess_path_pat $tess_path $tess_ver]

set safe_device [join [split $default_device /] \\/]
set device_pat %default-device%
puts $sed_script [format {s/%s/%s/} $device_pat $safe_device]

puts $sed_script [format {s/%s/%s/} "%default-owner-perms%" \
				     $default_owner_perms]

puts $sed_script [format {s/%s/%s/} "%default-update-mod-time%" \
				     $default_update_mod_time]

set safe_temp_dir [join [split $temp_dir /] \\/]
puts $sed_script [format {s/%s/%s/} "%temp-dir%" $safe_temp_dir]

puts $sed_script [format {s/%s/%s/} "%use-mht%" $use_mht]

puts $sed_script [format {s/%s/%s/} "%have-gzip%" $have_gzip]

puts $sed_script [format {s/%s/%s/} "%have-gzcat%" $have_gzcat]

close $sed_script

# Actually run sed on tess.tmpl tkcd.tmpl tkcf.tmpl

puts "Creating tess..."
exec sed -f sedshmed tess.tmpl > tess
puts "Creating tkcd..."
exec sed -f sedshmed tkcd.tmpl > tkcd
puts "Creating tkcf..."
exec sed -f sedshmed tkcf.tmpl > tkcf

# Make the new files executable
exec chmod +x tess tkcd tkcf

# Remove the sed script file

exec rm sedshmed

proc install_files {copy_files install_dir errors} {
    upvar $errors errs
    set install_dir_exists [file exists $install_dir]
    set install_dir_isdirectory [file isdirectory $install_dir]
    set error_msgs ""

    if { $install_dir_exists != 0 } {
	# At least a file exists of the correct name
	if { $install_dir_isdirectory != 0 } {
	    # The file is a directory, copy the files
	    if { [file writable $install_dir] != 0 } {
		foreach file $copy_files {
		    if { [file exists $install_dir/$file] == 1 } {
			catch {exec mv $install_dir/$file \
					$install_dir/$file.old}
		    }
		}
		set copy_ok [catch {[eval [concat exec cp $copy_files \
				$install_dir]]} msg]
		if { $copy_ok == 0 } {
		    # The copy failed
		    lappend error_msgs $errs(copy_failed)
		}
	    } else {
		# The directory is not writable
		lappend error_msgs $errs(no_write_dir)
	    }
	} else {
	    # The named directory is not a directory
	    lappend error_msgs $errs(not_a_dir)
	}
    } else {
	# The requested directory does not exist, try to create it
	catch {exec mkdir $install_dir} mkdir_error
	if {$mkdir_error == ""} {
	    # The directory was created copy the files
	    set copy_ok [eval [concat exec cp $copy_files $install_dir]]
	    if { $copy_ok == 0 } {
		# The copy failed
		lappend error_msgs $errs(copy_failed)
	    }
	} else {
	    lappend error_msgs $errs(cannot_create)
	}
    }
    return $error_msgs
}

# Install the files in the requested directories

proc create_dir {dir_path} {
    set dir_exists [file exists $dir_path]
    set dir_isdirectory [file isdirectory $dir_path]

    if {$dir_exists == 1} {
	# The directory exists, make sure it is a directory
        if {$dir_isdirectory == 1} {
	    set results 1
	} else {
	    set results 0
	    puts "$dir_path exists but is not a directory."
	}
    } else {
	# The directory does not exist
	catch {exec mkdir $dir_path} mkdir_error
	if {$mkdir_error == ""} {
	    # The directory was created
	    set results 1
	} else {
	    set results 0
	    puts "$dir_path could not be created."
	}
    }
    return $results
}

# If every thing is ok so far try to create the installation directories

# Try to create the bin directory if it does not exist
set ok_to_continue [create_dir $base_dir/$binary_dir]

# Try to create the man directory if it does not exist
if {$ok_to_continue == 1} {
    set ok_to_continue [create_dir $base_dir/$man_dir]
    if {$ok_to_continue == 1} {
	set ok_to_continue [create_dir $base_dir/$man_dir/man1]
    }
}

# Try to create the tess lib directory if it does not exist
if {$ok_to_continue == 1} {
    set ok_to_continue [create_dir $base_dir/$tess_lib]
    if {$ok_to_continue == 1} {
	set ok_to_continue [create_dir $base_dir/$tess_lib/tess-$tess_ver]
    }
}

# Install tess, tkcd, and tkcf
if {$ok_to_continue == 1} {
    set install_dir $base_dir/$binary_dir
    set files [list tess tkcd tkcf]
    set errors(copy_failed) "Unable to copy $files to $install_dir"
    set errors(no_write_dir) \
	"The directory $install_dir is not writable. Files not installed."
    set errors(not_a_dir) \
	"The install directory is not a directory.  Files not installed."
    set errors(cannot_create) \
	"The install directory could not be created. Files not installed."

    puts "Installing executables..."
    set error_msgs [install_files $files $install_dir errors]
    if {$error_msgs != ""} {
	puts $error_msgs
	set ok_to_continue 0
    }
}

# Install man pages only override the copy_failed error message
if {$ok_to_continue == 1} {
    cd doc
    set install_dir $base_dir/$man_dir/man1
    set files [list tess.1 tkcd.1 tkcf.1]
    set errors(copy_failed) \
	"Unable to copy $files to $install_dir"

    puts "Installing man pages..."
    set error_msgs [install_files $files $install_dir errors]
    if {$error_msgs != ""} {
	puts $error_msgs
	set ok_to_continue 0
    }
}

# Install tess_lib only override the copy_failed error message

if {$ok_to_continue == 1} {
    set install_dir $base_dir/$tess_lib/tess-$tess_ver
    set files [list disjoint.tk fileselect.tcl read.tcl tessdlg.tk tclIndex]
    set errors(copy_failed) \
	"Unable to copy $files to $install_dir"

    cd ../library
    puts "Installing tess lib files..."
    set error_msgs [install_files $files $install_dir errors]
    if {$error_msgs != ""} {
	puts $error_msgs
	set ok_to_continue 0
    }
}

if {$ok_to_continue == 1} {
    puts " "
    puts "Installation complete."
} else {
    puts " "
    puts "Installation did not complete.  See previous error messages."
    puts "The most likely cause is the target directories do not have"
    puts "proper write permissions for the user executing the install"
    puts "program.  Running the install program as root should allow"
    puts "completion of the installation."
}
# Go back home

cd ..
exit 0

