#!/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: /usr/u/warnold/plot/RCS/plotit.sh_sgi,v $
#    $Revision: 6.4 $
#     $Author: warnold $
#    $Date: 96/03/20 17:22:08 $

#
#  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
#         [-a]            - use alternate (hpgl/2) for direct plotter
#         [-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=""
plotcmd="hp_glplotf"

while getopts g:d:f:o:ast: arg
do
    case $arg in
    g)    plotterdestination="$OPTARG";;
    d)    plotterdevicename="$OPTARG" ;;
    f)    options="$options -$OPTARG" ;;
    o)    poptions="$poptions -o$OPTARG" ;;
	a)	  plotcmd="hp_gl2plotf" ;;
    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 \nfor output of
plotter type \"$plottertype\""
            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>\n\n  \
where options include:\n\n  \
[-o<lpopt>..]\t- <lpopt> is an option that will be passed to the\n  \
\t\t\t/usr/bin/lp command using lp's \"-ooption\" \n  \
[-f<opt>..]\t- <opt> is an option that will be passed to the \n  \
\t\t\tplotfile formatter \n  \
[-t<type>]\t- <type> is plotter type either postscript or the \n  \
\t\t\tHP/GL plotter model (no default)\n  \
[-s]\t\t- save alias plotfile\n  \
[-a]\t\t- use hp_gl2 filter for plotting diretly to plotter\n  \
[-d<dev>]\t- <dev> is the name by which /usr/bin/lp knows\n  \
\t\t\tthe 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" || $2 == "xfs" || $2 == "nfs" {print $5}'`
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.  
# Note: we now convert file size to 1K blocks
# this is to reduce the chance of numeric overflow on disks with > 2GB free

bloated_file_size=`expr $plot_file_size \* 5 / 4096`
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"
        $plotcmd $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



