#!/bin/sh
package=dblatex
launch=/tmp/${package}_launcher
install_script=./install.sh
scriptname=`basename $0`
shel="#!/bin/sh"

usage ()
{
  echo "`basename $0` [options]"
  echo ""
  echo "Install options:"
  echo "--prefix=PREFIX        where to install the package"
  echo "--catalog=CATALOG      catalogs location"
  echo "--target=TARGET        default latex style"
  exit 1
}

# Default configuration
prefix=/usr/local
catalog=
target=
ENVCATS=
EXPCATS=
ENVMAKE=
EXPMAKE=

args="$*"

while true
do
  optarg=`expr "x$1" : 'x[^=]*=\(.*\)'`
  case "$1" in
  --prefix=*)      prefix=$optarg;;
  --exec-prefix=*) ;; # Nothing to do, provided only for compatibility
  --catalog=*)     catalog=$optarg;;
  --target=*)      target=$optarg;;
  -*)              echo "$1: unknown option"; usage; break;;
  *) break;;
  esac
  shift
done

share=$prefix/share
bin=$prefix/bin

if [ "$catalog" != "" ]; then
  if [ ! -f $catalog ]; then
    echo "Error: $catalog is not a file"
    exit 1
  else
    ENVCATS="SGML_CATALOG_FILES=\\\$SGML_CATALOG_FILES:$catalog"
    EXPCATS="export SGML_CATALOG_FILES"
  fi
fi

if [ "$target" != "" ]; then
  default_opt="-T $target"
fi

notfound=""
for util in perl xsltproc latex pdflatex kpsewhich file; do
  path=`which $util 2>/dev/null|sed 's/^no //'`
  if [ "$path" = "" ]; then
    status="no"
    notfound="$util $notfound"
  else
    status="yes"
  fi
  echo "+checking $util... $status"
done

# Special check procedure for GNU make (can be gmake or make)
status="no"
path=`which gmake 2>/dev/null|sed 's/^no //'`
if [ "$path" = "" ]; then
  echo "+checking gmake... $status"
  path=`which make 2>/dev/null|sed 's/^no //'`
  if [ "$path" != "" ]; then
    gnu=`make -v 2>/dev/null|grep -c GNU`
    if [ $gnu -ne 0 ]; then
      ENVMAKE="GMAKE=make"
      EXPMAKE="export GMAKE"
      status="yes"
    fi
  fi
else
  status="yes"
fi
      
echo "+checking [g]make... $status"
[ "$status" = "no" ] && notfound="[g]make $notfound"

# Check the Latex dependencies
deps_sty=`perl -e 'while(<>) {
                     if (/^ *%.*/) {
                       # skip a comment line
                     } elsif (/\\\usepackage\\[*.*\\]*{(\\w+)}/) {
                       print "$1\n";
                     }
                   }' latex/style/*.sty`
for sty_name in $deps_sty ;do
  sty_file=$sty_name.sty
  if [ -f latex/style/$sty_file ] || [ -f latex/misc/$sty_file ]; then
    # This file is provided by this package...
    echo "+checking $sty_file... found in package"
  else
    path=`kpsewhich $sty_file 2>/dev/null`
    if [ "$path" = "" ]; then
      status="no"
      notfound="$sty_file $notfound"
    else
      status="yes"
    fi
    echo "+checking $sty_file... $status"
  fi
done


if [ "$notfound" != "" ]; then
  echo "Error: $notfound are needed"
  exit 1
fi

# Don't loose the configure options
CONFCMD="$0 $args"

echo "building $install_script"
cat <<foe > $install_script
$shel
# This script was produced by the command:
# $CONFCMD
#
#
# Let's install the package
#
echo "install the package in $share..."
rm -rf $share/$package/scripts
rm -rf $share/$package/xsl
rm -rf $share/$package/docs
# Maintain the latex/contrib directory
rm -rf $share/$package/latex/example
rm -rf $share/$package/latex/style
mkdir -p $share
mkdir -p $bin
cp -r . $share/$package
if [ \$? -ne 0 ]; then
  exit 1;
fi
rm $share/$package/$install_script
rm $share/$package/Makefile
rm $share/$package/$scriptname

#
# Now make the script to launch
#
cat <<eof > $launch
$shel
#
# This script is automatically created by
# $CONFCMD
#
$ENVCATS
$EXPCATS
$ENVMAKE
$EXPMAKE
TEXINPUTS=:$share/$package/latex//:\\\$TEXINPUTS
export TEXINPUTS

$share/$package/scripts/dblatex $default_opt \\\$*
eof

#
# Copy the script to the right place
#
echo "install dblatex in $bin"
chmod +x $launch
cp $launch $bin/dblatex
rm $launch
foe
chmod +x $install_script
