#!/bin/sh
#
#  install.sh
#
#  Install the binaries, fonts, libraries and header files for X11R6

ARCH=""
COPY=link
CDROM="/cdrom"
PREFERRED_LOC="/usr/X11R6"
INSTALL=${PREFERRED_LOC}
THIS_DISC=disc2
DISC1_ARCHS="Bsdi Linux Solaris-2.4 solaris2.3 NetBSD sunos4.1.3 FreeBSD-1.1.5 hpux9.05 ultrix4.4 FreeBSD-2.0 SVR4"
DISC2_ARCHS="irix5.2 ISC osf3.0"
MY_ARCHS="irix5.2 ISC osf3.0"

PATH=/bin:/usr/bin:/usr/ucb:${INSTALL}/bin:.

# fail on errors
set -e

usage() {
  echo "usage: $0 [-c] [-a <arch>] [-p <cdrom_path>] [-i <install_path>]"
  echo " -c set flag to copy files instead of using symbolic links"
  echo " -a <arch> selects architecture desired (default is to try and guess)"
  echo " -p <cdrom_path> sets cdrom mount point (default is \"${CDROM}\")"
  echo " -i <install_path> sets installation directory (default is \"${INSTALL}\")"
  exit 1
}

check_args() {
	while [ $# -gt 0 ]; do
  	   case $1 in
		-c)	COPY=copy ;;
		-a)	if [ $# -lt 2 ]; then usage; fi; ARCH=$2; shift ;;
		-p)	if [ $# -lt 2 ]; then usage; fi; CDROM=$2; shift ;;
		-i)	if [ $# -lt 2 ]; then usage; fi; INSTALL=$2; shift ;;
		*)	usage ;;
  	   esac
  	   shift
	done
}

verify_dirs() {
	if [ ! -d ${CDROM} -o ! -f ${CDROM}/Copyright ]; then
		echo "${CDROM} is either not mounted or points to an invalid location."
		exit 1
	fi
	if [ ! -d ${INSTALL} ]; then
		if ! mkdir ${INSTALL}; then
			echo "Unable to create destination directory $INSTALL"
			exit 1
		fi
	fi
	if [ "${INSTALL}" != "${PREFERRED_LOC}" ]; then
		echo ""
		echo "Warning:  You have picked a non-standard location for the X11R6"
		echo "installation - you should be sure to make a symbolic link from"
		echo "${INSTALL} to ${PREFERRED_LOC} before attempting to use this software."
		echo -n "If you like, we can do it now.  Would you like to do it now? [y/n] "
		read ans
		if [ "X${ans}" = "Xy" ]; then
			if [ -e ${PREFERRED_LOC} ]; then
				echo "${PREFERRED_LOC} already exists!  Please fix this and try again."
				exit 1
			fi
			if ln -s ${INSTALL} ${PREFERRED_LOC}; then
				echo Link created successfully.
			else
				echo "Couldn't create link from ${INSTALL} to ${PREFERRED_LOC}!  ABORT!"
				exit 1
			fi
		fi
	fi
}

guess_arch() {
	if [ -f /hp-ux ]; then
		ARCH=hpux9.05
	elif [ -f /vmunix ] && uname -a | grep ultrix > /dev/null 2>&1; then
		ARCH=ultrix4.4
	elif [ -f /386bsd ]; then
		ARCH=FreeBSD-1.1.5
	elif [ -f /kernel ] && uname -a | grep FreeBSD > /dev/null 2>&1; then
		ARCH=FreeBSD-2.0
	elif [ -f /kernel ] && uname -a | grep NetBSD > /dev/null 2>&1; then
		ARCH=NetBSD
	elif [ -f /kernel ] && uname -a | grep SunOS > /dev/null 2>&1; then
		ARCH=solaris2.3
	elif [ -f /vmunix ] && uname -a | grep SunOS > /dev/null 2>&1; then
		ARCH=sunos4.1.3
	elif [ -f /mach_servers ]; then
		ARCH=osf3.0
	else
		echo "Unable to guess your architecture.  You'll have to specify it on"
		echo "the command line and try again."
		echo
		echo "See the README file on this disc for a list of supported architectures."
		exit 1
	fi
	echo "Hmmmm.  Looks rather like a ${ARCH} system to me!"
}

verify_arch() {
	if [ "X${ARCH}" = "X" ]; then
		echo "No arch specified.  Trying to guess your architecture."
		guess_arch
	fi
	# First, check to make sure we simply haven't got the wrong CD.
	if [ "${THIS_DISC}" = "disc1" ]; then
		for i in ${DISC2_ARCHS}; do
			if [ "${i}" = "${ARCH}" ]; then
				echo
				echo "The ${ARCH} architecture distribution is on the second CDROM and you are"
				echo "using the first.  Please switch CDs and try again."
				exit 1
			fi
		done
	else
		for i in ${DISC1_ARCHS}; do
			if [ "${i}" = "${ARCH}" ]; then
				echo
				echo "The ${ARCH} architecture distribution is on the first CDROM and you are"
				echo "using the second.  Please switch CDs and try again."
				exit 1
			fi
		done
	fi
	# Now check to make sure that the distribution exists here.
	for i in ${MY_ARCHS}; do 
		if [ "${i}" = "${ARCH}" ]; then
			return;
		fi
	done

	# If we've fallen this far, it's hopeless.
	echo
	echo "The ${ARCH} architecture you have selected is not supported for direct"
	echo "execution.  You may wish to create a link tree to the sources"
	echo "and compile them from the CD to create a distribution of your own."
	echo "The tools in the utils subdirectory are useful for creating such"
	echo "link trees, and the sources themselves may be found on disc2 in:"
	echo "	${CDROM}/distrib/XFree86-3.1.1/untarred/xc"
	exit 1
}

do_install() {
	if [ "${COPY}" = "copy" ]; then
		echo "Copying to ${INSTALL}..."
		( cd ${CDROM}/${ARCH}; tar cf - *) | (cd ${INSTALL}; tar xpvf -)
	else
		echo "Linking to ${CDROM}/${ARCH}..."
		${CDROM}/${ARCH}/bin/lndir ${CDROM}/${ARCH} ${INSTALL};
	fi
	echo
	echo "You may need to add ${INSTALL}/lib to your LD_LIBRARY_PATH"
	echo "and/or use the ldconfig command to add the X11R6 shared libraries"
	echo "to the appropriate search path before the various executables"
	echo "will work.  Please see your system documentation for ld(1)"
	echo "(man ld) for more information."
	exit 0
}

# Ok, here we go.
check_args $*
verify_dirs
verify_arch
echo ""
echo "About to begin installation to ${INSTALL} from ${CDROM} for ${ARCH}."
echo -n "Are you sure? [y/n] "
read ans
if [ "X${ans}" = "Xy" ]; then
	do_install
else
	echo Installation not confirmed
	exit 1
fi
