#!/bin/sh
#
# install-ocst   --   install/update Octave Controls Toolbox (OCST)
#                    of A. Scottedward Hodel <A.S.Hodel@Eng.Auburn.EDU>
#
# This script uses chunks of "install-octave"
# from John W. Eaton <jwe@bevo.che.wisc.edu>
#
# Kai P Mueller <mueller@ifr.ing.tu-bs.-de>, TU Braunschweig
# 11/22/97 initial version
#

if [ $# -ne 1 ]
then
  echo
  echo "    usage: $0 (help | all | select)"
  echo
  exit 1
fi

case $1 in

  help)
    cat << EOF
==========================================================================
  install-ocst attempts to install (or update) the OCST (Octave controls
  toolbox) of your version of Octave.

   The files in the subdirectories "general", "linear-algebra",
   "strings", "plot", and "control" will be copied to the Octave data
   directories with proper permissions.

   If the file "obsolete.files" exists this script file fill
   ask if the files listed in "obsolete.files" should be removed.

   The install and update procedures differ in the way they perform the
   copy of the  files in this OCST version.

   o all     Copy all files to the Octave data directories assuming that
             this version is newer. Install does not ask any questions.
	     This modfe is best if you are the system administrator you
	     just want to install the control stuff.

   o select  Copy all files but ask before replacing an existing file
             which differs from the one in this distribution. The update
	     mode is appropriate for developers. New files will be
	     copied without confirmation.
	     Note: it can be very annoying to confirm the replacements.
==========================================================================
EOF
    exit 0
    ;;

  all)
    ask=no
    ;;

  select)
    ask=yes
    ;;

  *)
    echo
    echo "    usage: $0 (help | all | select)"
    echo
    exit 1
    ;;
esac

if [ `whoami` != root ]
then
  echo -n " * You should execute install-ocst as root, continue anyway? [n]: "
  read ans
  case "$ans" in
    y | Y | yes | YES)
    ;;
    *)
      exit 1
    ;;
  esac
fi

# change location of the distribution file here:
ocst_version=1.2
datadir=/usr/local/share
oct_version=2.0.12

echo
echo " - This is the OCST V$ocst_version update procedure -"

# change everything below at your own risk.
# check data directory
echo
echo -n " * Octave data dir is $datadir/octave; is that correct? [y]: "
read ans
case "$ans" in
  n | N | no | NO)
    echo -n " * Enter Octave data dir (without /octave/...): "
    read datadir
  ;;
esac
if [ ! -d "$datadir/octave" ]
then
  echo "install-ocst: Octave dir ($datadir/octave) does not exist - Goodbye."
  exit 1
fi

# check Octave version
echo
echo -n " * Octave version is $oct_version; is that correct? [y]: "
read ans
case "$ans" in
  n | N | no | NO)
    echo -n " * Enter Octave version: "
    read oct_version
  ;;
esac
toplevel=$datadir/octave/$oct_version/m
if [ ! -d $toplevel ]
then
echo "install-ocst: toplevel dir ($toplevel) does not exist - Goodbye."
  exit 1
fi

# perform installation
echo
echo -n " * This is your last chance to quit, continue installation? [y]: "
read ans
case "$ans" in
  n | N | no | NO)
    echo "install-ocst aborted."
    exit 0
  ;;
esac

echo
distdir=`pwd`
for dd in general linear-algebra strings plot control
do
  if [ -d "$dd" ]
  then
    echo " o installing files in $dd..."
    cd $dd
    for ff in `ls`
    do
      if [ $ask = yes ]
      then
        if [ -e "$toplevel/$dd/$ff" ]
	then
	  diff $ff "$toplevel/$dd/$ff" > /dev/null
	  if [ "$?" = 0 ]
	  then
	    # files are identical; do not copy
	    echo "---> $ff  (same version does already exist, no replacement)"
	  else
	    # file already exit, our version is (possible) newer
            echo -n " * Replace existing $ff? [y]: "
            read ans
            case "$ans" in
              n | N | no | NO)
	        echo "---> $ff skipped."
              ;;
              *)
                cp $ff $toplevel/$dd
                chmod 644 $toplevel/$dd/$ff
              ;;
            esac
	  fi
	else
	  echo "---> $ff  (new file)"
          cp $ff $toplevel/$dd
          chmod 644 $toplevel/$dd/$ff
	fi
      else
        # copy everything without asking
        cp $ff $toplevel/$dd
        chmod 644 $toplevel/$dd/$ff
      fi
    done
    echo "   ...files in $dd installed."
  fi
  cd $distdir
done

# check for "obsolete.files"
if [ ! -r obsolete.files ]
then
  echo " o No obsolete files."
else
  echo " o removing obsolete files..."
  flist=`cat obsolete.files`
  for ff in $flist
  do
    dfile=$toplevel/$ff
    if [ ! -e "$dfile" ]
    then
      echo " * no obsolete file: $ff"
    else
      echo -n " * remove $ff? [n]: "
      read ans
      case "$ans" in
        y | Y | yes | YES)
          rm $dfile
        ;;
      esac
    fi
  done
fi

echo
echo "   All files have been installed. Now to the final question:"
echo

echo -n " * Replace/create ls-R file? [y]: "
read ans
case "$ans" in
  n | N | no | NO)
    echo "--> no new ls-R file."
  ;;
  *)
    if [ -d "$datadir/libexec" ]
    then
      echo "install-ocst: libexec directory found."
      ls -LR $datadir/octave $libexecdir/octave > $datadir/octave/ls-R
    else
      echo "install-ocst: libexec directory not found."
      echo "              (you can safely ingnore this message.)"
      ls -LR $datadir/octave > $datadir/octave/ls-R
    fi
  ;;
esac

echo "Thank you for using install-ocst."
exit 0
