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

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

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

## ----------------------------------------------------------------------
## print help message
function usage_message
{
    cat >&2 <<'END'
debiandoc2@@@format@@@ version 1.1

Copyright (C) 1998,1999,2000 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: debiandoc2@@@format@@@ [options] <filename>.sgml
options: -h               print this help message
         -k               keep intermediate files
@@@comment@@@
	 -m               put comments in output
@@@endcomment@@@
@@@stdout@@@
         -O               send output to stdout instead of <filename>.@@@ext@@@
@@@endstdout@@@
         -c               use content-negotiation
         -b <basename>    basename to be used
@@@topname@@@
         -t <topname>     topname to be used
@@@endtopname@@@
         -e <extension>   extension to be used
         -l <locale>      locale to be used
	 -d <declaration> SGML declaration to be used
         -n <options>     nsgmls options to be passed on
END
    exit 0;
}

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

## ----------------------------------------------------------------------
## set default values
keep=false
@@@comment@@@
comment=''
@@@endcomment@@@
stdout=false
content=''
basename=''
@@@topname@@@
topname=''
@@@endtopname@@@
extension=''
locale=''
declaration=''
nsgmls_options=''

## ----------------------------------------------------------------------
## get command line options
options=":hkcb:e:l:d:n:"
@@@comment@@@
options="${options}m"
@@@endcomment@@@
@@@stdout@@@
options="${options}O"
@@@endstdout@@@
@@@topname@@@
options="${options}t:"
@@@endtopname@@@
while getopts $options opt
do
    case $opt
    in
	h  ) usage_message
	     ;;
	k  ) keep=true
	     ;;
@@@comment@@@
	m  ) comment="-$opt"
	     ;;
@@@endcomment@@@
@@@stdout@@@
	O  ) stdout=true
	     ;;
@@@endstdout@@@
	c  ) content="-$opt"
	     ;;
	b  ) basename="-$opt $OPTARG"
	     ;;
@@@topname@@@
	t  ) topname="-$opt $OPTARG"
	     ;;
@@@endtopname@@@
	e  ) extension="-$opt $OPTARG"
	     ;;
	l  ) locale="-$opt $OPTARG"
	     ;;
	d  ) declaration="-$opt $OPTARG"
	     ;;
	n  ) nsgmls_options="$nsgmls_options $OPTARG"
	     ;;
	\? ) usage_error "unknown option \`$OPTARG'"
	     ;;
    esac
done
shift $(($OPTIND - 1))

## ----------------------------------------------------------------------
## check remaining command line options
[ $# = 1 ] || usage_error "need exactly one input filename"

## ----------------------------------------------------------------------
## get input file name
case "$1"
in
    - )   nsgmlsi="-"
	  ! $keep || usage_error "-k not possible with input from stdin"
@@@stdout@@@
	  stdout=true
@@@endstdout@@@
	  $stdout || usage_error "stdin not allowed with debiandoc2@@@format@@@"
	  ;;
    -?* ) nsgmlsi="./$1"
	  ;;
    * )   nsgmlsi="$1"
	  ;;
esac

## ----------------------------------------------------------------------
## get basename
if [ -n "$basename" ]
then
    bsn="$(echo $basename | cut -d' ' -f2- | cut -d'/' -f-1)"
else
    if [ "$nsgmlsi" != "-" ]
    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-)"
    elif [ -n "$LANG" ]
    then
	cnt="$LANG"
    else
	cnt="en"
    fi
    cnt=".$cnt"
fi

## ----------------------------------------------------------------------
## get extension
if [ -n "$extension" ]
then
    ext="$(echo $extension | cut -d' ' -f2-)"
else
    ext="@@@ext@@@"
    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-@@@format@@@"

## ----------------------------------------------------------------------
## what needs to be passed on to the backend
passing_on="$basename $extension $locale $content"
@@@topname@@@
passing_on="$passing_on $topname"
@@@endtopname@@@
@@@comment@@@
passing_on="$passing_on $comment"
@@@endcomment@@@

## ----------------------------------------------------------------------
## set environment
set -a
PATH=@@@helperdir@@@:${PATH}
set +a

## ----------------------------------------------------------------------
## do the actual work
nsgmls -oline $nsgmls_options $declaration $nsgmlsi >$tf1
saspconvert @@@toc@@@ <$tf1 >$tf2
@@@stdout@@@
$stdout || exec >$bsn$cnt$ext
@@@endstdout@@@
sgmlspl @@@perldir@@@/Driver.pm -f @@@format@@@ $passing_on <$tf2

## ----------------------------------------------------------------------
## remove temporary files
$keep || rm -f $tf1 $tf2

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