#!/bin/bash
#
# Installs the DocBook XSL Stylesheets on openSUSE systems
#
# Author: Thomas Schraitle
# Copyright (C) 2013 toms@opensuse.org
#

# set -x

ME=${0##*/}
PACKAGE=docbook-xsl-stylesheets
PACKAGE_DIR=docbook-xsl
PACKAGE_VERSION=@VERSION@

#----------------------------------------------------------------------
# System variables
BUILDROOT=
PREFIX=/usr
BINDIR=bin
DATAROOTDIR=share
DATADIR=$DATAROOTDIR
DOCDIR=$DATAROOTDIR/doc/packages
SYSCONFDIR=/etc
XMLCONFDIR=$SYSCONFDIR/xml
XMLDIR=$DATAROOTDIR/xml
DBSTYLE_CATALOG=$PACKAGE.xml
DBSTYLE_DIR=$XMLDIR/docbook/stylesheet/nwalsh
ORIG_DBSTYLE_DIR=$PREFIX/$DBSTYLE_DIR

function exit_on_error() {
   local result
   result=${2:-1}
   echo "ERROR: ${1}" >&2
   exit $result;
}


function my_debug() {
# Syntax: my_debug "MESSAGE" [MORE ARGUMENTS...]
# Prints debug messages, if DEBUG_SCRIPT is non-empty
    if [[ "$DEBUG_SCRIPT" ]]; then
        echo -e "[debug] ${0}: $@\n" >&2;
    fi
}

function usage() {
   cat << EOF
Installs the DocBook XSL Stylesheets

Usage:
$ME [OPTIONS]

Fine tuning of the installation directories:
  --buildroot=DIR         build root directory []
  --prefix=DIR              [/usr]
  --bindir=DIR            user executables [PREFIX/bin]
  --sysconfdir=DIR        read-only single-machine data [/etc]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/packages]

Package options:
  --package-name=NAME         name of the package [$PACKAGE]
  --package-dir=NAME          name of the package directory [$PACKAGE_DIR]
  --package-version=VERSION   version of the package [$PACKAGE_VERSION]

EOF
}

function printvariables() {
cat << EOF
package=$PACKAGE
package-version=$PACKAGE_VERSION
buildroot=$BUILDROOT
prefix=$PREFIX
bindir=$BINDIR
datarootdir=$DATAROOTDIR
datadir=$DATADIR
sysconfdir=$SYSCONFDIR
xmlconfdir=$XMLCONFDIR
docdir=$DOCDIR
xmldir=$XMLDIR
xsltrootdir=$XSLTROOTDIR
dbstyle_catalog=$DBSTYLE_CATALOG
dbstyle_catalog_entry=${DBSTYLE_CATALOG_ENTRY}
dbstyle_dir=$DBSTYLE_DIR
EOF
}

#----------------------------------------------------------------------
# 
DEBUG_SCRIPT=
DB4_XSLT_URL="http://docbook.sourceforge.net/release/xsl/"
DB5_XSLT_URL="http://docbook.sourceforge.net/release/xsl-ns/"
# Installation directories
DBXSLT_DIRS="assembly common eclipse epub epub3 extensions fo images \
highlighting html htmlhelp lib javahelp manpages profiling roundtrip slides \
template webhelp website xhtml xhtml-1_1 xhtml5"
DBXSLT_FILES="catalog.xml VERSION VERSION.xsl REVISION"
# Without INSTALL
DBXSLT_DOCFILES="params AUTHORS BUGS COPYING NEWS.html NEWS.xml README REVISION TODO \
RELEASE-NOTES.html RELEASE-NOTES.pdf RELEASE-NOTES.txt RELEASE-NOTES.xml"


#----------------------------------------------------------------------
# printing help / catching errors
#

if [[ -z "$1" ]]; then
  usage
  printvariables
  exit
fi


longopt="help,debug,package-name:,package-version:,\
bindir:,prefix:,datarootdir:,datadir:,docdir:,sysconfdir:,buildroot:"

export POSIXLY_CORRECT=1
ARGS=$(getopt -o h,d -l $longopt -n $ME -- "$@")
eval set -- "$ARGS"
while true ; do
   case "$1" in
      -h|--help)
         usage
         # printvariables
         exit
         ;;
      -d|--debug)
         DEBUG_SCRIPT=1
         shift
         ;;
      # 
      --package-name)
         PACKAGE=$2
         shift 2
         ;;
      --package-version)
         PACKAGE_VERSION="$2"
         shift 2
         ;;
      #
      --buildroot)
         BUILDROOT=$2
         shift 2
         ;;
      --bindir)
         BINDIR=$2
         shift 2
         ;;
      --prefix)
         PREFIX=$2
         shift 2
         ;;
      --datarootdir)
         DATAROOTDIR=$2
         shift 2
         ;;
      --datadir)
         DATADIR=$2
         shift 2
         ;;
      --docdir)
         DOCDIR=$2
         shift 2
         ;;
      --sysconfdir)
         SYSCONFDIR=$2
         shift 2
         ;;
      --printvariables)
         printvariables
         exit
         ;;
      --)
         break
         ;;
   esac
done
unset POSIXLY_CORRECT

PREFIX=$BUILDROOT$PREFIX
BINDIR=$PREFIX/$BINDIR
DATAROOTDIR=$PREFIX/$DATAROOTDIR
DATADIR=$PREFIX/$DATADIR
DOCDIR=$PREFIX/$DOCDIR
SYSCONFDIR=$BUILDROOT$SYSCONFDIR
XMLCONFDIR=$BUILDROOT/$XMLCONFDIR
XMLDIR=$PREFIX/$XMLDIR
XSLTROOTDIR=$PREFIX/$XSLTROOTDIR
# XSLTDIR=$PREFIX/$XSLTDIR

DBSTYLE_CATALOG=$PACKAGE.xml
DBSTYLE_CATALOG_ENTRY=for-catalog-${PACKAGE}-${PACKAGE_VERSION}.xml
DBSTYLE_DIR=$XMLDIR/docbook/stylesheet/nwalsh

printvariables

#----------------------------------------------------------------------
# 
#
function create_directory_structure() {
   local i
   my_debug "\n\n=== Create directory structure ==="
   for i in $PREFIX $BINDIR $DATAROOTDIR $DATADIR $XMLCONFDIR $DOCDIR/$PACKAGE/html $DBSTYLE_DIR/$PACKAGE_VERSION; do
      mkdir ${DEBUG_SCRIPT:+-v} -p $i
      [[ $? -ne 0 ]] && exit_on_error "Could not create $i directory"
   done
}


#----------------------------------------------------------------------
# 
#
function install_dbxsltdirs() {
   my_debug "\n\n=== Install all DocBook XSL stylesheet directories ==="
   
   cp ${DEBUG_SCRIPT:+-v} -a $DBXSLT_DIRS  $DBSTYLE_DIR/$PACKAGE_VERSION
}


function create_link() {
   my_debug "\n\n=== Create link ==="

   pushd $DBSTYLE_DIR 2>/dev/null
      ln -s $PACKAGE_VERSION current
   popd
}

function install_scripts() {
   my_debug "\n\n=== Install scripts ==="
   
   install ${DEBUG_SCRIPT:+-v} -m755 fo/pdf2index       $BINDIR
   install ${DEBUG_SCRIPT:+-v} -m755 epub/bin/dbtoepub  $BINDIR
   install ${DEBUG_SCRIPT:+-v} -m755 slides/images/callouts/gen.sh $BINDIR/callout-gen
   
#   rm fo/pdf2index epub/bin/dbtoepub slides/images/callouts/gen.sh
}

function install_dbxsltfiles() {
   my_debug "\n\n=== Install other files ==="
   
   cp ${DEBUG_SCRIPT:+-v} -a $DBXSLT_FILES $DBSTYLE_DIR/$PACKAGE_VERSION
}

function install_dbxsltdocfiles() {
   my_debug "\n\n=== Install documentation ==="
   my_debug "    Current dir: $PWD"
   
   
   for i in "$DBXSLT_DOCFILES"; do
      cp ${DEBUG_SCRIPT:+-v} -a $i  $DOCDIR/$PACKAGE
   done
}


function create_dbxslt_catalog() {
   my_debug "\n\n=== Create catalog file ==="
   
   # /etc/xml/docbook-xsl-stylesheets.xml
   xmlcatalog --noout --create $XMLCONFDIR/$DBSTYLE_CATALOG
}

function create_dbxslt_addentries() {
   my_debug "\n\n=== Create entries in the catalog file ==="
   
   xmlcatalog --noout --add "rewriteSystem" \
      "${DB4_XSLT_URL}${PACKAGE_VERSION}" \
      "file://$ORIG_DBSTYLE_DIR/${PACKAGE_VERSION}" $XMLCONFDIR/$DBSTYLE_CATALOG
   xmlcatalog --noout --add "rewriteURI" \
      "${DB4_XSLT_URL}${PACKAGE_VERSION}" \
      "file://$ORIG_DBSTYLE_DIR/$PACKAGE_VERSION" $XMLCONFDIR/$DBSTYLE_CATALOG
   xmlcatalog --noout --add "rewriteSystem" \
      "${DB4_XSLT_URL}current" \
      "file://$ORIG_DBSTYLE_DIR/current" $XMLCONFDIR/$DBSTYLE_CATALOG
   xmlcatalog --noout --add "rewriteURI" \
      "${DB4_XSLT_URL}current" \
      "file://$ORIG_DBSTYLE_DIR/current" $XMLCONFDIR/$DBSTYLE_CATALOG
   
   #
   # temporary file that is added to /etc/xml/suse-catalog.xml
   # during post
   #
   xmlcatalog --noout --create $XMLCONFDIR/${DBSTYLE_CATALOG_ENTRY}
   xmlcatalog --noout --add "delegateSystem" \
      "${DB4_XSLT_URL}" \
      "file:///etc/xml/${DBSTYLE_CATALOG}" $XMLCONFDIR/${DBSTYLE_CATALOG_ENTRY}
   sed -i -e "/<catalog/a\ <group id=\"${PACKAGE}\">" \
       -e '/<\/catalog/i\ </group>' $XMLCONFDIR/${DBSTYLE_CATALOG_ENTRY}
}


function cleanup_buildroot() {
   rm $DBSTYLE_DIR/$PACKAGE_VERSION/fo/pdf2index \
      $DBSTYLE_DIR/$PACKAGE_VERSION/epub/bin/dbtoepub \
      $DBSTYLE_DIR/$PACKAGE_VERSION/slides/images/callouts/gen.sh
}

#----------------------------------------------------------------------
# MAIN
#

# [[ -e "$PACKAGE_DIR-$PACKAGE_VERSION" ]] || exit_on_error "Could not find directory $PACKAGE_DIR-$PACKAGE_VERSION"
#
# cd $PACKAGE_DIR-$PACKAGE_VERSION
create_directory_structure
# create_link
install_dbxsltdirs
install_dbxsltfiles
install_scripts
install_dbxsltdocfiles
#
create_dbxslt_catalog
create_dbxslt_addentries
cleanup_buildroot

# EOF
