#!/bin/csh -f
#
#	install: csh script version of the BSD install utility.
#
#	usage:	install [-c] [-m mode] [-o owner] [-g group] [-s] src dest
#		-s indicates stripping of binaries after installation
#		-c forces copy mode; included for backwards compatibility
#		"dest" can be a filename or a directory name
#
#	Copyright 1988 - University Corportation for Atmospheric Research
#	Summer 1988 - Don Middleton - Graphics Group
#

set mover=/bin/cp
set chmod="/bin/chmod 755"
#set chown="/etc/chown root"
#set chgrp="/bin/chgrp wheel"
#set strip="strip"

while ($#argv > 0)

	switch ($1)
		case "-c":
			set mover=/bin/cp
			breaksw

		case "-m":
			set chmod="/bin/chmod $2"
#
# If on Windows, and the file is an executable, then
# the file will be called $2.exe.
#
			if (-f {$2}.exe) then
				set chmod="/bin/chmod {$2}.exe"
			endif
			shift
			breaksw

		case "-o":
			set chown="/etc/chown $2"
			shift
			breaksw

		case "-g":
			set chgrp="/bin/chgrp $2"
			shift
			breaksw

		case "-s":
			set strip="strip"
			shift
			breaksw

		default:
			if (! $?srcfile ) then
				set srcfile=$1
			else
				if (! $?dest) then
					set dest=$1
				else
					echo "install: Too many arguments"
					exit 1
				endif
			endif
	endsw
	shift
end

if (! $?srcfile) then
	echo "$0 : No source file specified"
	exit 1
endif

if (! $?dest ) then
	echo "$0 : No destination specified"
	exit 1
endif

# avoid tragedies like installing onto an existing file

if ( -d $srcfile ) then
	echo "$0 : Source cannot be a directory"
endif

if ("$srcfile" == "$dest" || "$dest" == ".") then
	echo "$0 : Cannot move a file onto itself"
	exit 1
else

if (-d $dest) then
	if (-f {$srcfile}.exe) then
		set newfile="$dest/{$srcfile}.exe"
	else
		set newfile="$dest/$srcfile"
	endif
else
	set newfile=$dest
endif

# finally perform the installation

$mover $srcfile $newfile

if ($?chown) then
	$chown $newfile
endif

if ($?chmod) then
	$chmod $newfile
endif

if ($?chgrp) then
	$chgrp $newfile
endif

if ($?strip) then
	$strip $newfile
endif
