#!/bin/sh
# finish	Finish script for Installation Utilities.  Jerry Rains 

# Determine form of echo to use.
if [ `echo "x\c"` = "x" ]
then
    # Then the standard command will work just fine.
    ECHO="echo"
else
    # Otherwise we need to enable the 'backslash' processing.
    ECHO="echo -e"
fi

this_dir=`pwd`
# Make sure finish is being run from ./fp directory.
[ -x dclerk ] || {
  [ -x util/finish ] && {
    $ECHO "\nThe Installation Utilities MUST be installed into the ./fp directory!"
    $ECHO "Remove the $this_dir/util directory using rm -rf $this_dir/util"
    $ECHO "and reinstall in the ./fp directory.\n"
    exit 0
    }
  $ECHO "\nYou must run util/finish from the ./fp directory.\n"
  exit 0
  }
    
# Calc location of run directory from command $0.
nix=`uname -s`
dir=`pwd`
if [ "$dir" = "/" ]
then
  rundir=`dirname $0`
else
  adir=`dirname $0`
  bdir=/`basename $adir`
  until [ "$bldok" = "ok" ] 
  do
  [ $adir = / ] && bldok=ok
  [ $adir = . ] && {
      rundir=$dir$rundir
      bldok=ok
      }
  [ "$bldok" = "ok" ] || {
      bdir=/`basename $adir`
      [ $bdir = /.. ] && {
        dir=`dirname $dir`
        bdir=""
        }
      adir=`dirname $adir`
      rundir=$bdir$rundir
      }
  done
fi
instutil=`dirname $rundir | sed -e "s,//,/,"`
PFPROG=`dirname $instutil`
# Set ECHO so SCO and Linux will behave the same.
[ -f "$PFPROG/fp/util/bin/set_echo" ] && ECHO="`$PFPROG/fp/util/bin/set_echo`"
[ -z "$ECHO" ] && ECHO="echo"

# Check to make sure /bin/ksh exists
[ -f "/bin/ksh" -a -x "/bin/ksh" ] || {
    # If not is the Linux?
    [ "$nix" = "Linux" ] && {
        # If so use bash
        ln -s bash /bin/ksh
        } || {
        $ECHO
        $ECHO "Several of these utilities require /bin/ksh\n"
        $ECHO "You may get errors on selections 7, 8, 9, B, C, D & F.\n"
        $ECHO "Press Enter to continue. \c" 
        }
    }
yesno=""
[ "$1" = "-y" ] && yesno="y"

if [ -n "$PFMENU" -a "$yesno" != "y" ]
then
    $ECHO
    $ECHO "PFMENU is set to $PFMENU\n"
    $ECHO "Do you want to install into $PFMENU [Y] \c"
    read yesno
    [ $yesno ] || yesno="y"
    yesno=`$ECHO $yesno | sed -e "y/YN/yn/"`
    [ "$yesno" != "y" ] && PFMENU=""
fi
[ ! -d "$PFMENU" -a "$1" = "-y" ] && PFMENU=$PFPROG/fp/menus

until [ -d "$PFMENU" ]
do
    [ "$yesno" = "y" ] || {
        $ECHO 
        $ECHO "Enter full path to PFMENU [$PFPROG/fp/menus] \c"
        read PFMENU
        [ $PFMENU ] || PFMENU=$PFPROG/fp/menus
        }
    [ -d "$PFMENU" ] || {
        $ECHO 
        $ECHO "$PFMENU is not a valid directory."
        PFMENU=""
        }
done

# Save util.-8 if already there then install util.-8.
[ -f $PFMENU/util.-8 -a -f $dir/util/tmp/util.-8 ] && {
    file=$PFMENU/util.-8
    while [ -f $file ]
    do
        file=$file\#
    done
    mv $PFMENU/util.-8 $file
    $ECHO 
    $ECHO "$PFMENU/util.-8 saved in $file"
    }
# clear
# Save existing util menu.
[ -f $PFMENU/util -a -f $dir/util/tmp/util ] && {
    file=$PFMENU/util
    while [ -f $file ]
    do
        file=$file\#
    done
    mv $PFMENU/util $file
    [ "$1" = "-y" ] || {
        $ECHO "\nYour 'util' menu was replaced."
        $ECHO "The old menu was saved in $file"
        }
    }
# Install new util menu
tmpdir=$dir/util/tmp
mv $tmpdir/util* $PFMENU
[ "$1" = "-y" ] || {
    $ECHO 
    $ECHO "Installed new 'util' menu."
    $ECHO 
    }
chown filepro $PFMENU/util $PFMENU/util.-8
chmod 644 $PFMENU/util
chmod 755 $PFMENU/util.-8

# Check to see if util.hlp-8 has already been appended to util.hlp and remove.
sed "/^##(2,3,13,76)8/,/# End of .-8/d" $PFMENU/util.hlp > $tmpdir/utilhlp 
cat $tmpdir/utilhlp $PFMENU/util.hlp-8 > $tmpdir/util.hlp
cp -f $tmpdir/util.hlp $PFMENU/util.hlp
rm -f $PFMENU/util.hlp-8 $tmpdir/util.hlp $tmpdir/utilhlp

# Install new instutil menu
mv $dir/util/tmp/instutil* $PFMENU
[ "$1" = "-y" ] || {
    $ECHO "Installed new 'instutil' menu."
    $ECHO 
    }
chown filepro $PFMENU/instutil
chmod 644 $PFMENU/instutil
# Set all permissions for .-? to 755
echo $PFMENU/instutil.-* | xargs chown filepro
echo $PFMENU/instutil.-* | xargs chmod 755
chmod 644 $PFMENU/*.hlp $PFMENU/instutil
chown filepro $PFMENU/*.hlp $PFMENU/instutil
# Now allow anyone to use the following entries.
find ./util | xargs chown filepro
chmod 755 ./util
instdir=$dir/util/bin
# Install proper version of showlock.
[ -f $instdir/showlock.$nix ] && {
    cp $instdir/showlock.$nix $instdir/showlock
    chown filepro $instdir/showlock
    chmod +x $instdir/showlock
    } || {
    $ECHO "\nYou will need to obtain the proper version of showlock for "$nix
    $ECHO "before you can use functions G and H."
    $ECHO "\nPress Enter to continue. \c"
    read key
    }
[ "$fpinstall" != "y" ] && {
    [ "$1" = "-y" ] || {
        $ECHO
        $ECHO "This is the Installation Utilities Menu 'instutil'."
        $ECHO "It will run on Version 4.8 and 5.0."
        }
    }
$ECHO
$ECHO "The Installation Utilities run with a different PFDIR.  You must run "
$ECHO "the 'instutil' menu from 'util' selection -8."
$ECHO
$ECHO "There is extensive help for each entry in the menu.  Press F10 from "
$ECHO "each menu option to read about that option."
$ECHO
[ "$1" = "-y" ] || {
  hash -r
  cd /
  is_less=`type less 2>&1 | sed -e "s,less is ,,"`
  tis_less=`echo $is_less | grep '/'`
  if [ -n "$tis_less" ]
  then
      pager=`basename $is_less`
  elif [ "$PAGER" ]
  then
    pager=$PAGER
  else
    pager=more
  fi
  [ "$pager" = "less" ] && {
    LESS="-f -k$this_dir/util/bin/.less$ -P\    PRESS\:\ x to Exit.    PgDn,  PgUp,  Arrows to move    F9 or / to Search    "
    export LESS
    }
  $ECHO "	To access the Installation Utilities Menu:"
  $ECHO 
  p=`echo $startup | sed -e "s,$startdir/,,"`
  [ -z "$p" ] && p=p
  $ECHO "	p		(filePro Plus Main Menu)"
  $ECHO "	F		(filePro Utilities Menu)"
  $ECHO "	8		(Installation Utilities Menu)"
  $ECHO
  $ECHO -n "Would you like to view the Installation Utilities Menu README file? [Y] "
  read yesno
  [ $yesno ] || yesno=y
  yesno=`echo $yesno | sed -e "y/YN/yn/"`
  [ "$yesno" = "y" ] && $pager $this_dir/util/README
  }
