#!/bin/sh
##############################################################################
#                                                                            #
#            Copyright (C) 1985, 1986, 1987, Alias Research, Inc.            #
#                                                                            #
#    These coded instructions,  statements and  computer programs contain    #
#    unpublished information proprietary to Alias Research, Inc.  and are    #
#    protected by the Canadian and US Federal copyright law. They may not    #
#    be disclosed to third parties  or copied  or duplicated, in whole or    #
#    in part,  without the prior written consent of Alias Research,  Inc.    #
#                                                                            #
# Unpublished-rights reserved under the Copyright Laws of the United States. #
#                                                                            #
##############################################################################

#     $Source: /tmp_mnt/worf/root/v/alias5.1/src/Utility/plot/RCS/plotit.sh_sgi,v $
#    $Revision: 5.0 $
#     $Author: software $
#    $Date: 94/03/23 02:35:40 $

#
#  filename: plotit
#  purpose : to be called by alias3 when alias3 has a file it wants to plot
#
#  interactive usage:
#
#  plotit [options] <file>
#
#  where options include:
#
#         [-o<lpopt> ...] - <lpopt> is an option that will be passed to the
#                           /usr/bin/lp command using lp's "-ooption"
#         [-f<opt> ...]   - <opt> is an option that will be passed to the
#                           plotfile formatter
#         [-t<type>]      - <type> is plotter type either postscript or the
#                           HP/GL plotter model (no default)
#		  [-g<destin>]    - what to do with the plot when finished (plotter,
#						    hp_gl, hp_gl2, postscript, plotfile...
#         [-s]            - save alias plotfile
#         [-d<dev>]       - <dev> is the name by which /usr/bin/lp knows
#                           the plotter (default is default system printer)
#
# 
#    Success of the plot script is indicated by exit codes:
#
#    0 - command succeeded
#    1 - command failed during actual execution of `lp'
#        command.
#    2 - command failed due to insufficient disk space
#        in /usr.
#    3 - unknown plotter type
# 

DEF_OPTIONS=""
DEF_POPTIONS=""
DEF_PLOTTERTYPE="postscript"
DEF_PLOTTERDEVICE=""
DEF_PLOTTERDESTINATION="plotter"

TRUE=1
FALSE=0

errs=/tmp/plot_errs$$
save_plotfile=$FALSE
options=""

lp=/usr/bin/lp

mesg()
{
    echo "$1" 1>&2
    echo "$1" | aliasWarning -b Ok -t "Plot Message" -f - > /dev/null
}

error()
{
    echo $1 1>&2
    echo $1 | aliasWarning -b Ok -t "Plot Errors" -f - > /dev/null
    exit 1
}

plotterdevicename=""
while getopts g:d:f:o:st: arg
do
    case $arg in
    g)    plotterdestination="$OPTARG";;
    d)    plotterdevicename="$OPTARG" ;;
    f)    options="$options -$OPTARG" ;;
    o)    poptions="$poptions -o$OPTARG" ;;
    s)    save_plotfile=$TRUE ;;
    t)    plottertype="$OPTARG" ;;
    \?)
        error "invalid plotit option -$arg" ;;
    esac
done

if [ "$plotterdestination" = "plotter" -o "$plotterdestination" = "postscript" ] ; then
    if [ "$plotterdevicename" != ""  ] ; then
        if [ -x /usr/spool/lp/interface/$plotterdevicename  ] ; then
            plotterdevice="-d$plotterdevicename"
        else
            error "Device \"$plotterdevicename\" does not exist"
            exit -1
        fi
    fi
fi

if [ "$options" = "" ] ; then
    options="$DEF_OPTIONS"
fi
if [ "$poptions" = "" ] ; then
    poptions="$DEF_POPTIONS"
fi
if [ "$plottertype" = "" ] ; then
    plottertype="$DEF_PLOTTERTYPE"
fi
if [ "$plotterdestination" = "" ] ; then
    plotterdestination="$DEF_PLOTTERDESTINATION"
fi

if [ "$plotterdestination" = "plotter" ] ; then

    if [ "$plottertype" = "postscript" ] ; then

        plotterdestination="postscript"
    fi
fi

#
#    shift past all options
#
shift `expr $OPTIND - 1`

if [ $# -ne 1 ]; then
    mesg "Usage:\tplotit [options] <file>"
    mesg ""
    mesg "where options include:"
    mesg ""
    mesg "\t[-o<lpopt> ...]\t- <lpopt> is an option that will be passed to the"
    mesg "\t\t\t  /usr/bin/lp command using lp\'s \"-ooption\""
    mesg "\t[-f<opt> ...]\t- <opt> is an option that will be passed to the"
    mesg "\t\t\t  plotfile formatter"
    mesg "\t[-t<type>]\t- <type> is plotter type either postscript or the"
    mesg "\t\t\t  HP/GL plotter model \(no default\)"
    mesg "\t[-s]\t\t- save alias plotfile"
    mesg "\t[-d<dev>]\t- <dev> is the name by which /usr/bin/lp knows"
    mesg "\t\t\t  the plotter \(default is system printer\)"
    exit 1
fi

plotfile=$1

if [ ! -r $plotfile ]; then
    error "\"$plotfile\" is an invalid file"
fi

usr_free_space=`/bin/df -k /usr | awk '$2 == "efs" {print $5}'`
usr_free_space=`expr $usr_free_space \* 1024`
plot_file_size=`/bin/ls -l $plotfile | awk '{print $5}'`

# Check for space 25% larger than input file in order to
# attempt to print the file.

bloated_file_size=`expr $plot_file_size \* 5 / 4`
if [ $bloated_file_size -ge $usr_free_space ]; then
    error "Insufficient disk space"
    exit 2
fi

case "$plotterdestination" in
    "postscriptfile" )
        psplotf $options <$plotfile > $plotfile.ps 2> $errs ;;
    "hp_glfile" )
        options="-m$plottertype $options"
        hp_glplotf $options $plotfile $plotfile.hp 2> $errs ;;
    "hp_gl2file" )
        options="-m$plottertype $options"
        hp_gl2plotf $options $plotfile $plotfile.hp 2> $errs ;;
    "plotfile"  )
        mv $plotfile $plotfile.pf ;;
    "postscript" )
        psplotf $options <$plotfile | \
        $lp $plotterdevice $poptions > /dev/null 2> $errs ;;
    "plotter" )
        options="-m$plottertype $options"
        hp_glplotf $options $plotfile | \
        $lp $plotterdevice $poptions > /dev/null 2> $errs ;;
    * )
        error "Unknown destination"
        exit 3 ;;
esac
status=$?

# the documentation says that we're supposed to blow the file away ...

if [ $save_plotfile -eq $FALSE ]; then
    /bin/rm -f $plotfile
fi

if [ -s $errs -o $status -ne 0 ]; then
    # send errors to stderr
    cat $errs 1>&2
    cat $errs | aliasWarning -b Ok -t "Plot Errors" -f -
    /bin/rm -f $errs
    exit 1
else
    /bin/rm -f $errs
    exit 0
fi



