
#! /bin/sh
# setup script
umask 0000
# figure out how to echo without a linefeed
NC=
BC=
n=`echo -n`
if [ -z "$n" ]
then NC="-n"
else BC="\\c"
fi

if [ $# != 5 ]
then echo "
User setup script 

This script sets up the proper environment variables to run QSP products- 
For each of the following questions, a default answer may be shown 
in brackets.  To select the default answer, press RETURN in response 
to the question.
"
	echo "Product(s) available for user setup in this directory:"
	echo
	if [ -d exclaim ]
	then
		if [ -d mpx ]
		then
			echo "1) eXclaim!"
			echo "2) MASTERPLAN"
			numprod=2
		else prodnum=1
		fi
	else
		if [ -d mpx ]
		then prodnum=2
		else 
			echo "(none)"
			errormsg=y
		fi
	fi
	if [ "$numprod" = "2" ]
	then 
		echo 
		echo $NC "Please enter '1' or '2': $BC"
		read prodnum
	fi
	if [ "$prodnum" = "1" ]
		then
		echo "eXclaim!"
		binaryname=exclaim
		PRODUCT="eXclaim!"
	elif [ "$prodnum" = "2" ]
	then
		echo "MASTERPLAN"
		binaryname=mpx
		PRODUCT="MASTERPLAN"
	fi
else
	binaryname=$PRODUCT
	if [ "$PRODUCT" = "mpx" ]
	then 
		PRODUCT="MASTERPLAN"
	fi
	if [ "$PRODUCT" = "exclaim" ]
	then 
		PRODUCT="eXclaim!"
	fi
fi

DEFS=/tmp/.$binaryname/setup_defaults
THISMACH=`(hostname) 2> /dev/null`
if [ $? != 0 ]
then 
     THISMACH=`uname`
fi
if [ "$PWD" = "" ]; then
        HERE=`pwd`
else
        HERE=$PWD
fi
error=n

if [ "$QSROOT" = "" ]
then qsroot=$HERE
else qsroot=$QSROOT
fi
if [ $# = 5 ]			# if called from a script...
then
     ARCH=$1
     MDSERVER_HOST=$2
     MDSERVER_MASTER=$3
     autoflag=$4
     qsroot=$5
elif [ -f $DEFS ]		# if there is a default file, use it
then
#     if [ "$QSROOT" = "" ]
#     then qsroot=`grep QSROOT $DEFS | awk -F"=" '{print $2}'`
#     fi
     if [ "$ARCH" = "" ]
     then ARCH=`grep ARCH $DEFS | awk -F"=" '{print $2}'`
     fi
     if [ "$MDSERVER_HOST" = "" ]
     then MDSERVER_HOST=`grep MDSERVER_HOST $DEFS | awk -F"=" '{print $2}'`
     fi
     if [ "$MDSERVER_MASTER" = "" ]
     then MDSERVER_MASTER=`grep MDSERVER_MASTER $DEFS | awk -F"=" '{print $2}'`
     fi
fi
#query qsroot - pwd
if [ ! -d mpx -a ! -d exclaim ]
then
#     echo "Warning: the current directory is an invalid $PRODUCT directory"
#     if [ "$autoflag" != "" ]
#     then
#          echo "This error was probably caused by:"
#          echo
#	  errmsg1=y
	  errmsg=y
#	  exit 1
#     else
#	  if [ -d "$QSROOT" ]
#	  then
#	       echo $NC "Use $QSROOT as the $PRODUCT directory? [y] $BC"
#	       read ans
#	       if [ "$ans" != "y" -a "$ans" != "" ]
#	       then exit 1
#	       fi
#	       cd $QSROOT
#	       if [ ! -d mpx -a ! -d exclaim ]
#	       then 
#		    echo "Warning: no \"mpx\" directory in $QSROOT -"
#	            echo "This error was probably caused by:"
#		    echo
#		    errmsg2=y
#		    errmsg=y
#	            exit 1
#	       fi
#	  else
#	       echo "I don't know where the main $PRODUCT directory is."
#	       echo "This error was probably caused by:"
#	       echo
#	       errmsg=y
#	       exit 1
#	  fi
#     fi
fi
	
ls -d bin.* > /tmp/qspbins
#numbins=`wc -l /tmp/qspbins | sed -e 's/^ *//' | cut -d" " -f1`
numbins=`grep -c "^" /tmp/qspbins | awk '{print $1}'`
case $numbins in
     0) 
#	echo "Could not find a bin.{machine} directory."
#        echo "This error was probably caused by:"
#	echo
#	errmsg2=y
#	errmsg1=y
	errmsg=y
#	exit 1
	;;
     1) ARCH=`cat /tmp/qspbins | awk -F"." '{print $NF}'`
        break;;
esac

#if [ "$errmsg2" = "y" ]
#then echo " - running the setup script from a directory other than the main 
#	$PRODUCT directory.  The main $PRODUCT directory should 
#	contain the \"mpx\" directory and at least one
#	bin.{machine} directory.  You should cd to the main 
#	$PRODUCT directory before running this script.
#
#        Also, the $PRODUCT directory tree could have been moved 
#	since the last time mpx was successfully run.  You 
#	should cd to the new main $PRODUCT directory before 
#	running this script.
#"
#fi
#if [ "$errmsg1" = "y" ]
#then echo " - a tape or disk read error, or improperly copied files.  If this
#        script is being run as part of the installation from tape
#        or disk, or if the $PRODUCT files were copied from
#        elsewhere, the \"mpx\" and/or bin.{machine} directories
#        were not copied or read properly from the media.  Please
#        remove all files in the main $PRODUCT directory and try
#        installing again from the $PRODUCT installation media.
#"
#fi
if [ "$errmsg" = "y" ]
then echo "
Warning: the current directory is not a completed installation tree.
This error was caused by:

1) running the setup script from a directory other than the main
        installation directory.  The main installation directory 
	must contain at least one bin.{machine} subdirectory and at 
	least one product subdirectory, e.g. \"exclaim\" or \"mpx.\"

2) the installation tree being moved since the last time the 
	product was successfully run.  You should cd to the new 
	directory before running this script.

3) - a tape or disk read error, or improperly copied files.  If 
	this script is being run as part of the installation from 
	tape or disk, or if the files were copied from elsewhere, 
	the product and/or bin.{machine} subdirectories were not 
	copied or read properly from the media.  Please remove all 
	files in the main installation directory and try installing 
	again from the installation media.
"
#if [ "$errmsg2" = "y" -o "$errmsg1" = "y" ]
#if [ "$errmsg" = "y" ]
#then 
exit 1
fi


#tests for architecture 
if [ $# != 5 -a "$ARCH" = "" ]
then
while true
do
#   if [ -x /bin/arch ]				# sun
   if [ -f /bin/arch ]				# sun
   then 
	ARCH=`/bin/arch`
	break
   fi
   (m88k) 2> /dev/null				# aviion
#   if [ -x /dgux -a $? = 0 ]
   if [ -f /dgux -a $? = 0 ]
   then
	ARCH=aviion
   break
   fi
   (i386) 2> /dev/null				# odt
   odtgrep=`grep "OpenDesk Top" /etc/passwd`
   if [ -n "$odtgrep" -a $? = 0 ]
   then
	ARCH=odt
	break
   fi 
   (hp9000s300) 2> /dev/null			# hp
#   if [ -x /hp-ux -a $? = 0 ]
   if [ -f /hp-ux -a $? = 0 ]
   then
	ARCH=hp300
	break
   fi
   ARCH=unknown
   break
done
fi

arch=$ARCH
while true
do
   if [ "$autoflag" != "" ]
   then break
   fi
   archknow=`grep $arch /tmp/qspbins`
   if [ "$archknow" = "" ]
   then echo "This machine type ($arch) is either unknown"
	echo "or does not have a match in the following list of "
	echo "machine-specific sets in $qsroot:"
	echo
#	cat /tmp/qspbins | cut -d"." -f2
	cat /tmp/qspbins | awk -F"." '{print $NF}'
	echo
	echo $NC "Continue? [y] $BC"
	read ans
	if [ "$ans" != "y" -a "$ans" != "" ]
	then exit1
	fi
	echo $NC "Please choose a machine type [$ARCH]: $BC"
	read arch
	if [ "$arch" = "" ]
	then arch=$ARCH
	fi
	if [ -d $qsroot/bin.$arch ]
	then break
	else echo "$qsroot/bin.$arch does not exist..."
	fi
   else echo
	break
   fi
done

# query mdserver_host
mdserver_host=$MDSERVER_HOST
while true 
do
   if [ "$autoflag" != "" ]
   then break
   fi
   if [ "$MDSERVER_HOST" = "" ]
   then MDSERVER_HOST=$THISMACH
   fi
   echo $NC "Please enter the Maitre D' server host [$MDSERVER_HOST]: $BC"
   read mdserver_host
   if [ "$mdserver_host" = "" ]
   then 
	if [ "$MDSERVER_HOST" = "" ]
	then continue
	else mdserver_host=$MDSERVER_HOST
	     break
	fi
   else
        echo $NC "Maitre D' server host is: $mdserver_host - $BC"
	echo $NC "OK? [y] $BC" 
	read ans
	if [ "$ans" = "y" -o "$ans" = "" ]
        then break
        else continue
        fi
   fi
done

# query mdserver_master
mdserver_master=$MDSERVER_MASTER
while true
do
   if [ "$autoflag" != "" ]
   then break
   fi
   if [ "$MDSERVER_MASTER" = "" ]
   then MDSERVER_MASTER=$THISMACH
   fi
   echo $NC "Please enter the master Maitre D' server [$MDSERVER_MASTER]: $BC"
   read mdserver_master
   if [ "$mdserver_master" = "" ]
   then 
	if [ "$MDSERVER_HOST" = "" ]
	then continue
	else mdserver_master=$MDSERVER_MASTER
	     break
	fi
   else
        echo $NC "The master Maitre D' server is: $mdserver_master - $BC"
        echo $NC "OK? [y] $BC"
        read ans
        if [ "$ans" = "y" -o "$ans" = "" ]
        then break
        else continue
        fi
   fi
done

# Create /tmp/.$binaryname/setup_defaults file if needed, and/or modify settings
#if [ ! -d /tmp/.mpx ]
#then mkdir /tmp/.mpx
#fi
#if [ ! -f /tmp/.mpx/setup_defaults ]
#then
#     echo "No $DEFS found..."
#QSROOT=$QSROOT
#ARCH=$ARCH
#MDSERVER_HOST=$MDSERVER_HOST
#MDSERVER_MASTER=$MDSERVER_MASTER
#" | cat > /tmp/.mpx/setup_defaults
#else
#     QSROOTeq=`grep QSROOT /tmp/.mpx/setup_defaults | cut -d= -f2`
#     ARCHeq=`grep ARCH /tmp/.mpx/setup_defaults | cut -d= -f2`
#     HOSTeq=`grep MDSERVER_HOST /tmp/.mpx/setup_defaults | cut -d= -f2`
#     MASTEReq=`grep MDSERVER_MASTER /tmp/.mpx/setup_defaults | cut -d= -f2`
#     if [ "$QSROOTeq" = "" ]
#     then echo "QSROOT=$QSROOT" | cat >> /tmp/.mpx/setup_defaults
#     fi
#     if [ "$ARCHeq" = "" ]
#     then echo "ARCH=$ARCH" | cat >> /tmp/.mpx/setup_defaults
#     fi
#     if [ "$HOSTeq" = "" ]
#     then echo "MDSERVER_HOST=$MDSERVER_HOST" | cat >> /tmp/.mpx/setup_defaults
#     fi
#     if [ "$MASTEReq" = "" ]
#     then echo "MDSERVER_MASTER=$MDSERVER_MASTER" | cat >> /tmp/.mpx/setup_defaults
#     fi
#fi

# CONFIGURATION
echo
echo "$PRODUCT - profile and cshrc settings:"
addmsg=no

qsrootp=`grep "QSROOT=$qsroot;" $HOME/.profile 2> /dev/null`
archp=`grep "$qsroot/bin\.$arch" $HOME/.profile 2> /dev/null`
hostp=`grep "MDSERVER_HOST=$mdserver_host" $HOME/.profile 2> /dev/null`
masterp=`grep "MDSERVER_MASTER=$mdserver_master" $HOME/.profile 2> /dev/null`

echo

qsrootc=`grep "setenv QSROOT $qsroot\$" $HOME/.cshrc 2> /dev/null`
archc=`grep "$qsroot/bin\.$arch" $HOME/.cshrc 2> /dev/null`
hostc=`grep "MDSERVER_HOST $mdserver_host\$" $HOME/.cshrc 2> /dev/null`
masterc=`grep "MDSERVER_MASTER $mdserver_master\$" $HOME/.cshrc 2> /dev/null`

#profileif=n
#if [ -z "$qsrootp" -o -z "$archp" -o -z "$hostp" -o -z "$masterp" ]
#then profileif=y
#fi
#cshrcif=n
#if [ -z "$qsrootc" -o -z "$archc" -o -z "$hostc" -o -z "$masterc" ]
#then cshrcif=y
#fi

#if [ "$profileif" = "y" ]
#then 




if [ -z "$qsrootp" -a "$autoflag" != "md" ]; then
echo "    QSROOT=$qsroot; export QSROOT" > /tmp/$$profile
fi
if [ -z "$archp" -a "$autoflag" != "md" ]; then
echo "    PATH=$qsroot/bin.$arch:\$PATH; export PATH" >> /tmp/$$profile
fi
if [ -z "$hostp" -a "$autoflag" != "md" ]; then
echo "    MDSERVER_HOST=$mdserver_host; export MDSERVER_HOST" >> /tmp/$$profile
fi
if [ -z "$masterp" -a "$autoflag" != "md" ]; then
echo "    MDSERVER_MASTER=$mdserver_master; export MDSERVER_MASTER" >> /tmp/$$profile
fi
if [ -f /tmp/$$profile ]
then
     addmsg=y
     echo "Do you wish to add the following line(s):"
     cat /tmp/$$profile
     echo $NC "to $HOME/.profile? [y] $BC"
     read ans
     if [ "$ans" = "y" -o "$ans" = "" ]
     then continue
     else rm /tmp/$$profile
     fi
     echo
fi

if [ -z "$qsrootc" -a "$autoflag" != "md" ]; then
echo "    setenv QSROOT $qsroot" > /tmp/$$cshrc
fi
if [ -z "$archc" -a "$autoflag" != "md" ]; then
if [ "$path" != "" ]
then echo "    set path= ( $qsroot/bin.$arch \$path )" >> /tmp/$$cshrc
else echo "    setenv PATH $qsroot/bin.$arch:\$PATH" >> /tmp/$$cshrc
fi
fi
if [ -z "$hostc" -a "$autoflag" != "md" ]; then
echo "    setenv MDSERVER_HOST $mdserver_host" >> /tmp/$$cshrc
fi
if [ -z "$masterc" -a "$autoflag" != "md" ]; then
echo "    setenv MDSERVER_MASTER $mdserver_master" >> /tmp/$$cshrc
fi
if [ -f /tmp/$$cshrc ]
then
     addmsg=y
     echo "Do you wish to add the following line(s):"
     cat /tmp/$$cshrc
     echo $NC "to $HOME/.cshrc? [y] $BC"
     read ans
     if [ "$ans" = "y" -o "$ans" = "" ]
     then continue
     else rm /tmp/$$cshrc
     fi
     echo
fi

if [ "$addmsg" != "y" ]
then echo "No additions to your .profile or .cshrc are needed."
fi

reminder=n
if [ -f /tmp/$$profile -o -f /tmp/$$cshrc ]
then 
     reminder=y
     if [ -f /tmp/$$profile ]; then
     while true
     do
	echo $NC "Modifying $HOME/.profile...$BC"
	cat /tmp/$$profile >> $HOME/.profile
	if [ $? != 0 ]
	then echo; echo "Could not append to $HOME/.profile..."
	else echo "Done."
	     break
	fi
	echo $NC "Try again? [n] $BC"
	read ans
	if [ "$ans" = "n" -o "$ans" = "" ]
	then break
	fi
     done
     fi
     if [ -f /tmp/$$cshrc ]; then
     while true
     do
	echo $NC "Modifying $HOME/.cshrc...$BC"
	cat /tmp/$$cshrc >> $HOME/.cshrc
	if [ $? != 0 ]
	then echo; echo "Could not append to $HOME/.cshrc..."
	else echo "Done."
	     break
	fi
        echo $NC "Try again? [n] $BC"
        read ans
        if [ "$ans" = "n" -o "$ans" = "" ]
        then break
	fi
     done
     fi
     echo
fi
autoraise=`grep "$binaryname.focusAutoRaise:" $HOME/.Xdefaults`
if [ -z "$autoraise" -a "$autoflag" != "md" ]
then
     echo "The following resource should be effective when your window manager is started:"
     echo "==> *$binaryname*focusAutoRaise: false"
     echo "Do you wish to have this resource added to your"
     echo $NC "$HOME/.Xdefaults file? [y] $BC"
     read ans
     if [ "$ans" = "y" -o "$ans" = "" ]
     then
          echo
          echo "Modifying $HOME/.Xdefaults..."
          while true
          do
             echo "*$binaryname*focusAutoRaise: false
" | cat >> $HOME/.Xdefaults
             if [ $? != 0 ]
             then
                  echo "Could not append to $HOME/.Xdefaults..."
             else
                  echo "Appended to $HOME/.Xdefaults."
                  break
             fi
             echo $NC "Try again? [n] $BC"
             read ans
             if [ "$ans" = "n" -o "$ans" = "" ]
             then break
             fi
          done
          echo
          echo "In order to make your new .Xdefaults resource setting(s)"
          echo "effective, you should exit your window system and re-start."
     fi
     echo
fi
xdefif=`grep ".:." $HOME/.Xdefaults 2> /dev/null`
if [ -z "$xdefif" ]
then
echo "$binaryname.placeholder: x 	# hp and odt do not work properly 
				# without at least one a priori resource
" > $HOME/.Xdefaults
fi

if [ "$reminder" = "y" ]
then
     echo "In order to make your new .cshrc and .profile setting(s)"
     echo "effective, you should log out and log in again before"
     echo "running $PRODUCT"
     echo
fi
echo "User setup complete."

