#!/bin/sh
#
# Copyright 1990,91,92,93,94  by Thomas Roell, Germany.
# Copyright 1993,94           by X Inside Inc, USA.
# All Rights Reserved.
#

## begin shared

unset SCOMPAT

XACCELHOME=
XARCH=
XOS=
XLIB=/usr/lib/X11
XBIN=/usr/bin/X11

if [ -x /bin/uname ]
then
	implementation="`/bin/uname -s`"
	release="`/bin/uname -r`"
else
	implementation="`/usr/bin/uname -s`"
	release="`/usr/bin/uname -r`"
fi

case "$implementation" in
"SINIX-D" | "SINIX-Z" )
	XARCH=SVR4
	XOS=SINIX
	;;

"SunOS" )
	XARCH=SVR4
	XOS=SOLARIS
	XLIB=/usr/openwin/lib
	XBIN=/usr/openwin/bin
	;;

"Linux" )
	XARCH=LINUX
	XOS=LINUX
	;;

"FreeBSD" )
	XARCH=FreeBSD
	XOS=FreeBSD
	;;

"BSD/386" )
	XARCH=BSDI
	XOS=BSDI
	XLIB=/usr/X11/lib/X11
	XBIN=/usr/X11/bin
	;;

*)
	case "$release" in
	"4.2" | "4.2MP" )
		XARCH=SVR4
		XOS=SVR4.2
		XLIB=/usr/X/lib
		XBIN=/usr/X/bin
		;;

	"4.0" | "4.1" )
		XARCH=SVR4
		XOS=SVR4
		;;

	"3.2" )
		if [ -f /etc/perms/rts ]
		then	
		        XARCH=SCO
			XOS=SCO
		else
		        XARCH=ISC
			XOS=ISC
		fi
		;;
	esac
	;;
esac

if [ -d /usr/X386/lib/X11 ]
then
	XLIB=/usr/X386/lib/X11
	XBIN=/usr/X386/bin
fi

if [ "x$XARCH" = "$XARCH" ]
then
	echo "Unkown Architecture"
	exit -1
fi

XACCELHOME=$XLIB/AcceleratedX

export XACCELHOME XARCH XBIN XLIB XOS

## end shared

if [ ! -d $XACCELHOME/arch/$XARCH ]
then
	echo "Support for $XARCH not installed"
	exit -2
fi

XFILES=$XACCELHOME/etc/Xfiles

##
## Remove all unwanted architectures
##
touch $XACCELHOME/arch/$XARCH/.installed >/dev/null 2>&1 && \
	echo "$XACCELHOME/arch/$XARCH/.installed f 644 bin bin" >>$XFILES
touch $XACCELHOME/arch/$XARCH/$XOS/.installed >/dev/null 2>&1 && \
	echo "$XACCELHOME/arch/$XARCH/$XOS/.installed f 644 bin bin" >>$XFILES

for arch in `/bin/ls $XACCELHOME/arch`
do
	if [ -f $XACCELHOME/arch/$arch/.installed ]
	then
		for os in `/bin/ls $XACCELHOME/arch/$arch`
		do
			if [ -d $XACCELHOME/arch/$arch/$os -a ! -f $XACCELHOME/arch/$arch/$os/.installed ]
			then
				/bin/rm -rf $XACCELHOME/arch/$arch/$os
			fi
		done
	else
		/bin/rm -rf $XACCELHOME/arch/$arch
	fi
done

##
## Execute XOS dependent install script first, and then the XARCH dependent
## one. Those will install device drivers and similar things.
##
if [ -f $XACCELHOME/arch/$XARCH/$XOS/Install.sh ]
then
	cd $XACCELHOME/arch/$XARCH/$XOS
	/bin/sh ./Install.sh
fi

if [ -f $XACCELHOME/arch/$XARCH/Install.sh ]
then
	cd $XACCELHOME/arch/$XARCH
	/bin/sh ./Install.sh
fi

##
## Fix permissions of binaries, and copy the Xaccel & Xsetup binaries into
## the final locations, if they are not already there.
##

chown root $XACCELHOME/arch/$XARCH/Xaccel
chgrp bin  $XACCELHOME/arch/$XARCH/Xaccel
chmod 4755 $XACCELHOME/arch/$XARCH/Xaccel


rm -f $XBIN/Xaccel
cp $XACCELHOME/bin/Xaccel $XBIN/Xaccel
echo "$XBIN/Xaccel f 755 bin bin" >>$XFILES

rm -f $XBIN/Xsetup
cp $XACCELHOME/bin/Xsetup $XBIN/Xsetup
echo "$XBIN/Xsetup f 755 bin bin" >>$XFILES

if [ -x $XBIN/X -a ! -x $XBIN/X.$XOS ]
then
	echo "## Saving $XBIN/X to $XBIN/X.$XOS."
	/bin/mv $XBIN/X $XBIN/X.$XOS
else
	/bin/rm -f $XBIN/X 2>/dev/null
fi

rm -f $XACCELHOME/etc/Xboards
touch $XACCELHOME/etc/Xboards
rm -f $XACCELHOME/etc/Xmonitors
touch $XACCELHOME/etc/Xmonitors

echo "## Linking $XBIN/Xaccel to $XBIN/X."
ln -s $XBIN/Xaccel $XBIN/X


##
## Now install the main manual page, whereever it will fit.
##

if [ -d /usr/share/man/cat1 ]
then
	if [ "$XARCH" = "BSDI" ]
	then
		cp $XACCELHOME/man/cat1/Xaccel.1 /usr/share/man/cat1/Xaccel.0
		echo "/usr/share/man/cat1/Xaccel.0 f 444 bin bin" >>$XFILES
	else
		cp $XACCELHOME/man/cat1/Xaccel.1 /usr/share/man/cat1
		echo "/usr/share/man/cat1/Xaccel.1 f 444 bin bin" >>$XFILES
	fi
fi

if [ -d /usr/flib/books/man/cat1 ]
then
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/flib/books/man/cat1/Xaccel.1
	echo "/usr/flib/books/man/cat1/Xaccel.1 f 444 bin bin" >>$XFILES
fi

if [ -d /usr/openwin/share/man ]
then
	if [ ! -d /usr/openwin/share/man/cat1 ]
	then
		mkdir /usr/openwin/share/man/cat1
	fi
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/openwin/share/man/cat1/Xaccel.1
	echo "/usr/openwin/share/man/cat1/Xaccel.1 f 444 bin bin" >>$XFILES
fi

if [ -d /usr/man/cat1 ]
then
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/man/cat1/Xaccel.1
	echo "/usr/man/cat1/Xaccel.1 f 444 bin bin" >>$XFILES
fi

if [ -d /usr/catman/u_man/man1 ]
then
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/catman/u_man/man1/Xaccel.1
	echo "/usr/catman/u_man/man1/Xaccel.1 f 444 bin bin" >>$XFILES
fi

if [ -d /usr/man/cat.X ]
then
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/man/cat.X/Xaccel.X
	echo "/usr/man/cat.X/Xaccel.X f 444 bin bin" >>$XFILES
fi

##
## Fix the permissions for all files belonging to the package
##
echo "## Fixing permissions."

for file in `find $XACCELHOME -print`
do
	if [ -d $file ]
	then
		echo $file d 755 bin bin

	elif [ -x $file ]
	then
		if [ -u $file ]
		then
			echo $file f 4755 root bin
		else
			echo $file f  755 bin bin
		fi
	else
		echo $file f 644 bin bin
	fi
done >>$XFILES

##
## Now the files of files in the release is complete. Just change the
## permissions for EVERYTHING.
##
sort $XFILES | uniq | tee /tmp/$$Xfiles | \
while read file type perms owner group
do
	chown $owner $file
	chgrp $group $file
	chmod $perms $file 
done

mv /tmp/$$Xfiles $XFILES

exit 0