#!/bin/sh
###########################################################################
#
# Easy Install (c) 1994 Hubert Feyrer
#
#--------------------------------------------------------------------------
#
# V1.0 940904 Initial hacking
# V1.1 940921 Change /opt to /usr/local
# V1.2 940925 Fix bug when linking directories
#
#--------------------------------------------------------------------------
#
# ============== Hubert Feyrer =========================================== 
#      Weekdays: Rennerstr. 19, D-93053 Regensburg,  Tel. 0941/701788
#      Weekends: Bachstr. 40,   D-84066 Mallersdorf, Tel. 08772/6084
#      Internet: hubert.feyrer@rz.uni-regensburg.de == IRC: hubertf
# ======================================================================== 
#
###########################################################################

# Verbosity control
exec=""		# set to "echo" to display commands 
v1=true		# Introduction blurb
v2=true		# what's going on
v3=false	# debugging stuff
v4=true		# do "$exec command" ? (more debugging)

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

# Defaults
BASE=/usr/local
BIN=""
ETC=""
LIB=""
MAN1=""
MAN2=""
MAN3=""
MAN4=""
MAN5=""
MAN6=""
MAN7=""
MAN8=""
CAT1=""
CAT2=""
CAT3=""
CAT4=""
CAT5=""
CAT6=""
CAT7=""
CAT8=""
INFO=""
APPDEF=""
INCLUDE=""
SUIDROOT=""
MISCDIRS=""


checkdir()
{
	if [ ! -d $1 ];
	then
		$v3&&echo creating directory $1
		$v4&&$exec mkdir -p $1
	else
		$v3&&echo directory $1 exists
	fi
}


#
# Read package-information
#
$v3&&echo using configuration from `dirname $0`/package
. `dirname $0`/package


$v1&& echo Installing $PKG in $BASE

#
# Determine directory where application defaults for X go to
#
if [ "$APPDEF" != "" ];
then
	# search some directories
	for d in /usr /usr/local/X11R6 /usr/X11R6 /usr/local/X11R5 /usr/X11R5 \
		 $BASE/X11R6 $BASE/X11R5;
	do
        	if [ -d  $d/lib/X11/app-defaults ];
        	then
			APPDEFDIR=$d/lib/X11/app-defaults
                	break
        	fi
	done
	# nothing found, ask user
	while [ "$APPDEFDIR" = "" -o ! -d "$APPDEFDIR" ];
	do
		echo Couldn\'t figure out directory for X application-defaults.
		echo -n "Please specify: "
		read APPDEFDIR
	done
        $v3&&echo APPDEFDIR: $APPDEFDIR
fi

#
# Create misc directories, if necessary
#
checkdir $BASE
for i in $MISCDIRS ;
do
	checkdir $BASE/$i
done

#
# Link existing files from package to destination
#
doinstall()
{
	what=$1 ; shift
	dir=$1  ; shift

	if [ "$*" != "" ];
	then
		# mkdir if necessary
		checkdir $BASE/$dir

		$v2&&echo -n installing $what:
		for i in $* ;
		do
			if [ -e $PKG/$dir/$i ];
			then
				$v2&&echo -n " $i"
				$v4&&$exec ln -sf $PKG/$dir/$i $BASE/$dir
			else
				echo ''
				echo ERROR: $PKG/$dir/$i not found!
			fi
		done
		$v2&&echo .
	fi
}

doinstall "binaries"					bin		$BIN
doinstall "etc-files"					etc		$ETC
doinstall "libraries"					lib		$LIB
doinstall "unformatted man-pages for general commands"	man/man1	$MAN1
doinstall "unformatted man-pages for system calls"	man/man2	$MAN2
doinstall "unformatted man-pages for C libraries"	man/man3	$MAN3
doinstall "unformatted man-pages for device driver"	man/man4	$MAN4
doinstall "unformatted man-pages for file formats"	man/man5	$MAN5
doinstall "unformatted man-pages for games"		man/man6	$MAN6
doinstall "unformatted man-pages for miscellaneous"	man/man7	$MAN7
doinstall "unformatted man-pages for system operation"	man/man8	$MAN8
doinstall "formatted man-pages for general commands"	man/cat1	$CAT1
doinstall "formatted man-pages for system calls"	man/cat2	$CAT2
doinstall "formatted man-pages for C libraries"		man/cat3	$CAT3
doinstall "formatted man-pages for device driver"	man/cat4	$CAT4
doinstall "formatted man-pages for file formats"	man/cat5	$CAT5
doinstall "formatted man-pages for games"		man/cat6	$CAT6
doinstall "formatted man-pages for miscellaneous"	man/cat7	$CAT7
doinstall "formatted man-pages for system operation"	man/cat8	$CAT8
doinstall "info-manual"					info		$INFO
doinstall "includes"					include		$INCLUDE

#
# application defaults for X
#
if [ "$APPDEF" != "" ];
then
        $v2&&echo -n installing X application defaults:
        for i in $APPDEF ;
        do
                if [ -e $PKG/$i ];
                then
                        $v2&&echo -n " `basename $i .ad`"
                        $v4&&$exec ln -sf $PKG/$i $APPDEFDIR/`basename $i .ad`
                else
			echo ''
                        echo ERROR: $PKG/$i not found!
                fi
        done
        $v2&&echo .
fi

#
# files to setuid-root
#
if [ "$SUIDROOT" != "" ];
then
	$v2&&echo -n making files setuid root:
	for i in $SUIDROOT ;
	do
		if [ -f $PKG/$i ];
		then
			$v2&&echo -n " $i"
			$v4&&$exec chown root $PKG/$i
			$v4&&$exec chmod 4111 $PKG/$i
		else
			echo ''
                        echo ERROR: $PKG/$i not found!
		fi
	done
	$v2&&echo .
fi

$v1&&echo Done.
