#!/bin/sh

# 	@(#) add_sneep_to_bin.src 1.3@(#) 06/08/05 11:50:12

# For those users who want easier access than /opt/SUNWsneep/bin/sneep ,
# add a link in /usr/sbin or some other convenient place,
# and register it as part of sneep .

# For Sneep RFE 00060 :  Link sneep into /usr/sbin for 
#					usability and path minimization

BINDIR=/usr/sbin	# The directory we want to link sneep into

ROOT=""		# root dir if not "/"
ROPT=""		# -R option for packaging  commands

while getopts R:h arg
do
	: arg is \""$arg"\"
	case "$arg" in
	h | \? )
		echo Link sneep into common directory.  Default: $BINDIR 1>&2
		echo usage: $0 '[ -R root_path ]' '[directory]' 1>&2
		exit 0
		;;
	R)
		ROOT=$OPTARG 
		if test ! -d $ROOT/. ; then
			echo No such directory as $ROOT 1>&2
			exit 1
		fi
		ROPT="-R $ROOT"		# option used in packaging commands
		ROOT=$ROOT/		# include / for messages later
		;;
	esac
done

shift `expr $OPTIND - 1`

	

case "$1" in
	"")	: leave BINDIR unchanged from default $BINDIR
		;;
	*)
		BINDIR="$1"
		;;
esac
		

if test ! -d "$BINDIR" ; then
	echo No such directory: $BINDIR 1>&2 
	exit 1
fi


easysneep=$BINDIR/sneep		# the link we want to put in place

sneep=""
if sneepbase=`pkgparam $ROPT SUNWsneep BASEDIR 2> /dev/null` ; then
	sneep=$sneepbase/bin/sneep
	if test ! -f $ROOT$sneep ; then
		echo Internal error: cannot find sneep at $ROOT$sneep 1>&2
		exit 1
	fi
	# try to get version of installed sneep
	sneepver=`grep "^version=" $ROOT$sneep 2> /dev/null`
else
	echo Sneep does not seem to be installed 1>&2
	exit 1
fi


# look for file where we plan to put a link
if test -f $ROOT$easysneep -a ! -h $ROOT$easysneep ; then
	echo $easysneep is already present as a file, not a link 1>&2
	echo This may have been done manually, and is not recommended. 1>&2
	ver=`grep "^version=" $ROOT$easysneep`
	test ! -z "$ver" && echo $easysneep $ver 1>&2
	test ! -z "$sneepver" && echo $sneep $sneepver  1>&2
	exit 1
fi



# remove  existing link if present
if test -h $ROOT$easysneep ; then
	/bin/rm -f $ROOT$easysneep 2> /dev/null
fi

# We would like to make it a relative link, but since we allow the user
# to pick the directory, it would be a lot of work for a tiny improvement
# in correctness, so we will link to the absolute path for sneep

# begin registration process by specifying the link
installf $ROPT SUNWsneep $easysneep=$sneep s || exit 1

# register link as part of sneep : this also creates the link for us
if installf $ROPT -f SUNWsneep ; then
	: no problem
else
	echo Could not add $easysneep 1>&2
	exit 1
fi

exit 0


