#!/bin/bash
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
## exit on erroneous subcommand
set -e

## ----------------------------------------------------------------------
## get script name
script=$(basename ${0})

## ----------------------------------------------------------------------
## print version and usage message
function usage_message
{
    cat >&2 <<'END'
debiandoc2texinfo version 1.1

Copyright (C) 2005-2008 Osamu Aoki
Copyright (C) 1998-2004 Ardo van Rangelrooij
Copyright (C) 1996 Ian Jackson

This is free software; see the GNU General Public Licence
version 2 or later for copying conditions.  There is NO warranty.

usage: debiandoc2texinfo [options] <filename>.sgml
options: -h               print this help message
         -O               send output to stdout instead of <filename>.texinfo
         -b <basename>    basename to be used
         -c               use content-negotiation (w/  encoding suffix)
         -C               use content-negotiation (w/o encoding suffix)
	 -d <declaration> SGML declaration to be used
         -e <extension>   extension to be used
         -k               keep intermediate files
         -l <locale>      locale to be used
         -s <script>      apply script on generated file
         -n <options>     onsgmls options to be passed on
         -X <custom_dir>  switch locale dependent format data tree
END
    exit 0;
}

## ----------------------------------------------------------------------
## print error message
function usage_error
{
    echo >&2 "${script}: ${@}";
    exit 2;
}

## ----------------------------------------------------------------------
## set default values
basename=''
content=''
contentx=''
postprocessing=''
declaration='-d /usr/share/sgml/debiandoc/dtd/sgml/1.0/debiandoc.dcl'
extension=''
keep=false
locale=''
onsgmls=''
stdout=false
custom_dir=''

## ----------------------------------------------------------------------
## get command line options
options=':'
options="${options}h"
options="${options}O"
options="${options}b:"
options="${options}c"
options="${options}C"
options="${options}s:"
options="${options}d:"
options="${options}e:"
options="${options}k"
options="${options}l:"
options="${options}n:"
options="${options}X:"
while getopts ${options} opt
do
    case ${opt}
    in
	h  ) usage_message
	     ;;
	O  ) stdout=true
	     ;;
	b  ) basename="-${opt} ${OPTARG}"
	     ;;
	c  ) content="-${opt}"
	     ;;
	C  ) contentx="-${opt}"
	     ;;
	s  ) postprocessing="${OPTARG}"
	     ;;
	d  ) declaration="-${opt} ${OPTARG}"
	     ;;
	e  ) extension="-${opt} ${OPTARG}"
	     ;;
	k  ) keep=true
	     ;;
	l  ) locale="-${opt} ${OPTARG}"
	     ;;
	n  ) onsgmls="${onsgmls} ${OPTARG}"
	     ;;
	X  ) custom_dir="${OPTARG}"
	     ;;
	\? ) usage_error "unknown option \`${OPTARG}'"
	     ;;
    esac
done
shift $((${OPTIND} - 1))

## ----------------------------------------------------------------------
## check remaining command line options
if [ ${#} -ne 1 ]
then
    usage_error "need exactly one input filename: $*"
fi

## ----------------------------------------------------------------------
## get input file name
case "${1}"
in
    -   ) onsgmlsi="-"
	  if ${keep}
	  then
	      usage_error "-k not possible with input from stdin"
	  fi
	  stdout=true
	  ;;
    -?* ) onsgmlsi="./${1}"
	  ;;
    *   ) onsgmlsi="${1}"
	  ;;
esac

## ----------------------------------------------------------------------
## get basename
if [ -n "${basename}" ]
then
    bsn="$(echo ${basename} | cut -d' ' -f2- | cut -d'/' -f-1)"
else
    if [ "${onsgmlsi}" != "-" ]
    then
	bsn="$(basename ${1} .sgml)"
	basename="-b ${bsn}"
    fi
fi
case "${bsn}"
in
    -* ) bsn="./${bsn}" 
	 ;;
esac

## ----------------------------------------------------------------------
## get content-negotiation
cnt=''
if [ -n "${content}" ]
then
    if [ -n "${locale}" ]
    then
	cnt="$(echo ${locale} | cut -d' ' -f2-)"
    else
	cnt='en'
    fi
    cnt=".${cnt}"
fi
if [ -n "${contentx}" ]
then
    if [ -n "${locale}" ]
    then
	cnt="$(echo ${locale} | cut -d' ' -f2- | sed -e 's/[@\.].*$//')"
    else
	cnt='en'
    fi
    cnt=".${cnt}"
fi

## ----------------------------------------------------------------------
## get extension
if [ -n "${extension}" ]
then
    ext="$(echo ${extension} | cut -d' ' -f2-)"
else
    ext='texinfo'
    extension="-e ${ext}"
fi
ext=".${ext}"

## ----------------------------------------------------------------------
## get declaration
if [ -n "${declaration}" ]
then
    declaration="$(echo ${declaration} | cut -d' ' -f2-)"
    if [ "$(basename ${declaration})" = "${declaration}" ]
    then
	declaration="declaration/${declaration}"
    fi
fi

## ----------------------------------------------------------------------
## construct temporary file names
tf1="${bsn}.sasp"
tf2="${bsn}.sasp-texinfo"

## ----------------------------------------------------------------------
## what needs to be passed on to the backend
passing_on=''
passing_on="${passing_on} ${basename}"
passing_on="${passing_on} ${content}"
passing_on="${passing_on} ${contentx}"
passing_on="${passing_on} ${extension}"
passing_on="${passing_on} ${locale}"
if [ -n "${custom_dir}" ]; then
  passing_on="${passing_on} -X ${custom_dir}"
fi
if [ -n "${cnt}" ]; then
  # Remove Largest Suffix Pattern if -C or -c is used.
  bsn="${bsn%%.*}"
fi
## ----------------------------------------------------------------------
## do the actual work
onsgmls -oline ${onsgmls} ${declaration} ${onsgmlsi} >${tf1}
/usr/share/debiandoc-sgml/saspconvert <${tf1} >${tf2}
if ! ${stdout}
then
    exec >${bsn}${cnt}${ext}
fi
sgmlspl /usr/share/perl5/DebianDoc_SGML/Format/Driver.pm -f texinfo ${passing_on} <${tf2}
if [ -n "${postprocessing}" ]
then
    mv ${bsn}${cnt}${ext} ${bsn}${cnt}${ext}-in
    "${postprocessing}" ${locale} ${bsn}${cnt}${ext}-in ${bsn}${cnt}${ext}
fi

## ----------------------------------------------------------------------
## remove temporary files
if ! ${keep}
then
    rm -f ${tf1} ${tf2} ${bsn}${cnt}${ext}-in
fi

## ----------------------------------------------------------------------
exit 0

## ----------------------------------------------------------------------
