#!/bin/sh

# Don't hack this script by hand if it's in one of your PATH directories.
# Instead, modify the pre-script in the YODL source package and do a 
# "make installscripts". This script is installed as yodl2manless AND
# as yodl2msless. Different groff macro commands, that's all.

#################################################### Print error msg and die.
error()
{
    echo 'yodl2msless:' $@ 1>&2
    exit 1
}

################################################### Print usage info and die.
usage()
{
    cat << ENDUSAGE
    
ICCE YODL to man/ms to less-pager conversion driver V1.22
Copyright (c) K. Kubat / ICCE 1996. All rights reserved.
Another MegaHard Production!

Usage: yodl2less [flags] file
Where:
    flags: Options for processing, run "yodl" without argument to see.
    file:  Input file, extension .yo assumed
This converter runs "yodl2ms" to convert the input file to groff
format, then runs "groff -t -Tascii -mgs" and pipes the output to
the "more" pager. 
Redirect to send the output to a file, as in
    yodl2msless file > output

ENDUSAGE

    exit 1
}

###################################################### Start of main program.

# get all flags
flags=""
inf=""
while [ "$1" != "" ] ; do
    case $1 in
        -*)
            # if you don't have bash, use
            # flags=`echo $flags $1`
            flags="$flags $1"
            shift
            ;;
        *)
            inf=$1
            shift
            ;;
    esac
done

# no input file, nogo
if [ "$inf" = "" ] ; then
    usage
fi

# determine output file
# if you don't have bash, use:
# echo $inf | grep '\.yo' > /dev/null 2>&1
# if [ $? -eq 0 ] ; then
#     troffile=`echo $inf | sed 's/\.yo/.ms/'`
# else
#     troffile=`echo $inf.ms`
# fi
troffile=${inf%%.yo}.ms

yodl2ms $flags $inf || error "YODL to ms conversion failed"

# are we redirected?
if [ -t ] ; then
    # nope.. pipe into less
    groff -t -Tascii -mgs $troffile | more
else
    # yep, send it wherever it goes
    groff -t -Tascii -mgs $troffile
fi
