#!/bin/sh -
#
# @(#)$Id: d444a2c6af8a43c5d4526548e1ce38adb9c28d50 $
#
# The author of this file, J.A. Neitzel <jan (at) etsh (dot) io>,
# hereby grants it to the public domain.
#

#
# Install all of the etsh and tsh command files into the specified
# directory.  If tsh cannot be found by which(1), convert all of the
# tsh command files to run with etsh instead.  It is an error if etsh
# cannot be found by which(1).
#
# If an error is detected, exit with an appropriate diagnostic and
# a non-zero status.
#
# usage: ./INSTALL directory
#    or:
# usage: env INSTALL=/path/to/BSD/GNU/install_utility ./INSTALL directory
#

umask 0022
PROGNAME="`basename $0`"

if test X"$0" != X"INSTALL" -a X"$0" != X"./INSTALL" ; then
	echo "$PROGNAME: $0: Invalid INSTALL pathname." >&2 ; exit 1
fi
if test $# -ne 1 -o X"$1" = X ; then
	( \
	  echo "usage: ./$PROGNAME directory" ;
	  echo "   or:" ;
	  echo "usage: env INSTALL=/path/to/BSD/GNU/install_utility" \
		"./$PROGNAME directory"
	) >&2 ; exit 1
fi
DIR="$1"
case "$DIR" in
tmp-$$)
	echo "$PROGNAME: $DIR: Reserved for internal use only." >&2 ; exit 1
	;;
/|/etc*|/bin|/sbin|/usr/bin|/usr/sbin|/opt*|/var*)
	echo "$PROGNAME: $DIR: May result in a system conflict."  >&2
	echo "Try a subdirectory of /usr/local or $HOME instead." >&2 ; exit 1
	;;
*)
	if echo "$DIR" | grep -v '^/' >/dev/null ; then
		echo "$PROGNAME: $DIR: Absolute directory required." >&2; exit 1
	fi
	if test -d "$DIR" -a ! -w "$DIR" ; then
		echo "$PROGNAME: $DIR: Permission denied." >&2 ; exit 1
	fi
	;;
esac

if ! which which >/dev/null 2>&1 ; then
	echo "$PROGNAME: which: Not found." >&2 ; exit 1
fi
if which . >/dev/null 2>&1 ; then
	echo "$PROGNAME: which: Invalid results." >&2 ; exit 1
fi
if ! which etsh >/dev/null 2>&1 ; then
	echo "$PROGNAME: etsh: Not found." >&2 ; exit 1
fi
test -d "$DIR" || mkdir -p "$DIR"
test $? -eq 0  || exit 1

if ! which tsh >/dev/null 2>&1 ; then
	# Convert each tsh command file to run with etsh instead.
	for OLD in *.tsh ; do
		NEW="${OLD%.tsh}.etsh"
		if test ! -e "$NEW" ; then
			echo "Converting \`$OLD' to \`$NEW' ..."
			<"$OLD" sed 's/tsh/etsh/g' >"$NEW"
			test $? -eq 0 || exit 1
			NEWLIST="$NEWLIST $NEW"
		fi
	done
	mkdir tmp-$$ && mv *.tsh tmp-$$/ || exit 1 ; echo
fi

if test X"$INSTALL" = X ; then
	INSTALL="`which install`"
elif ! which "$INSTALL" >/dev/null 2>&1 ; then
	echo "$PROGNAME: $INSTALL: Not found." >&2 ; exit 1
fi

#
# Install everything now.
#
MODE="-m 0555"
for FILE in *.etsh *.tsh ; do
	DEST="$DIR/${FILE%.*}"
	echo "$INSTALL -c $MODE	$FILE	$DEST"
	"$INSTALL" -c $MODE	"$FILE"	"$DEST"
	test $? -eq 0 || exit 1
	if test X"$FILE" = X"lf.etsh" ; then
		echo "ln -fs lf $DIR/li && ln -fs lf $DIR/ll"
		ln -fs lf "$DIR/li" && ln -fs lf "$DIR/ll"
		test $? -eq 0 || exit 1
	fi
done

#
# Clean up if needed.
#
if test X"$NEWLIST" != X -a -d tmp-$$ ; then
	rm -f $NEWLIST && mv tmp-$$/*.tsh ./ && rmdir tmp-$$
fi

#
# Advise user if $PATH does not contain absolute pathname of $DIR.
#
cd "$DIR"
CWD="`pwd`"
case "${PATH}" in
${CWD}:*|*:${CWD}:*|*:${CWD}) : nothing ;;
*)
cat <<EOI >&2

Prepend $CWD to PATH
in order to successfully invoke all of the command files.

Appending $CWD to PATH
is another option but is less useful in the general case.
EOI
;;
esac
