#!/bin/sh
#
# install-octave -- install script for binary distributions.
#
# John W. Eaton
# jwe@bevo.che.wisc.edu
# University of Wisconsin-Madison
# Department of Chemical Engineering

# get version
version=`cat VERSION 2> /dev/null`

if test -z "$version"
then
  cat <<EOF 
install-octave: no version number!

Are you sure you have a binary distribution of Octave?  If you are
trying to install Octave from its sources, please read and follow
the directions given in the file INSTALL.OCTAVE.
EOF
  exit 1
fi

# get host type
canonical_host_type=`cat ARCH 2> /dev/null`

if test -z "$canonical_host_type"
then
  cat <<EOF 
install-octave: host archetecture not found!

Are you sure you have a binary distribution of Octave?  If you are
trying to install Octave from its sources, please read and follow
the directions given in the file INSTALL.OCTAVE.
EOF
  exit 1
fi

distdir=`pwd`

# Check whether to use -n or \c to keep echo from printing a newline
# character.  Stolen from autoconf, which borrowed the idea from dist 3.0.

if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  # Stardent Vistra SVR4 grep lacks -e, says ghazi@caip.rutgers.edu.
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    echo_n=
    echo_c='
'
  else
    echo_n=-n
    echo_c=
  fi
else
  echo_n=
  echo_c='\c'
fi

EXE=@EXE@

# ==================== Where To Install Things ====================

# The default location for installation.  Everything is placed in
# subdirectories of this directory.  The default values for many of
# the variables below are expressed in terms of this one, so you may
# not need to change them.  This defaults to /usr/local.
prefix=@prefix@

alt_dir=false
if test $# -eq 1
then
  alt_dir=true
  prefix=$1
else
  if test $# -gt 1
  then
    echo "usage: install-octave [prefix-directory]"
    exit 1
  fi
fi

# Like `prefix', but used for architecture-specific files.
exec_prefix="$prefix"

# Where to install Octave and other binaries that people will want to
# run directly.
bindir="$exec_prefix/bin"

# Where to install architecture-independent data files.  ${fcnfiledir}
# and ${localfcnfiledir} are subdirectories of this.
datadir="$prefix/share"

libdir="$exec_prefix/lib"

# Where to install and expect libraries like libcruft.a and liboctave.a.
octlibdir="$libdir/octave-$version"

# Where to install and expect executable programs to be run by Octave
# rather than directly by users.
libexecdir="$exec_prefix/libexec"

includedir="$prefix/include"

# Where to install Octave's include files.  The default is
# ${prefix}/include/octave-$version
octincludedir=$includedir/octave-$version

# Where to install Octave's man pages, and what extension they should
# have.  The default is ${prefix}/man/man1
mandir="$prefix/man/man1"
manext="1"

# Where to install and expect the info files describing Octave..
infodir="$prefix/info"

# The fill path to the default info file.
infofile="$infodir/octave.info"

# ==================== Octave-specific directories ====================

# These variables hold the values Octave will actually use.  They are
# based on the values of the standard Make variables above.

# Where to install the function file distributed with
# Octave.  This includes the Octave version, so that the
# function files for different versions of Octave will install
# themselves in separate directories.
fcnfiledir="$datadir/octave/$version/m"

# Directories Octave should search for function files specific
# to this site (i.e. customizations), before consulting
# ${fcnfiledir}.  This should be a colon-separated list of
# directories.
localfcnfiledir="$datadir/octave/site/m"
localfcnfilepath="$localfcnfiledir//"

# Where to put executables to be run by Octave rather than
# the user.  This path usually includes the Octave version
# and configuration name, so that multiple configurations
# for multiple versions of Octave may be installed at once.
archlibdir="$libexecdir/octave/$version/exec/$canonical_host_type"

# Where to put executables to be run by Octave rather than by the
# user that are specific to this site.
localarchlibdir="$libexecdir/octave/site/exec/$canonical_host_type"

# Where to put object files that will by dynamically loaded.
# This path usually includes the Octave version and configuration
# name, so that multiple configurations for multiple versions of
# Octave may be installed at once. 
octfiledir="$libexecdir/octave/$version/oct/$canonical_host_type"

# Directories Octave should search for object files that will be
# dynamically loaded and that are specific to this site
# (i.e. customizations), before consulting ${octfiledir}.  This should
# be a colon-separated list of directories.
localoctfiledir="$libexecdir/octave/site/oct/$canonical_host_type"
localoctfilepath="$localoctfiledir//"

# Where Octave will search to find its function files.  Before
# changing this, check to see if your purpose wouldn't
# better be served by changing localfcnfilepath.  This
# should be a colon-separated list of directories.
fcnfilepath=".:$localoctfilepath:$localfcnfilepath:$octfiledir//:$fcnfiledir//"

# Where Octave will search to find image files.es.
imagedir="$datadir/octave/$version/imagelib"
imagepath=".:$imagedir//"

cat << EOF
Installing octave in the following subdirectories of
$prefix:

            bindir: `echo $bindir | sed "s,^$prefix/,,"`
           datadir: `echo $datadir | sed "s,^$prefix/,,"`
            libdir: `echo $libdir | sed "s,^$prefix/,,"`
         octlibdir: `echo $octlibdir | sed "s,^$prefix/,,"`
        includedir: `echo $includedir | sed "s,^$prefix/,,"`
     octincludedir: `echo $octincludedir | sed "s,^$prefix/,,"`
            mandir: `echo $mandir | sed "s,^$prefix/,,"`
           infodir: `echo $infodir | sed "s,^$prefix/,,"`
        fcnfiledir: `echo $fcnfiledir | sed "s,^$prefix/,,"`
   localfcnfiledir: `echo $localfcnfiledir | sed "s,^$prefix/,,"`
        archlibdir: `echo $archlibdir | sed "s,^$prefix/,,"`
   localarchlibdir: `echo $localarchlibdir | sed "s,^$prefix/,,"`
        octfiledir: `echo $octfiledir | sed "s,^$prefix/,,"`
   localoctfiledir: `echo $localoctfiledir | sed "s,^$prefix/,,"`
          imagedir: `echo $imagedir | sed "s,^$prefix/,,"`

EOF

echo $echo_n "Is this correct [y/N]? " $echo_c

read ans

case "$ans" in
  y | Y | yes | YES)
  ;;
  *)
    exit 1
  ;;
esac

DIRS_TO_MAKE="$bindir $datadir $libdir $octlibdir $libexecdir \
  $includedir $octincludedir $mandir $infodir $fcnfiledir \
  $localfcnfiledir $archlibdir $localarchlibdir \
  $octfiledir $localoctfiledir $imagedir"

./mkinstalldirs $DIRS_TO_MAKE

if test "$prefix" = /usr/local
then
  echo "installing src/octave as $bindir/octave"
  cp src/octave $bindir/octave
  chmod 755 $bindir/octave
else
  echo "installing octave-sh as $bindir/octave"
  sed "s|@OCTAVE_HOME@|$prefix|; s|@LD_LIBRARY_PATH@|$octlibdir|" octave-sh \
    > $bindir/octave
  chmod 755 $bindir/octave

  echo "installing src/octave as $bindir/octave.bin"
  cp src/octave $bindir/octave.bin
  chmod 755 $bindir/octave.bin
fi

echo "installing octave-bug as $bindir/octave-bug"
cp octave-bug $bindir/octave-bug
chmod 755 $bindir/octave-bug

if test -f LIBRARIES; then
  echo "installing shared libraries in $octlibdir"
  for f in `cat LIBRARIES`
  do
    file=`basename $f`
    cp $f $octlibdir/$file
    chmod 644 $octlibdir/$file
  done
fi

oct_files=`find . -name '*.oct' -print`
if test -n "$oct_files"; then
  echo "installing .oct files in $octfiledir"
  for f in $oct_files
  do
    file=`basename $f`
    cp $f $octfiledir/$file
    chmod 755 $octfiledir/$file
  done
  if test -f src/links-to-make; then
    cat src/links-to-make | while read src dest
    do
      if test -n "$src" && test -n "$dest"; then
	cd $octfiledir
	ln $src $dest
      fi
    done
  fi
  cd $distdir
fi

echo "installing .m files in $fcnfiledir"
cd scripts
tar cf - . | ( cd $fcnfiledir ; tar xf - )
find $fcnfiledir -type f -print | xargs chmod 0644
find $fcnfiledir -name '*.img' -print | xargs rm -f
cd $distdir

echo "installing image files in $imagedir"
cd scripts/image
cp *.img $imagedir
chmod 644 $imagedir/*.img
cd $distdir

echo "creating ls-R file in $datadir/octave"
ls -LR $datadir/octave > $datadir/octave/ls-R
chmod 644 $datadir/octave/ls-R

echo "creating ls-R file in $libexecdir/octave"
ls -LR $libexecdir/octave > $libexecdir/octave/ls-R
chmod 644 $libexecdir/octave/ls-R

echo "installing info files in $infodir"
for f in doc/interpreter/octave.info*
do
  file=`basename $f`
  cp $f $infodir/$file
  chmod 644 $infodir/$file
done

echo "installing man page in $mandir"
cp doc/interpreter/octave.1 $mandir/octave.$manext
chmod 644 $mandir/octave.$manext

case "$canonical_host_type" in
  *-*-cygwin32)
    if $alt_dir; then
      echo "*** You have specified an installation directory different"
      echo "*** from the default.  For Octave to run properly, you must"
      echo "*** set the environment variable OCTAVE_HOME to"
      echo "***"
      echo "***   $prefix"
      echo "***"
      echo "*** before starting Octave."
    fi
  ;;
esac

exit 0
