#!/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'
debiandoc2info 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: debiandoc2info [options] <filename>.sgml
options: -h               print this help message
	 -v               be verbose
         -k               keep intermediate files
         -c               use content-negotiation
         -b <basename>    basename to be used
         -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;
}

## ----------------------------------------------------------------------
## check for presence of makeinfo
command -v makeinfo >/dev/null 2>&1 || usage_error "makeinfo not found"

## ----------------------------------------------------------------------
## set default values
verbose=false
keep=false
keepopt=''
content=''
basename=''
extension=''
locale=''
declaration=''
nsgmls_options=''

## ----------------------------------------------------------------------
## get command line options
options=":hvkcb:e:l:d:n:"
while getopts $options opt
do
    case $opt
    in
	h  ) usage_message
	     ;;
	v  ) verbose=true
	     ;;
	k  ) keep=true
	     keepopt="-$opt"
	     ;;
	c  ) content="-$opt"
	     ;;
	b  ) basename="-$opt $OPTARG"
	     ;;
	e  ) extension="-$opt $OPTARG"
	     ;;
	l  ) locale="-$opt $OPTARG"
	     ;;
	d  ) declaration="-$opt $OPTARG"
	     ;;
	n  ) nsgmls_options="-$opt $OPTARG $nsgmls_options"
	     ;;
	\? ) usage_error "unknown option \`$OPTARG'"
	     ;;
    esac
done
shift $(($OPTIND - 1))

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

## ----------------------------------------------------------------------
## get basename
if [ -n "$basename" ]
then
    bsn="$(echo $basename | cut -d' ' -f2- | cut -d'/' -f-1)"
else
    bsn="$(basename $1 .sgml)"
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="info"
fi
ext=".$ext"

## ----------------------------------------------------------------------
## what needs to be passed on to the backend
passing_on="$basename $locale"
passing_on="$passing_on $keepopt"
passing_on="$passing_on $nsgmls_options $declaration"

## ----------------------------------------------------------------------
## do the actual work
[ -e $bsn.texinfo ] && echo "$script: WARNING: overwriting $bsn.texinfo"
if ! debiandoc2texinfo $passing_on $1
then
    echo "$script: ERROR: $bsn.texinfo could not be generated properly"
    exit 1
fi
if !( $verbose || exec >/dev/null 2>&1; makeinfo -v -o $bsn$cnt$ext $bsn.texinfo )
then
    echo "$script: ERROR: $bsn$cnt$ext could not be generated properly"
    $verbose || echo "$script: rerun with the -v option to found out why"
    exit 1
fi

## ----------------------------------------------------------------------
## remove temporary files
$keep || rm -f $bsn.texinfo

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