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

# Location of XFree
X11HOME=/usr/X386

# These must correspond with contents of libs.tar
OLGMAJOR=2
OLGMINOR=0
XV3MAJOR=2
XV3MINOR=0
SSMAJOR=2
SSMINOR=0
UITMAJOR=1
UITMINOR=0
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.1 ]
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 [ ! -f /lib/libolg.so.1.? ]; then
		keepoldmajor=0
	else
		echo ""
		echo "You seem to have a previous version of the shared libs in /lib"
		echo "If those are removed, any executables linked with them will stop"
		echo 'working. If you have xview applications *not* from this package,'
		echo "they will need to be relinked before removing the old libraries."
		echo -n "Do you want to keep the old shared libs [y/n] "
		read answer
		case $answer in
		y*)
			keepoldmajor=1
			;;
		Y*)
			keepoldmajor=1
			;;
		*)
			echo "Will remove old libraries"
			keepoldmajor=0
			;;
		esac
	fi

	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/libolgx.a
		rm -f /usr/lib/libxview.a
		rm -f /usr/lib/libsspkg.a
	fi
	if [ -f /usr/lib/shlib/jump/libxview.a -o -f /lib/libolg.so.1 -o -f /lib/libolg.so.1.? ]
	then
		echo "Removing old stub 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 /usr/lib/libolgx.sa
		rm -f /usr/lib/libxview.sa
		rm -f /usr/lib/libsspkg.sa
		if [ $keepoldmajor -eq 0 ]; then
			echo "Removing old jump libraries"
			rm -f /lib/libolg.so.*
			rm -f /lib/libxv3.so.*
			rm -f /lib/libss.so.*
		fi
	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

echo "Installing miscellaneous files"
if [ ! -f /usr/lib/.text_extras_menu ]; then
	ln -sf $OPENWINHOME/lib/.text_extras_menu /usr/lib/.text_extras_menu
fi
if [ ! -f $HOME/.Xdefaults ]; then
	cp Xdefaults $HOME/.Xdefaults && echo "\$HOME/.Xdefaults"
fi
if [ ! -f $HOME/.openwin-init ]; then
	cp Openwin-init $HOME/.openwin-init && echo "\$HOME/.openwin-init"
fi
if [ -x $OPENWINHOME/bin/openwin -a -d $X11HOME/bin ]; then
	ln -sf $OPENWINHOME/bin/openwin $X11HOME/bin/openwin
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 ""
	echo "Static versions of the libraries are needed if you are developing your own"
	echo "xview applications and will do debugging, or if you use the -static flag"
	echo "flag when compiling an xview application using gcc. If you will not be"
	echo "building your own xview programs, the static libraries will never be used."
	echo "The static libs will occupy about 2MB diskspace in /usr/openwin"
	echo ""
	echo -n "Do you want to install the static libraries? [y/n] "
	read answer
	case $answer in
	y*)
		inststatic=1
		;;
	Y*)
		inststatic=1
		;;
	*)
		inststatic=0
		;;
	esac	
	if [ $inststatic -ne 0 ]; then
		echo "Installing static libs"
		cat slibs.tar | (cd /; tar xvf -)
		if [ $? -ne 0 ]
		then
			echo "Library installation failed, aborting"
			exit 1
		fi
	else
		echo "Skipped static libs"
	fi

	echo "Making links"
	ln -sf $OPENWINHOME/lib/libolg.so.$OLGMAJOR.$OLGMINOR /lib/libolg.so.$OLGMAJOR
	ln -sf $OPENWINHOME/lib/libxv3.so.$XV3MAJOR.$XV3MINOR /lib/libxv3.so.$XV3MAJOR
	ln -sf $OPENWINHOME/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 ""
	if [ -d $X11HOME/lib/X11/xinit ]; then
		echo "This package contains a xmodmap which redefines F1-F5 as special keys"
		echo "which allows you to do copy/paste etc. using the keyboard. If you want,"
		echo "the definitions will be appended to xmodmap file."
		echo -n "Should I append to $HOME/.Xmodmap? [y/n] "
		read answer
		case $answer in
		y*)
			if [ ! -f $HOME/.Xmodmap ]; then
				cat Xmodmap >$HOME/.Xmodmap && echo "Created \$HOME/.Xmodmap"
			elif [ -z "`grep F18 $HOME/.Xmodmap`" ]; then
				cat Xmodmap >>$HOME/.Xmodmap && echo "Appended to \$HOME/.Xmodmap"
			else
				echo "Keydefs already present, xmodmap not changed"
			fi
			;;
		*)
			echo "Will not change xmodmap"
			;;
		esac
	fi

	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 delete)
		;;
	*)
		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 ""
echo "Done. Please note that it is recommended to use the .xinitrc provided in"
echo "this package to allow both \"startx\" and \"openwin\" to work correctly"
echo ""
echo "You should now be able to start X using the \"openwin\" command"
exit 0
