#!/bin/sh
# $Header: /home/tv/src/debugger/rel/scripts/Install.rs6000,v 1.3 1998/12/11 18:47:07 jdelsign Exp $
#
# Bourne Shell script to install TotalView
#

#
# Make sure we get the right executables.
#
PATH=/usr/ucb:/bin:/usr/bin:/sbin:/usr/sbin

dists="DIST1.tar.Z DIST2.tar.Z"
files="Install DIST $dists"
hit_return='echo ""; echo "Type <CR> to continue...\c"; read xxx'
docs="Release_Notes.htm Release_Notes.txt README.TXT LICENSE.TXT Install_Guide.pdf Install_Guide.ps DEMO_Instructions.htm DEMO_Instructions.txt"

options=""

tv_home=/usr/totalview

yes_or_no ()	# args: question
{
    response=""
    while [ "$response" != y -a "$response" != n ]; do
	echo "$1 (y or n)? \c" >&2
	read response
    done
    echo $response
}

######################################################################

echo ""
echo "**********************************************************************"
echo ""
echo ""
echo "                          Install"
echo ""
echo ""
echo "Install is an interactive shell script to help you install"
echo "the TotalView(TM) debugger."
echo ""
echo "You may ^C out of this script at any time, and start again."
echo "When you are asked to type <CR>, press the 'Return' key."
eval $hit_return

######################################################################

echo ""
echo "**********************************************************************"
echo ""
echo "Checking for files needed to install TotalView..."
echo ""
failed=0
for i in $files; do
	echo $i": \c"
	if [ -f $i ]; then
		echo "OK."
	else
		echo "Does not exist."
		failed=1
	fi
done
echo ""
if [ $failed = 1 ]; then
	echo "You are missing some files, $0 aborted."
	exit 1
fi
eval $hit_return

######################################################################

rootdir="/usr/toolworks"
echo ""
echo "**********************************************************************"
echo ""
echo "Please enter the name of the directory to hold the TotalView"
echo "installation.  The directory you specify should already exist."
echo ""
echo "This installation script will create a directory named `cat DIST`"
echo "under the installation directory."
echo ""
typed_rootdir=""
while [ -z "$typed_rootdir" ]; do
	echo "Enter installation directory name [default $rootdir]: \c"
	read typed_rootdir
	if [ -z "$typed_rootdir" ]; then
		typed_rootdir=$rootdir
	fi
	if [ -d "$typed_rootdir" ]; then
		echo "Using directory $typed_rootdir"
		rootdir=$typed_rootdir
	elif mkdir "$typed_rootdir"; then
		echo "Created directory $typed_rootdir"
		rootdir=$typed_rootdir
	else
		echo "Error creating directory $typed_rootdir"
		typed_rootdir=""
	fi
done
eval $hit_return

######################################################################

target=$rootdir/`cat DIST`
echo ""
echo "**********************************************************************"
echo ""
echo "Checking for directory $target ..."
echo ""
if [ -d $target ]; then
	echo "Directory $target already exists, continuing..."
elif mkdir $target; then
	echo "Created directory $target."
else
	echo "Error creating directory $target, $0 aborted."
	exit 1
fi
eval $hit_return

######################################################################

echo ""
echo "**********************************************************************"
for dist in $dists; do
	echo ""
	echo "Uncompressing and extracting $dist..."
	zcat $dist | ( cd $target && tar xfvp - )
	eval $hit_return
done

######################################################################

full_target=$target/rs6000
case $target in
/* )	;;
*  )	full_target=`pwd`/$full_target ;;
esac
echo ""
echo "**********************************************************************"
echo ""
echo "$tv_home should be a symbolic link to the directory"
echo ""
echo "    $full_target"
echo ""
response=`yes_or_no "Create the $tv_home symbolic link"`
if [ $response = y ]; then
if [ -h $tv_home ]; then
	echo "$tv_home link already exists.  Recreating the link..."
	rm -f $tv_home
	if ln -s $full_target $tv_home; then
		echo "$tv_home link created."
	else
		echo "ERROR: Failed creating $tv_home link."
	fi
elif [ -d $tv_home -o -f $tv_home ]; then
	echo "ERROR: $tv_home already exists."
else
	echo "$tv_home does not exist.  Creating the link..."
	echo ""
	if ln -s $full_target $tv_home; then
		echo "$tv_home link created."
	else
		echo "ERROR: Failed creating $tv_home link."
	fi
fi
eval $hit_return
fi

######################################################################

echo ""
echo "**********************************************************************"
echo ""
echo "Copying installation documents to $full_target/docs"
echo ""
mkdir -p $full_target/docs
for i in $docs; do
	echo "$i"
	cp $i $full_target/docs/$i
done

eval $hit_return

######################################################################

echo ""
echo "**********************************************************************"
echo ""
echo "Installation complete."
echo ""
echo "Users should add $tv_home/bin to their PATH."
echo ""
echo "Users should add $tv_home/man to their MANPATH."
echo ""
echo "Users should use -L$tv_home/lib -bkeepfile:$tv_home/lib/libdbfork.a"
echo "when linking with -ldbfork."
echo ""
