#!/bin/sh
#
# Boot-strap values
#
NEWFBVERS=4.2.9
NEWFBDIR=/usr/local/FrontBase
NEWETCDIR=/usr/local/etc/rc.d
#NEWCGIDIR=
#NEWDOCDIR=
NEWEOFDIR=
NEWEOFVER=
#
# Functions
#
ECHO="/bin/echo"
ECHONL=""
ECHOPARM="-n"

# Ask for a path and create it if nessecary
#
askpath () {
  path=""
  while [ -z "$path" ]
  do
    $ECHO $ECHOPARM "$1 (no=skip, default=$2) ? $ECHONL"
    read path
    if [ -z "$path" ]; then path="$2"; fi
    if [ "$path" != "no" -a ! -d $path ]; then mkDir $path; fi
    if [ "$path" != "no" -a ! -d $path ]; then path=""; fi
  done
}

# Ask for confirmation
#
yesno () {
  ask=$2
  while [ -z "$ask" ]
  do
    $ECHO $ECHOPARM "$1 [yes|no] ? $ECHONL"
    read ask
    ask=`echo $ask | tr "[A-Z]" "[a-z]"`
    if [ "$ask" != "yes" -a "$ask" != "no" ]; then ask=""; fi
  done
}

# Create a directory recursively
#
mkDir() {
  if test ! -d $1
  then
     mkDir `dirname $1`
     mkdir $1
  fi
}

# Copy from one directory to another
#
copy() {
  (cd $1; tar cf - *) | (cd $2; tar x${3}f -)
}

# Link files in /etc
#
etclink() {
  up="`echo $1 | sed 's~/[^/]*$~~'`"
  rm -f $1/FrontBase.sh
  ln -s $1/FrontBase $1/FrontBase.sh
}
user=`whoami`
if test "$user" != "root"
then
   echo "You must be root to install FrontBase package"
   $ECHO $ECHOPARM "Please enter root $ECHONL" 
   su root -c "$0" && exit $?
   exit 1
fi
cd=`echo $0 | sed "s~/[^/]*$~~"`
test "$cd" != "install.sh" && cd $cd

test -f /etc/FrontBase.conf && . /etc/FrontBase.conf
FBVERS=$NEWFBVERS
test -z "$FBDIR" && FBDIR=$NEWFBDIR
test -z "$ETCDIR" && ETCDIR=$NEWETCDIR
#test -z "$CGIDIR" && CGIDIR=$NEWCGIDIR
#test -z "$DOCDIR" && DOCDIR=$NEWDOCDIR
test -z "$EOFDIR" && EOFDIR=$NEWEOFDIR
test -z "$EOFVER" && EOFVER=$NEWEOFVER

procs=`ps axw | grep 'FrontBase$' | cut -c 1-5`
if [ -z "$procs" ]; then ask="no"; else ask=""; fi
yesno "Terminate running FrontBase servers " $ask
if [ -z "$procs" ]; then fbkill="yes"; else fbkill="$ask"; fi
if [ "$ask" = "no" ]
then
	if [ -z "$procs" ]; then ask="yes"; else ask=""; fi
	yesno "\
        Continuing with the installation of FrontBase without stopping\n\
        already running databases may fail or produce unexpected results.\n\n\
	     Continue anyway ? " $ask
   if [ "$ask" = "no" ]; then exit 1; fi
fi

echo "Unpacking FrontBase-$FBVERS"
gzip -d ./Package.tar.*
tar -xf ./Package.tar
chmod 777 FrontBase/Databases
#chmod 755  FrontBase/etc/FBWeb 
chmod 755  FrontBase/etc/FrontBase 
cp deinstall.sh FrontBase

askpath "FrontBase installation in" $FBDIR 
fbpath=$path; test "$fbpath" != "no" && FBDIR=$fbpath
if [ -n "$EOFDIR" ]
then
   askpath "Framework 4.5 installation in" $EOFDIR 
   eofpath=$path; test "$eofpath" != "no" && EOFDIR=$eofpath
   if [ "$eofpath" != "no" ]
   then
      EOFVER=45
   fi
fi
askpath "Startup files are placed in" $ETCDIR 
etcpath=$path; test "$etcpath" != "no" && ETCDIR=$etcpath
#askpath "Web server cgi-bin directory" $CGIDIR 
#cgipath=$path; test "$cgipath" != "no" && CGIDIR=$cgipath
#askpath "Web server document directory" $DOCDIR 
#docpath=$path; test "$docpath" != "no" && DOCDIR=$docpath

if [ "$fbkill" = "yes" ]
then
   test -x $ETCDIR/FBWeb && $ETCDIR/FBWeb stop
   echo "Stopping FrontBase servers"
   if [ -n "$procs" ]; then kill -9 $procs; fi
   test -x $ETCDIR/FrontBase && $ETCDIR/FrontBase stop
fi

echo "Copying to $FBDIR"
test "$fbpath" != "no" && copy FrontBase $FBDIR
if [ -n "$EOFDIR" ]
then
   echo "Copying to $EOFDIR"
   test "$eofpath" != "no" && copy FrontBase/Frameworks/WO$EOFVER $EOFDIR
fi
echo "Copying to $ETCDIR"
test "$etcpath" != "no" && copy FrontBase/etc $ETCDIR
#echo "Copying to $CGIDIR"
#test "$cgipath" != "no" && copy FrontBase/httpd/cgi-bin $CGIDIR
#echo "Copying to $DOCDIR"
#test "$docpath" != "no" && copy FrontBase/httpd/htdocs $DOCDIR

echo FBVERS=$FBVERS >  /etc/FrontBase.conf
echo FBDIR=$FBDIR   >> /etc/FrontBase.conf
echo ETCDIR=$ETCDIR >> /etc/FrontBase.conf
#echo CGIDIR=$CGIDIR >> /etc/FrontBase.conf
#echo DOCDIR=$DOCDIR >> /etc/FrontBase.conf
echo EOFDIR=$EOFDIR >> /etc/FrontBase.conf
echo EOFVER=$EOFVER >> /etc/FrontBase.conf

(
cd FrontBase
echo $FBDIR/filelist
find *             -print | sed "/Databases/d;s~^~$FBDIR/~"
#find httpd/cgi-bin -print | sed "/^httpd\/cgi-bin$/d;s~^httpd/cgi-bin~$CGIDIR~"
#find httpd/htdocs  -print | sed "/^httpd\/htdocs$/d;s~^httpd/htdocs~$DOCDIR~"
find etc           -print | sed "/^etc$/d;s~^etc~$ETCDIR~"
if [ -n "$EOFDIR" ]; then
find Frameworks    -print | sed "/^Frameworks$/d;s~^Frameworks/WO$EOFVER~$EOFDIR~;/^Frameworks.*$/d"
fi
echo /etc/FrontBase.conf
) | sort -r > $FBDIR/filelist

echo "Setting up startup files"
etclink $ETCDIR

if [ "$fbkill" = "yes" ]
then
   echo "Starting FrontBase"
   $ETCDIR/FrontBase start
#   $ETCDIR/FBWeb start
fi

echo
echo "Be sure to include $FBDIR/bin in your PATH environment variable"
echo "Documentation can be found in $FBDIR/Documentation/Readme$OS.html"
echo

# Cleanup
#
gzip ./Package.tar &
rm -rf FrontBase &
wait

exit 0
