#!/bin/ksh
#
# This script creates a makefile for building and maintaining the DOC
# package.  It also runs the make file to build the DOC.
#
# It must reside in the DOC's top directory.
#
# Usage:
#
# install <doc-bin-directory> [<java interpreter>] [no_build]
#
# <doc-bin-directory> is the path to the directory that holds the
# binaries and scripts for the services which the DOC servers out.
# 
# This install script will automatically build the DOC package.  If
# "no_build" is given, then the build will be suppressed.
#

DOC_path=`whence $0` >/dev/null 2>&1
DOC_home=`dirname $DOC_path`

link=$DOC_home/bin/make_dotdir.sh
file=$DOC_home/bin/rps2/make_dotdir.sh
if [ -L $link ]; then rm $link ; fi
if [ ! -a $link ]; then
   ln -s $file $link
else 
   echo "$link exists as a non-link file; link not created."
fi

doc_bin_directory=$1
if [ -z "$doc_bin_directory" ]; then
   echo Warning: bogus services bin directory used!
   doc_bin_directory=bogus_bin_directory
fi

if [ -n "$2" ]; then
   if [ "$2" = "no_build" ]; then
      no_build=$2
      java_interpreter=""
   else
      java_interpreter=$2
      if [ -n "$3" ]; then
         no_build=$3
      fi
   fi
else
   no_build=""
   java_interpreter=""
fi

echo "# This makefile was automatically generated by the DOC install script on" > $DOC_home/Makefile
echo "# "`date` >> $DOC_home/Makefile
echo " " >> $DOC_home/Makefile
cat $DOC_home/makefile.base >> $DOC_home/Makefile
echo "configure :" >> $DOC_home/Makefile
echo "		configuration/set_config $doc_bin_directory $java_interpreter" >> $DOC_home/Makefile

if [ -z "$no_build" ]; then
    here=`pwd` ; cd $DOC_home ; gmake ; cd $here
fi

script=DOCTEST
$DOC_home/bin/insert_env_var.tcl $DOC_home/script_bases/$script \
   DOC_DIR $DOC_home
if [ "$?" != "0" ]; then exit 1 ; fi
mv $DOC_home/script_bases/$script $DOC_home/$script
