#!/bin/sh
# Usage:
#	install_component component_name
#		or
#	install_component /system_cd / filelist_file
#
# Copyright 1994 Yggdrasil Computing, Inc.
# Written by Adam J. Richter (adam@yggdrasil.com)

cd /
if [ .$1 = . ] ; then
	echo 'Usage: install_component component_name'
	echo 'Valid components: '
	for x in /usr/lib/yggdrasil/components/*.include ; do
		echo -n ' '`basename $x .include`
	done
	echo
	exit 1
fi
if [ .$1 = .motif -a .$2 = . -a .$3 = . ] ; then
	echo 'Motif must be installed from the graphical control panel.'
	exit 1
fi

if [ .$2 = . ] ; then
	fromtree=/system_cd
	totree=/
	filelist=/system_cd/usr/lib/yggdrasil/components/${1}.include
else
	fromtree="$1"
	totree="$2"
	filelist="$3"
fi

# Search for commands in $fromtree/bin and $fromtree/usr/bin instead
# of in /bin and /usr/bin because  we may temporarily blow away
#/usr/bin, /bin and other directories during installation.
PATH=${PATH}:${fromtree}/usr/bin:${fromtree}/bin

# Make a list of the parent directories of every file that is
# going to be installed, and the parents of the parents, and their
# parents and so on.

# Remove trailing component and trailling slashes from filenames
sed 's#/[^/]*/*$##' < $filelist | egrep -v '^$' | sort -u > /tmp/parents.new
cp /dev/null /tmp/parents

#Generate parent directories
until cmp -s /tmp/parents /tmp/parents.new ; do
	cp /tmp/parents.new /tmp/parents
	sed 's#/[^/]*/*$##' < /tmp/parents | cat - /tmp/parents | sort -r -u > /tmp/parents.new
done
# Delete blank lines, add root.
( egrep -v '^$' /tmp/parents.new ; echo / ) | sort -r -u > /tmp/parents

# Remove all of the symlinks that originally pointed to the disc's tree.
checklinks $fromtree `cat /tmp/parents` `cat $filelist` | \
	tee /tmp/removed-symlinks | xargs rm -f
# Invalidate the shell's command cache, in case we deleted an important
# binary directory that now has to be read from the CD.
PATH=${PATH}

# Now look for symlinks living under one of the installed components.
# For example: /usr/include/tcl --> /system_cd/usr/include/tcl while
# trying to install /usr/include.
filelist_contents=`cat $filelist`
( checklinks $fromtree `find $filelist_contents -type l -print` | \
	xargs rm -f ) >& /dev/null
# Invalidate the shell's command cache, in case we deleted an important
# binary directory that now has to be read from the CD.
PATH=${PATH}

# If we're copying from /usr/src, then turn on auto-decompression.
if egrep -q ^usr/src ${filelist} ; then
	export LD_LIBRARY_PATH=${fromtree}/lib/compressed
fi

# Do the actual copying.
( cd $fromtree ; tar cf - `cat $filelist` ) | ( cd $totree ; tar xfpv - )

rebuild_symlinks $fromtree $fromtree $totree `cat /tmp/parents`

# Kludge: Work around fact that autodecompression does not set executable bit.
chmod -f +x /usr/src/linux/drivers/pcsnd/configure /usr/src/linux/makever.sh \
	>& /dev/null || exit 0
exit 0

