#!/bin/sh

# Don't hack the executable script by hand. Rather, edit the top-level 
# Makefile of the YODL package and do a "make installscripts".

############################################################## Some variables.
# What are the allowed destination formats? Corresponding .yo files must
# exist in system-wide include directory (e.g. in /usr/local/lib/yodl)!
FORMATS="tex html sgml txt man ms"

# What's the include dir?
STD_INCLUDE=/usr/local/lib/yodl

######################################################### Print msg to stderr.
chat()
{
    echo "$@" 1>&2
}

######################################################### Print error and die.
error()
{
    chat "$@"
    exit 1
}

################################################################## Usage info.
usage()
{
    cat << ENDUSAGE 1>&2

ICCE YODL Conversion Driver V1.22
Copyright (c) K. Kubat / ICCE 1996-1997. All rights reserved.
Another MegaHard Production!

Usage: yodl2.... [flags] inputfile
Where: 
    yodl2....: Specifies the conversion, the first part of the program name
        is always yodl2, the second part is the destination format, one 
        of $FORMATS
    flags:  Options for the yodl processor. Start yodl without arguments to 
        see what options are valid.
    inputfile: The file to process, extension .yo assumed.
This converter supplies the name of the macrofile that suits the conversion,
and makes sure that the output goes to the same name as the input file but
with the right extension. E.g., "yodl2html file" will read "file.yo" and 
write "file.html".

ENDUSAGE
    exit 1
}

####################################################### Run a command or exit.
run()
{
    $* || error "$1 indicates failure!"
}

############################################################# Start of script.
# Do we have arguments at all?
if [ -z "$1" ] ; then
    usage
fi

# Determine basename of this program.
base=`basename $0`

# Determine destination format.
dest=`IFS=2; set $base; echo $2`

# Check that format is one that we know.
found=no
for f in $FORMATS ; do
    if [ $f = $dest ] ; then
        found=yes
    fi
done
if [ $found = no ] ; then
    chat "Unknown destination format $dest. You can convert from yodl to:"
    chat $FORMATS
    error "Use e.g. yodl2$f for a conversion from YODL to $f."
fi

# Determine all flags.
outf=""
flags=""
files=$dest
while [ -n "$1" ] ; do
    case $1 in
	-o)
	    [ $# -eq 1 ] && usage
	    shift
	    outf=$1
	    shift
	    ;;
	-o*)
	    outf=`echo "$1" | sed 's/^..//'`
	    shift
	    ;;
        -*) 
            # if you don't have bash, use:
	    # flags=`echo $flags $1`
            flags="$flags $1"
            shift
            ;;
        *)
            # if you don't have bash, use:
	    # files=`echo $files $1`
            files="$files $1"
            if [ -z "$outf" ] ; then
                outf=`echo "$1" | sed 's/\.yo$//'`.$dest
            fi
            shift
            ;;
    esac
done

# Found an output file?
if [ -z "$outf" ] ; then
    usage
fi

# Run the YODL conversion.
run yodl -w $flags "-o$outf" $files

# See if there's a post-processor.
if [ -x $STD_INCLUDE/$base-post ] ; then
    chat "Running post-processor $STD_INCLUDE/$base-post.."
    run $STD_INCLUDE/$base-post $outf
fi
