
PATH=$PATH:/bin:/usr/bin:/etc:/usr/etc
export PATH
# figure out how to echo without a linefeed
BC=
NC=
n=`echo -n`
if [ -z "$n" ]
then NC="-n"
else BC="\\c"
fi
export BC NC

here=`pwd`
selfname="`echo $0 | awk -F/ '{print $NF}'`"
umask 0000

if [ $# = 0 ]
then 
     if [ "$QSROOT" = "" ]
     then
	  echo
          echo "The environment variable QSROOT is not set,"
	  echo "and no argument to $selfname was given."
	  echo
	  echo "Please enter the root directory"
	  echo $NC "of the installation [/usr/qsp]: $BC"
	  read QSROOT
     fi
else
     QSROOT=$1
fi

if [ $# = 2 ]
then autoflag=md
else autoflag=nonauto
fi

while true
do
   if [ "$QSROOT" = "" ]
   then QSROOT=/usr/qsp
   fi
   touch $QSROOT/maitred/test 2> /dev/null
   if [ ! -d $QSROOT ]
   then echo "The directory \"$QSROOT\" does not exist."
   elif [ ! -d $QSROOT/maitred ] 
   then echo "\"$QSROOT/maitred\" not found - "
	echo "\"$QSROOT\" is not a valid installation directory."
   elif [ ! -f $QSROOT/maitred/test ]
   then echo "\"$QSROOT/maitred\" is not writeable."
   else
	rm -f $QSROOT/maitred/test 2> /dev/null
	cd $QSROOT
        ls -d bin.* > /tmp/qspbins
	cd $here
        numbins=`grep -c "^" /tmp/qspbins | awk '{print $1}'`
	case $numbins in
		0) echo "No $QSROOT/bin.{MACHINE} directory... " ;;
	   	1) MACHINE=`cat /tmp/qspbins | awk -F"." '{print $NF}'` 
		   break;;
	   	*)
	   	 echo "There is more than one set of executables in $QSROOT: "
	   	 while true
	   	 do
	   	   echo
	   	   cat /tmp/qspbins | awk -F"." '{print $NF}'
	   	   echo
	   	   echo $NC "Please choose a machine type: $BC"
	   	   read MACHINE
	   	   if [ -d $QSROOT/bin.$MACHINE ]
	   	   then break
			machineflag=n
	   	   else echo "$QSROOT/bin.$MACHINE does not exist..."
	   	   fi
	   	 done 
		 break;;
	esac
   fi
   echo $NC "Do you wish to specify another installation directory? [y] $BC"
   read ans
   if [ "$ans" = "y" -o "$ans" = "" ]
   then 
	echo "Please enter the root directory"
	echo $NC "of the installation [/usr/qsp]: $BC"
	read QSROOT
   else echo "Leaving Maitre D' license configuration."
	exit 1
   fi
done

QSPQUERY=$QSROOT/scripts/qspquery
QSPDATA=/tmp/qspdata
# load in product-specific and machine-specific info
if [ -f $QSPDATA ]
then 
     . $QSPDATA
else
	cd $QSROOT
	if [ -f /bin/sh5 ]
	then SH=/bin/sh5
	fi
	if [ -d exclaim ]
	then
		if [ -d mpx ]
		then
			echo "Which product would you like to license?"
			echo
			echo "1) eXclaim!"
			echo "2) MASTERPLAN"
			numprod=2
		else prodnum=1
		fi
	else
		if [ -d mpx ]
		then prodnum=2
		else 
			"No product to license in $QSROOT!"
			"Exiting..."
			exit
		fi
	fi
	if [ "$numprod" = "2" ]
	then 
		echo 
		echo $NC "Please enter '1' or '2': $BC"
		read prodnum
	fi
	if [ "$prodnum" = "1" ]
		then
		PRODUCT=exclaim
	elif [ "$prodnum" = "2" ]
	then
		PRODUCT=mpx
	fi
	cd $here

	VERSION=1.1
	machstring=`grep ARCH /tmp/.$PRODUCT/setup_defaults 2> /dev/null`
	if [ -z "$machstring" ]
	then machineflag=y
	else MACHINE=`echo $machstring | awk -F"=" '{print $2}'`
	fi
fi

echo
echo "If you need to obtain a password, or you have already obtained a password"
echo "and wish to set up the master license server, please continue."
echo
echo "If you don't have a license, and you only need to run $PRODUCT in"
echo "demonstration mode, you may exit now and run $PRODUCT."
echo
echo "If the master license server is already installed and your license is"
echo "configured, you may exit now."
echo "
Note: You may configure a license later by 
   1. killing the 'mdserver' process, if it exists
   2. removing $QSROOT/maitred/md_state, if it exists
   3. executing: 

	$SH $QSROOT/maitred/$selfname $QSROOT
"
echo $NC "<Type 'q' and RETURN to quit, or just RETURN to continue>$BC"
read x
if [ "$x" = "q" ]
then
     echo "Leaving Maitre D' license configuration."
     echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
exit 1
fi

QSROOT=$QSROOT
export QSROOT
echo
if $QSPQUERY "Do you need to obtain a password from your software vendor?" "y" "n"
then
     while true
     do
	echo "(Determining ID information)..."
	echo
	$QSROOT/bin.$MACHINE/mdinfo 2> /dev/null
	if [ $? != 0 ]
	then 
		sleep 1
		echo
		echo "Could not execute {QSROOT}/bin.{MACHINE}/mdinfo,"
		echo
		echo "    where QSROOT is: $QSROOT"
		echo "    and MACHINE is the machine type: $MACHINE"
		echo
		echo $NC "Would you like to:
1 - try again
2 - change the QSROOT and/or MACHINE and try again
3 - continue with the license configuration
4 - exit

[1,2,3,4 - default 1]: $BC"
		read ans
		case $ans in
		2)
		   while true
		   do 
		      echo $NC "Please enter the QSROOT [$QSROOT]: $BC"
		      read qsr
		      if [ "$qsr" = "" ]
		      then echo
		      else QSROOT=$qsp
			   echo
		      fi
		      echo "List of machine-specific executable sets:"
		      echo
		      ls -d $QSROOT/bin.*
		      echo
#		      numbins=`ls -d $QSROOT/bin.* | wc -l | sed -e 's/^ *//' | cut -d" " -f1`
		      numbins=`ls -d $QSROOT/bin.* | grep -c "^" | awk '{print $1}'`
		      if $QSPQUERY "Continue?" "y" "n"
		      then
			   echo
		      else
			   echo "Leaving Maitre D' license configuration."
			   echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
			   exit 1
		      fi
		      case $numbins in
			0) echo "Invalid QSROOT $QSROOT."
			   echo "No directory(ies): $QSROOT/bin.{MACHINE}."
			   if $QSPQUERY "Continue?" "y" "n"
			   then
				echo
			   else
				echo "Leaving Maitre D' license configuration."
				echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
				exit 1
			   fi;;
#			1) MACHINE=`ls -d $QSROOT/bin.* | cut -d"." -f2`
			1) MACHINE=`ls -d $QSROOT/bin.* | awk -F"." '{print $NF}'`
			   break;;
			*) break;;
		      esac
		   done
		   if [ $numbins != 1 ]
		   then
		        echo "Please enter one of the extensions following '.bin' -"
		        echo $NC "Machine type: [$MACHINE] $BC"
    		        read mach
		        if [ "mach" = "" ]
		        then echo 
		        else MACHINE=$mach
		     	    echo
		        fi
		   fi
		   ;;
		3)
		   break;;
		4)
		   echo "Leaving Maitre D' license configuration."
		   echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
		   exit 1;;
		*)
		   continue;;
		esac
	else
		echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
		echo $NC "<Type 'c' and RETURN to continue, or just RETURN to quit>$BC"
		read x
		if [ "$x" = "c" ]
		then
		     break
		else
		     echo "Leaving Maitre D' license configuration."
		     exit 1
		fi
	fi
     done
fi

host=`$QSROOT/bin.$MACHINE/mdinfo | grep "System Hostname" | awk -F":" '{print $2}' | awk '{print $1}'`

while true 
do
   while true 			# password loop
   do
      echo $NC "Please enter your Maitre D' password: $BC"
      read password
      if [ "$password" = "" ]
      then
	   if $QSPQUERY "Do you wish to quit?" "n" "y"
	   then continue
	   else 
		echo "Leaving Maitre D' license configuration."
		echo "
    Note: You may configure a license later by running
    $SH $QSROOT/maitred/$selfname $QSROOT
"
		exit 1
	   fi
      fi
      if $QSPQUERY "Your password is: $password - OK?" "y" "n"
      then
	   break
      fi
   done
   product=$PRODUCT
   if [ "$PRODUCT" = "" ]
   then product=$DEFPROD
   fi
   while true
   do
      if $QSPQUERY "The product is $product - OK?" "y" "n"
      then
	   break
      else
	   echo $NC "Please enter product: $BC"
	   read product
      fi
   done
   version=$VERSION
   if [ "$VERSION" = "" ]
   then version=$DEFVERS
   fi
   while true
   do
      if $QSPQUERY "The version is $version - OK?" "y" "n"
      then
	   break
      else
	   echo $NC "Please enter version: $BC"
	   read version
      fi
   done
   cd $QSROOT
   if [ "`echo $QSROOT | awk -F/ '{print$1}'`" != "" ]
   then QSROOT="`pwd`"
   fi
   cd $here
   echo "The root directory of this installation (QSROOT) is set to:"
   echo
   echo "        $QSROOT"
   echo
   if $QSPQUERY "Is this acceptable?" "y" "n"
   then
        qsroot=$QSROOT
   else
	while true
	do
	   echo 
	   echo "Please enter the root directory of Maitre D'" 
	   echo $NC "(or type RETURN to set to $QSROOT): $BC"
	   read qsroot
	   if [ "$qsroot" = "" ]
	   then 
	 	qsroot=$QSROOT
	   fi
	   if [ ! -d $qsroot ]
	   then 
		echo
		echo "The directory $qsroot does not exist."
		if $QSPQUERY "Do you wish to try a different directory?" "y" "n"
		then
		     continue
		else
		     echo "Leaving Maitre D' license configuration."
		     exit 1
		fi
	   else
		break
	   fi
	done
   fi

if [ -f $qsroot/maitred/md_mldb ]
then 	echo "A license database already exists:"
	echo "($qsroot/maitred/md_mldb)"
	if $QSPQUERY "OK to add $product $version to existing database?" "y" "n"
	then echo
	else exit 1
	fi
	MDSERVER_HOST=$host
	MDSERVER_MASTER=$host
	export MDSERVER_HOST MDSERVER_MASTER
	$qsroot/bin.$MACHINE/mdserver -r $qsroot -m 2>/dev/null
	stat=$?
	if [ $? != 7 ]
	then 
		echo "Starting server..."
		echo "Please wait 10 seconds..."
		sleep 10
	fi
	$qsroot/bin.$MACHINE/mdconfig -h $host -P $password -N $product -V $version
	stat=$?
else 
	$qsroot/bin.$MACHINE/mdinstall $qsroot $password $product $version
	stat=$?
fi

case $stat in
     0)
	echo "Maitre D' license installed."
	cd $qsroot
	$SH ./setup $MACHINE $host $host $autoflag $qsroot
	cd $here
	if [ $? = 0 ]
	then echo
	else echo "Maitre D' user configuration incomplete."
	fi
	break;;
     1) echo "Invalid number of arguments."
	echo "usage: bin.{MACHINE}/mdinstall qsroot password product version";;
     2) echo "Can't get internet address for $host"
	echo "Probably an incorrect /etc/hosts file...";;
     3) echo "Password not decodable.";;
     4) echo "Unable to open database in $qsroot/maitred.";;
     5) echo "Problem writing database...";;
    99) echo "Cannot load message file...";;
   233) echo "Mdconfig: secure password in place. Someone has already"
	echo "locked the license database with a password. You must exit "
	echo "and run mdconfig manually - please refer to Appendix D.";;
   234) echo "Invalid password...";;
   235) echo "Invalid password...?";;
   236) echo "Product name not found...";;
   237) echo "Cannot find master server...";;
    -1) echo "Serious problem!...";;
     *) echo "Unknown error...";;
esac
if $QSPQUERY "Would you like to re-enter the information?" "y" "n"
then
     echo
     continue
else
     echo "Leaving Maitre D' license configuration."
     echo "
    Note: You may configure a license later by running
    $SH $qsroot/maitred/$selfname $qsroot
"
     exit 1
fi
done

#echo "Maitre D' license configuration complete!"
