#!/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
  echo "install-octave: no version number!"
  exit 1
fi

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

if test -z "$target_host_type"
then
  echo "install-octave: host archetecture not found!"
  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

# ==================== 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=/usr/local

prefix="/usr/local"
if test $# -eq 1
then
  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"

# Where to install and expect libraries like libcruft.a, liboctave.a,
# and libreadline.a, executable files to be run by Octave rather than
# directly by users, and other architecture-dependent data.
# ${archlibdir} is a subdirectory of this. 
libdir="$exec_prefix/lib"

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

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

# 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/$target_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/$target_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/$target_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/$target_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/,,"`
        includedir: `echo $includedir | 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 $libexecdir $includedir $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@|$libdir|" 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

echo "installing info as $archlibdir/info"
cp info/info $archlibdir/info
chmod 755 $archlibdir/info

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

oct_files=`find . -name '*.oct' -print`
if test -n "$oct_files"; then
  echo "installing .oct files in $octfiledir"
  cd src
  for f in $oct_files
  do
    file=`basename $f`
    cp $f $octfiledir/$file
    chmod 755 $octfiledir/$file
  done
  if test -f links-to-make; then
    cat 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
for f in `find . -name '*.img' -print`
do
  file=`basename $f`
  cp $f $imagedir/$file
  chmod 644 $imagedir/$file
done
cd $distdir

echo "creating ls-R file in $datadir/octave"
ls -LR $datadir/octave $libexecdir/octave > $datadir/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

exit 0
