#!/bin/sh
# 
# $Id: instlinks,v 1.8 2000/09/26 18:54:28 dgregor Exp $
# 
# Copyright (c) 1997-1999 Daniel J. Gregor, Jr., All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by Daniel J. Gregor, Jr.
# 4. The name of Daniel J. Gregor, Jr. may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
# 
# THIS SOFTWARE IS PROVIDED BY DANIEL J. GREGOR, JR. ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL DANIEL J. GREGOR, JR. BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# DESCRIPTION:
#	This script installs symbolic links into the PREFIX for the directories
#	specified on the command line.  You should be able to specify absolute
#	or relative directories.  The directories for which links are made are
#	bin, sbin, and man/man*.
#
# If you have any fixes, suggestions, etc., drop me a note at <dj@gregor.com>.
#   - djg
#
# What is PREFIX??
#	PREFIX is the base directory where the links are put.  By default,
#	this is /usr/local.  You can change this default just below this
#	comment, or you can change it at run time with the "-p prefix"
#	option.
PREFIX="/usr/local"
ROOT=""

USAGE="Usage: `basename $0` [-v] [-n] [-p prefix] [-R root] <package directories>"

BASENAME="`basename $0`"
warn() {
	echo "${BASENAME}: $*" >&2
}


linkdir() {
	PACKAGEDIR="$1"

	if [ ! -d ${ROOT}${PACKAGEDIR} ]
	then
		warn "${ROOT}${PACKAGEDIR} does not exist" 
		return 1
	fi

	cd "${ROOT}${PACKAGEDIR}"

	DIRS=""
	for DIR in bin sbin
	do
		if [ -d ${DIR} ]
		then
			DIRS="${DIRS} ${DIR}"
		fi
	done

	if [ -d man ]
	then
		DIRS="${DIRS} `find man/* -type d -print`"
	fi

	if [ "x${DIRS}" != "x" ]
	then
		for FILE in `find ${DIRS} \! -type d -print`
		do
			if [ $DRYRUN -eq 0 ]; then
				rm -f "${ROOT}${PREFIX}/${FILE}"
			fi
			echo "${PACKAGEDIR}/${FILE} -> ${PREFIX}/${FILE}" >&3
			if [ $DRYRUN -eq 0 ]; then
				ln -s ${PACKAGEDIR}/${FILE} \
					${ROOT}${PREFIX}/${FILE}
			fi
		done
	fi
}

VERBOSE=0
DRYRUN=0

while getopts vhnp:R: c
do
	case $c in
		v)
			VERBOSE=1
		;;

		p)
			PREFIX="$OPTARG"
		;;

		R)
			ROOT="$OPTARG"
		;;

		n)
			DRYRUN=1
		;;

		h)
			echo $USAGE
			exit 0
		;;

		\?)
			echo $USAGE
			exit 1
		;;
	esac
done

shift `expr $OPTIND - 1`


if [ $VERBOSE -ne 0 ]; then
	exec 3>&1
else
	exec 3>/dev/null
fi

if [ $# -eq 0 ]
then
	echo "`basename $0`: you must specify at least one directory." >&2
	exit 2
fi
			
for PACKAGEDIR
do
	if echo "${PACKAGEDIR}" | grep "^/" > /dev/null
	then
		linkdir "${PACKAGEDIR}"
	else
		warn "${PACKAGEDIR} is not absolute -- skipping" 
	fi

done
