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

## begin shared

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:$PATH
export PATH
unset SCOMPAT

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

implementation="`uname -s`"
release="`uname -r`"

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
	XLIB=/usr/X11R6/lib/X11
	XBIN=/usr/X11R6/bin
	;;

"FreeBSD" )
	XARCH=FreeBSD
	XOS=FreeBSD
	XLIB=/usr/X11R6/lib/X11
	XBIN=/usr/X11R6/bin
	;;

"NetBSD" )
	XARCH=NetBSD
	XOS=NetBSD
	XLIB=/usr/X11R6/lib/X11
	XBIN=/usr/X11R6/bin
	;;

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

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

"LynxOS" )
	XARCH=LynxOS
	XOS=LynxOS
	;;

"AIX" )
	XARCH=AIX
	XOS=AIX
	;;

"HP-UX" )
	XARCH=HPUX
	XOS=HPUX
	;;

*)
	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 $XBIN -o ! -d $XLIB/AcceleratedX ]
then
	if [ -d /usr/X11R6/lib/X11/AcceleratedX ]
	then
		XLIB=/usr/X11R6/lib/X11
		XBIN=/usr/X11R6/bin
	elif [ -d /usr/X11R5/lib/X11/AcceleratedX ]
	then
		XLIB=/usr/X11R5/lib/X11
		XBIN=/usr/X11R5/bin
	elif [ -d /usr/X386/lib/X11/AcceleratedX ]
	then
		XLIB=/usr/X386/lib/X11
		XBIN=/usr/X386/bin
	elif [ -d /usr/lib/X11/AcceleratedX ]
	then
		XLIB=/usr/lib/X11
		XBIN=/usr/bin/X11
	else
		echo "Cannot find Accelerated-X in any supported directory:"
		echo
		[ "$XLIB" != "/usr/lib/X11" ] && echo "\t$XLIB"
		echo "\t/usr/X11R6/lib/X11"
		echo "\t/usr/X11R5/lib/X11"
		echo "\t/usr/X386/lib/X11"
		echo "\t/usr/lib/X11"
		echo
		exit 1
	fi
fi

if [ "x$XARCH" = "x" ]
then
	echo "Unknown Architecture ($implementation $release)"
	exit 1
fi

XACCELHOME=$XLIB/AcceleratedX

export XACCELHOME XACCELEXEC XARCH XBIN XLIB XOS

## end shared

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

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:$PATH
export PATH


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

if [ -d $XACCELHOME/arch/$XARCH/$XOS ]
then 
    touch $XACCELHOME/arch/$XARCH/$XOS/.installed >/dev/null 2>&1 && \
	echo "$XACCELHOME/arch/$XARCH/$XOS/.installed f 644 bin bin" >>$XFILES
fi

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

if [ -h $XBIN/X ]
then
	cd $XBIN
	XSERVER=`/bin/ls -l $XBIN/X | awk '{ print $11 }'`
	XSRVDIR=`dirname $XSERVER`
	if [ ! -z "$XSRVDIR" -a "$XSRVDIR" != "$XBIN" -a "$XSRVDIR" != "." ]
	then
		echo "## Linking $XBIN/Xaccel to $XSERVER."
		mkdir -p $XSRVDIR 2>/dev/null
		/bin/rm -f $XSERVER 2>/dev/null
		ln -s $XBIN/Xaccel $XSERVER
	else
		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
		fi

		echo "## Linking $XBIN/Xaccel to $XBIN/X."
		/bin/rm -f $XBIN/X 2>/dev/null
		ln -s $XBIN/Xaccel $XBIN/X
	fi
else
	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
	fi

	echo "## Linking $XBIN/Xaccel to $XBIN/X."
	/bin/rm -f $XBIN/X 2>/dev/null
	ln -s $XBIN/Xaccel $XBIN/X
fi

##
## 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/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

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/X11R6/man/man1 ]
then
	cp $XACCELHOME/man/cat1/Xaccel.1 /usr/X11R6/man/man1/Xaccel.1
	echo "/usr/X11R6/man/man1/Xaccel.1 f 444 bin bin" >>$XFILES
fi

##
## Fix permissions of binaries, and copy the Xaccel & Xsetup binaries into
## the final locations, if they are not already there.
##
echo "## Fixing permissions (this takes a long time)."

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

if [ "$XOS" = "ISC" -o "$XOS" = "SCO" ]
then
	find $XACCELHOME -exec chown bin {} \;
	find $XACCELHOME -exec chgrp bin {} \;
	find $XACCELHOME -exec chmod 644 {} \;
else
	chown -R bin $XACCELHOME
	chgrp -R bin $XACCELHOME
	chmod -R 644 $XACCELHOME
fi

find $XACCELHOME -type d -exec chmod 755 {} \;

chown root $XACCELHOME/arch/$XARCH/Xaccel >/dev/null 2>&1
chgrp bin  $XACCELHOME/arch/$XARCH/Xaccel >/dev/null 2>&1
chmod 4111 $XACCELHOME/arch/$XARCH/Xaccel >/dev/null 2>&1
chown root $XACCELHOME/arch/$XARCH/Xaccel.demo >/dev/null 2>&1
chgrp bin  $XACCELHOME/arch/$XARCH/Xaccel.demo >/dev/null 2>&1
chmod 4755 $XACCELHOME/arch/$XARCH/Xaccel.demo >/dev/null 2>&1
chown root $XACCELHOME/arch/$XARCH/Xaccel-* >/dev/null 2>&1
chgrp bin  $XACCELHOME/arch/$XARCH/Xaccel-* >/dev/null 2>&1
chmod 4111 $XACCELHOME/arch/$XARCH/Xaccel-* >/dev/null 2>&1
chown root $XACCELHOME/arch/$XARCH/Xaccel-*.demo >/dev/null 2>&1
chgrp bin  $XACCELHOME/arch/$XARCH/Xaccel-*.demo >/dev/null 2>&1
chmod 4755 $XACCELHOME/arch/$XARCH/Xaccel-*.demo >/dev/null 2>&1

rm -f $XBIN/Xaccel
cp $XACCELHOME/bin/Xaccel $XBIN/Xaccel
rm -f $XBIN/Xsetup
cp $XACCELHOME/bin/Xsetup $XBIN/Xsetup

echo "$XBIN/Xaccel f 755 bin bin" >>$XFILES
echo "$XBIN/Xsetup f 755 bin bin" >>$XFILES
echo "$XACCELHOME/bin/Xinstall f 755 bin bin" >>$XFILES
echo "$XACCELHOME/bin/Xuninstall f 755 bin bin" >>$XFILES
echo "$XACCELHOME/bin/Xaccel f 755 bin bin" >>$XFILES
echo "$XACCELHOME/bin/Xsetup f 755 bin bin" >>$XFILES

##
## Now the files of files in the release is complete. Just change the
## permissions for the files we have collected up to now.
##
sort $XFILES | uniq | \
while read file type perms owner group
do
	chown $owner $file
	chgrp $group $file
	chmod $perms $file 
done

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

sort $XFILES | uniq >/tmp/$$Xfiles
mv /tmp/$$Xfiles $XFILES

##
## Now, tell user about the Xsiteinfo configuration.
##
##

if [ -x /usr/bin/mail ]
then
    MAILPROGRAM=/usr/bin/mail
elif [ -x /bin/mail ]
then
    MAILPROGRAM=/bin/mail
else
    MAILPROGRAM=mail
fi

MAILTARGET=`grep 'BugReportEmail' $XACCELHOME/etc/Xsiteinfo`
MESSAGE=/tmp/$$message
cat >$MESSAGE <<EOF
Accelerated-X 2.1 has been successfully been installed on your
machine. The original X-Server (if present) has been saved to
$XBIN/X.$XOS.

By default Accelerated-X is configured for a standard VGA running
at 640x480. To configure the X-Server for your specific graphics
hardware, switch to text mode (i.e. disable the graphics login)
and run $XBIN/Xsetup.

For late documentation updates, please refer the README file in
$XACCELHOME/etc.

*** IMPORTANT NOTICE ***

	If the Accelerated-X Server fails, it will attempt
	to write a stack-trace and mail it.  The email address
	is currently set to:

		$MAILTARGET

	You may change this by editing 'BugReportEmail' in the
	file \$XACCELHOME/etc/Xsiteinfo .  We like reports to
	be sent to 'bugs@xinside.com'.

EOF

echo "Subject: Installation of Accelerated-X 2.1, $XOS ($XARCH) version" | \
cat - $MESSAGE | $MAILPROGRAM root >/dev/null 2>&1
cat $MESSAGE
rm $MESSAGE 2>&1 >/dev/null

exit 0

