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

# Location of XFree
X11HOME=/usr/X386
OPENWINHOME=/usr/openwin; export OPENWINHOME
OLDLIBFILE=/usr/tmp/xvinst.tmp

if [ -x /usr/bin/clear ]; then
	/usr/bin/clear
fi
echo "XView Linux port, release 5.1. Starting installation"
echo ""

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

if [ -d $OPENWINHOME -o -f /usr/lib/libxview.a -o -f /lib/libxv3.so.1 ]
then

	if [ ! -f $OPENWINHOME/lib/libolg.so.2.? ]; then
		keepoldmajor=0
	else
		echo "You seem to have a previous version of the shared libs installed."
		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
		[yY]*)
			keepoldmajor=1
			;;
		*)
			echo "Will remove old libraries"
			keepoldmajor=0
			;;
		esac
	fi

	if [ $keepoldmajor -eq 1 ]; then
		echo "Storing old shared libs"
		(tar cvf - $OPENWINHOME/lib/*.so.* | compress >$OLDLIBFILE)
		if [ $? -ne 0 ]; then
			echo "Failed to store old libs, aborting"
			exit 1
		fi
	fi

	echo -n "The old XView installation will be erased, please confirm: [y/n] "
	read answer
	case $answer in
	[yY]*)
		;;
	*)
		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/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
		echo "Removing old jump libraries"
		rm -f /lib/libolg.so.*
		rm -f /lib/libxv3.so.*
		rm -f /lib/libss.so.*
	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

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 [ -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
else
	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:"
cat libs.tar | (cd /; tar xvf -)
if [ $? -ne 0 ]
then
	echo "Library installation failed, aborting"
	exit 1
fi
echo "Compacting libraries to decrease disk space utilization"
dubefore=""
if [ -x /usr/bin/du ]; then
	dubefore="`du -s $OPENWINHOME/lib | awk '{print $1}'`"
fi
for f in $OPENWINHOME/lib/lib*.so.?.?
do
	bname=`basename $f`
	cp $f $OPENWINHOME/lib/t$bname || exit 1
	rm -f $f
	mv $OPENWINHOME/lib/t$bname $f
done
if [ -x /usr/bin/du ]; then
	duafter="`du -s $OPENWINHOME/lib | awk '{print $1}'`"
	echo "Saved `expr $dubefore - $duafter` kB in library compaction"
	unset dubefore duafter
fi

# Check for ld.so config file, and make sure root is in the path
if [ -f /etc/ld.so.conf ]; then
	echo ""
	rf=`awk 'BEGIN {f=0} $0=="/" {f=1} END {print f}' </etc/ld.so.conf`
	if [ "$rf" -eq 0 ]; then
		echo "Adding / to list of library directories"
		echo "/" >/tmp/ld.so.conf
		cat /etc/ld.so.conf >>/tmp/ld.so.conf
		mv /tmp/ld.so.conf /etc/ld.so.conf
		if [ $? -ne 0 ]; then
			echo "Failed to update /etc/ld.so.conf, aborting"
			exit 1
		fi
	fi
	unset rf
#	if [ -x /etc/ldconfig ]; then
#		ldc=/etc/ldconfig
#	elif [ -x /sbin/ldconfig ]; then
#		ldc=/sbin/ldconfig
#	fi
#	if [ ! -z "$ldc" ]; then
#		echo "Updating shared library links"
#		$ldc -v
#	fi
fi

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/lib"
echo ""
echo -n "Do you want to install the static libraries? [y/n] "
read answer
case $answer in
[yY]*)
	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/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

if [ $keepoldmajor -eq 1 ]; then
	echo "Restoring old shared libs"
	(cd / && zcat $OLDLIBFILE | tar xvf -)
	if [ $? -ne 0 ]; then
		echo "Failed to restore old libs, aborting."
		echo "Old libs are available in $OLDLIBFILE (a compressed tarfile)"
		exit 1
	fi
fi

echo ""
if [ ! -f $HOME/.Xmodmap -o -z "`grep F18 $HOME/.Xmodmap`" ]; 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 your Xmodmap file."
	echo -n "Should I append to \$HOME/.Xmodmap? [y/n] "
	read answer
	case $answer in
	[yY]*)
		if [ ! -f $HOME/.Xmodmap ]; then
			cat Xmodmap >$HOME/.Xmodmap && echo "Created \$HOME/.Xmodmap"
		else
			cat Xmodmap >>$HOME/.Xmodmap && echo "Appended to \$HOME/.Xmodmap"
		fi
		;;
	*)
		echo "Will not change Xmodmap"
		;;
	esac
fi

echo ""
echo "Compilation of the XView, SlingShot and UIT examples takes a few hours"
echo "(on a reasonably fast system)."
echo -n "Do you want to build and install the example programs? [y/n] "
read answer
case $answer in
[yY]*)
	IMAKEINCLUDE="-I$OPENWINHOME/lib/config -I$X11HOME/lib/X11/config"
	export IMAKEINCLUDE
	(cd src && make all install clean) 2>&1 | tee examples.log
	echo ""
	echo "Build of examples finished."
	echo "A log of the compile output is in \"examples.log\""
	;;
*)
	echo "Examples build skipped"
	echo ""
	echo "Note! If you want to build the examples at a later time, see the"
	echo "Install script (this program) on how to do it."
	;;
esac

echo ""
echo "Install done. Please note that it is recommended to use the .xinitrc provided"
echo "in 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
