#!/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

# Exit on any error.

set -e

SED=@SED@

# 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

# 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

EXEEXT=@EXEEXT@
SHLEXT=@SHLEXT@
SHLEXT_VER=$SHLEXT.$version

# ==================== 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

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

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

if test -f LIBRARIES; then
  echo "installing shared libraries in $octlibdir"
  for f in `cat LIBRARIES`
  do
    file=`basename $f`
    cp $f.$SHLEXT_VER $octlibdir/$file.$SHLEXT_VER
    chmod 644 $octlibdir/$file.$SHLEXT_VER
    ( cd $octlibdir
      ln -s $file.$SHLEXT_VER $file.$SHLEXT )
  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
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 )

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

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

# Do this last, so that it won't be available if some other error
# happens earlier.  Otherwise, people might try to run Octave with
# an incomplete installation.

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

case "$canonical_host_type" in
  *-*-cygwin* | *-*-mingw*)
    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
