#!/bin/sh
# Copyright (C) 2017-2018 Free Software Foundation, Inc.
#
# This file is part of GNU Texinfo.
#
# GNU Texinfo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNU Texinfo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Texinfo.  If not, see <http://www.gnu.org/licenses/>.

# option parsing

version () {
  cat <<EOF
texinfo-js 0.0.90

Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
    exit 0
}

help () {
  cat <<EOF
Usage: $progname MANUAL [texi2any options]"

Run texi2any on MANUAL, adding a JavaScript browsing interface.  Any
extra options are passed through to texi2any.

Report bugs to bug-texinfo@gnu.org,
GNU Texinfo home page: <http://www.gnu.org/software/texinfo/>
General help using GNU software: <http://www.gnu.org/gethelp/>
EOF

}

progname=$0
progname=`echo $progname | sed -e s,.*/,,`

manual=

while test x"$1" != x ; do
  case $1 in
    --version)
      version
      exit 0
      ;;
    --help)
      help
      exit 0
      ;;
    -*)
      echo "$progname: unknown option $1"
      exit 1 ;;
    *)
      manual=$1
      shift
      break ;;
  esac
  shift
done

if test x$manual = x; then
  help
  exit 1
fi

texi2any_flags=$@

TEXI2ANY=${TEXI2ANY:-texi2any}

# substituted by Makefile
pkgdatadir=@pkgdatadir@
if test $pkgdatadir = '@'pkgdatadir'@' ; then
  pkgdatadir=.
fi

extra_head='
<link rel="stylesheet" type="text/css" href="info.css"/>
<script src="modernizr.js" type="text/javascript"></script>
<script src="info.js" type="text/javascript"></script>
'

js_scripts="$pkgdatadir/modernizr.js $pkgdatadir/info.js"
css_files="$pkgdatadir/info.css"
for f in $js_scripts $css_files ; do
  if ! test -f $f ; then
    echo "$progname: $f not found"
    exit 1
  fi
done

if test x$OUTDIR = x ; then
  OUTDIR=`basename $manual`
  OUTDIR=${OUTDIR%.texi}-html
fi

echo $TEXI2ANY --html -c EXTRA_HEAD="$extra_head" $manual -o $OUTDIR $texi2any_flags
$TEXI2ANY --html -c EXTRA_HEAD="$extra_head" $manual -o $OUTDIR $texi2any_flags

if test $? -ne 0 ; then
  echo "$progname: texi2any failed"
  exit 1
else
  cp $js_scripts $css_files $OUTDIR
fi
