#!/bin/sh
# XView linux port, installation utility
# 12/1992 K.Osterberg

# These must correspond with contents of libs.tar
OLGMAJOR=1
OLGMINOR=1
XV3MAJOR=1
XV3MINOR=1
SSMAJOR=1
SSMINOR=1
OPENWINHOME=/usr/openwin; export OPENWINHOME

if [ ! -f openwin.tar ]
then
	echo "Please change your directory to where the distribution files reside"
	exit 1
fi

INSTALL=""
if [ $# -gt 0 ]
then
	case "$1" in
	olwm)
		;;
	olvwm)
		;;
	*)
		echo "Usage: $0 [olwm|olvwm]"
		echo "When no command line arguments are provided, the whole package"
		echo "will be installed."
		exit 1
	esac
	INSTALL="$1"
fi

if [ -d $OPENWINHOME -o -f /usr/lib/libxview.a -o -f /lib/libxv3.so.$XV3MAJOR.? ]
then
	echo -n "Is it ok to remove old XView installation? [y/n] "
	read answer
	case $answer in
	y*)
		;;
	Y*)
		;;
	*)
		echo "Install aborted"
		exit 1
		;;
	esac	

	if [ -d $OPENWINHOME ]
	then	
		echo "Removing $OPENWINHOME"
		rm -rf $OPENWINHOME
	fi
	if [ -d /usr/include/xview ]
	then
		echo "Removing old header files"
		rm -rf /usr/include/xview
		rm -rf /usr/include/olgx
		rm -rf /usr/include/pixrect
	elif [ -L /usr/include/xview ]
	then
		echo "Removing links from /usr/include"
		rm -f /usr/include/xview
		rm -f /usr/include/olgx
		rm -f /usr/include/pixrect
		rm -f /usr/include/sspkg
		rm -f /usr/include/uit
	fi

	if [ -f /usr/lib/libxview.a -o -f /usr/lib/libolgx.a ]
	then
		echo "Removing old static libraries"
		rm -f /usr/lib/libxview.a /usr/lib/libolgx.a
	fi
	if [ -f /usr/lib/shlib/jump/libxview.a -o -f /lib/libolg.so.${OLGMAJOR} ]
	then
		echo "Removing old jump libraries"
		rm -f /usr/lib/shlib/jump/libolgx.a
		rm -f /usr/lib/shlib/jump/libxview.a
		rm -f /usr/lib/shlib/jump/libsspkg.a
		rm -f /lib/libolg.so.${OLGMAJOR}*
		rm -f /lib/libxv3.so.${XV3MAJOR}*
		rm -f /lib/libss.so.${SSMAJOR}*
	fi
	echo "Old installation removed"
	echo ""
fi

echo "Installing the $OPENWINHOME directory"
cat openwin.tar | (cd /; tar xf -)
if [ $? -ne 0 ]
then
	echo "Failure during openwin directory installation, aborting"
	exit 1
fi
if [ -n "$INSTALL" ]
then
	if [ -x "$OPENWINHOME/bin/$INSTALL" ]
	then
		mv "$OPENWINHOME/bin/$INSTALL" "/usr/X386/bin/$INSTALL"
		if [ $? -ne 0 ]
		then
			echo "Failed to install $OPENWINHOME/bin/$INSTALL in /usr/X386/bin"
			exit 1
		fi
		rm -rf $OPENWINHOME
		echo "The $INSTALL window manager was installed in /usr/X386/bin" 
	else
		echo "Installation error, missing $OPENWINHOME/bin/$INSTALL"
		exit 1
	fi
fi

if [ -z "$INSTALL" -a -n "`cat /etc/termcap | grep 'Sun Microsystems Workstation console'`" ]
then
	echo "Replacing Sun terminal entry in /etc/termcap with my own"
	awk '
		BEGIN {
			copy = 1;
		}
		($0 ~ "Sun Microsystems Workstation console") && ($1 !~ "#") {
			copy = 0;
		}
		copy == 1 {
			print;
		}
		copy == 0 {
			l = length $0;
			if (substr($0, l, 1) != "\\")
				copy = 1;
		}
	' </etc/termcap >/tmp/termcap.tmp
	if [ $? -ne 0 -o ! -s /tmp/termcap.tmp ]
	then
		echo "The install script broke, could not understand your termcap"
		exit 1
	fi
	cp /etc/termcap /etc/termcap.bak  # Store old termcap in case of trouble
	if [ -n "`cat /tmp/termcap.tmp | grep 'Cmdtool/Shelltool'`" ]
	then
		awk '
			$0 ~ "Cmdtool/Shelltool" {
				print;
				exit;
			}
			{ print }
		' </tmp/termcap.tmp >/etc/termcap
		cat termcap.sun >>/etc/termcap
		awk '
			BEGIN {
				copy = 0;
			}
			$0 ~ "Cmdtool/Shelltool" {
				copy = 1;
				next;
			}
			copy == 1 {
				print;
			}
		' </tmp/termcap.tmp >>/etc/termcap
	else
		cp /tmp/termcap.tmp /etc/termcap
		echo "" >>/etc/termcap
		echo "# Cmdtool/Shelltool expects termcap entry for Sun to exist" >>/etc/termcap
		cat termcap.sun >>/etc/termcap
	fi
	rm /tmp/termcap.tmp
	chmod 644 /etc/termcap
elif [ -z "$INSTALL" ]
then
	echo "Adding entry for Sun terminal to /etc/termcap"
	echo "" >>/etc/termcap
	echo "# Cmdtool/Shelltool expects termcap entry for Sun to exist" >>/etc/termcap
	cat termcap.sun >>/etc/termcap
	chmod 644 /etc/termcap
fi

echo "Installing libraries:"
if [ -n "$INSTALL" ]
then
	FLIST="lib/libolg.so.$OLGMAJOR.$OLGMINOR"
	ln -sf /lib/libolg.so.$OLGMAJOR.$OLGMINOR /lib/libolg.so.$OLGMAJOR
fi
cat libs.tar | (cd /; tar xvf - $FLIST)
if [ $? -ne 0 ]
then
	echo "Library installation failed, aborting"
	exit 1
fi
if [ -z "$INSTALL" ]
then
	echo "Making links"
	ln -sf /lib/libolg.so.$OLGMAJOR.$OLGMINOR /lib/libolg.so.$OLGMAJOR
	ln -sf /lib/libxv3.so.$XV3MAJOR.$XV3MINOR /lib/libxv3.so.$XV3MAJOR
	ln -sf /lib/libss.so.$SSMAJOR.$SSMINOR /lib/libss.so.$SSMAJOR
	ln -sf $OPENWINHOME/include/olgx /usr/include/olgx
	ln -sf $OPENWINHOME/include/pixrect /usr/include/pixrect
	ln -sf $OPENWINHOME/include/xview /usr/include/xview
	ln -sf $OPENWINHOME/include/sspkg /usr/include/sspkg
	ln -sf $OPENWINHOME/include/uit /usr/include/uit

	echo ""
	echo -n "Do you want to compile and install UIT examples? [y/n] "
	read answer
	case $answer in
	y*)
		(cd examples/UIT && make all install clean)
		;;
	*)
		echo "UIT examples skipped"
		;;
	esac	
	echo ""
	echo -n "Do you want to compile and install Slingshot examples? [y/n] "
	read answer
	case $answer in
	y*)
		(cd examples/Slingshot && make all install clean)
		;;
	*)
		echo "Slingshot examples skipped"
		;;
	esac
fi
echo "Done. Please read README for .xinitrc configuration. This package contains"
echo "a sample .xinitrc, named Xinitrc. If you want to use it, issue the command"
echo "\"cp Xinitrc \$HOME/.xinitrc\"."
exit 0
