#!/bin/sh
################################################################################
# Script Name: rlp
# Purpose: Used to print file from your local system to printers on other
#          network printers.  rlp will also check file type using the unix
#          file command. If file is executable or postscript it will not print.
#          This function is system dependent on what string file returns (adj
#          as needed.
#          rlp will except input from a pipe "|" but no check will be done.
# Options: rlp [-h hostname] [-d printername] [-n]"n means print line numbers".
# 
################################################################################


N=0           # Default (no line numbers)

set -- `getopt h:d:n $@ 2>/dev/null`              # set up command line

for i in $@                                       # parse command line
do
    case $i in
        -h) H="$2"              # assigns string after the -h to H (host)
           shift 2;;           # moves over 2 places on command line.
        -d) D="-d$2"            # assigns string after the -d to D (destination)
            shift 2;;           # moves over 2 places on command line.
        -n) N="1"               # assigns 1  to N (numbers)
            shift 1;;           # moves over 1 place on command line.
        --) shift               # no more options.
            break;;             # exit loop.
    esac
done
    

if [ -z "$H" ]; then
    H="devel"           # Sets default host (wont have to always give opts)
fi

if [ -z "$D" ]; then   # Sets default dest. (wont have to always give opts)
    D="-dpcl2"
fi

################################################################################
# BELOW:
# If more than one file on command line. Loop through all files on command
# line one at a time. Basename will strip off the full path from each file,
# if  it  was given, and assign the file name to
# the shell variable REMOTENAME. ($i still contains the full path)
# The unix file command is invocted on $i and the output is piped to awk.
# Awk strips off the fist column of the file commands output (the file name)
# and assigns what is left to TYPE. Note: the ` ` backquoats let you do 
# this type of assignment, which is called command substitution.
# The string in $TYPE is echoed to egrep -i which checks for the words " text or
# /bin/sh "  in upper or lower case.
# If either of these words are found then the file could be ansii test of some 
# kind or postscript text.  We don't want to print postscript text on a line
# printer so send $TYPE through grep and check for postscript.  If post script 
# is found ($? = 0) echo the error message and exit the script.
# If the egrep -i didn't find either of the words text or /bin/sh, the exit 
# status (?$) will be somthing other than 0, ([1]*) so echo message and exit.
################################################################################

if [ $# -ge 1 ]; then       
    for i in $@             
    do
        REMOTENAME="`basename $i`"   
                     
        TYPE=`file $i | awk '{print $2, $3, $4, $5}'` 

        echo "${TYPE}" | egrep -i '(text|/bin/sh)'  > /dev/null 2>&1

        case $? in
            0) echo "${TYPE}" | grep -i postscript  > /dev/null 2>&1
               if [ $? -eq 0 ]; then 

                   echo "$i, is of the type, ${TYPE},  and can not be printed!!"
                else 

################################################################################
# BELOW:
# If $N = 1 (line numbers on), do a word count so you can see how big the file
# is. Use the pr command to format the output to include line numbers and
# put the formated vertion in a file $i.$LOGNAME.$$ (filename.lognameCurrentPID)
# the -t option of pr will omit the default page header and trailing blank
# line pr normaly would insert. Oprion -n5 will alow the line numbers to
# go as high as 99999.
# Next rcp (remote copy) the $i.$LOGNAME$$ (filename.yourloginnameCurrentPID)
# to $H (remote host) /usr/tmp $REMOTENAME (filename striped of path).$LOGNAME$$
# Next remove $i.$LOGNAME$$ on your local machine.
################################################################################
                    case $N in         # Line number option was given
                        1) wc "$i"     # do a word count
                           pr -t -n5 "$i" > $i.$LOGNAME$$ # add line numbers
                           rcp $i.$LOGNAME$$ $H:/usr/tmp/$REMOTENAME.$LOGNAME$$
                           rm $i.$LOGNAME$$
EXITSTAT=`/bin/rsh $H "sh -c 'lp $D -c -s /usr/tmp/$REMOTENAME.$LOGNAME$$; echo \\$?'"`
                           if [ $EXITSTAT -eq 0 ]; then
                               echo "$i is printing on $D from Host $H!"
                                /bin/rsh $H rm /usr/tmp/$REMOTENAME.$LOGNAME$$
                           else
                               echo "The print command has failed! " 2>&1
                           fi
                           ;;   
                         0) rcp $i $H:/usr/tmp/$REMOTENAME.$LOGNAME$$
EXITSTAT=`/bin/rsh $H "sh -c 'lp $D -c -s /usr/tmp/$REMOTENAME.$LOGNAME$$; echo \\$?'"`
                            if [ $EXITSTAT -eq 0 ]; then
                                echo "$i is printing on $D from Host $H!"
                                /bin/rsh $H rm /usr/tmp/$REMOTENAME.$LOGNAME$$
                            else
                                echo "The print command has failed! " 2>&1
                            fi
                            ;;
                    esac
                fi
                   ;;
             [1]*) echo "$i, is of the type, ${TYPE}, and can not be printed!!"
                   ;;
        esac
    done
    
else
    cat > /usr/tmp/$LOGNAME.$$
    i=/usr/tmp/$LOGNAME.$$
    REMOTENAME="`basename $i`"
    
    case $N in
          1) wc "$i"
             pr -t -n5 "$i" > $i.$LOGNAME$$
             rcp $i.$LOGNAME$$ $H:/usr/tmp/$REMOTENAME.$LOGNAME$$
             rm $i
EXITSTAT=`/bin/rsh $H "sh -c 'lp $D -c -s /usr/tmp/$REMOTENAME.$LOGNAME$$; echo \\$?'"`
             if [ $EXITSTAT -eq 0 ]; then
                 echo "$i is printing on $D from Host $H!"
                  /bin/rsh $H rm /usr/tmp/$REMOTENAME.$LOGNAME$$
             else
                 echo "The print command has failed! " 2>&1
             fi
             ;;   
           0) rcp $i $H:/usr/tmp/$REMOTENAME.$LOGNAME$$
EXITSTAT=`/bin/rsh $H "sh -c 'lp $D -c -s /usr/tmp/$REMOTENAME.$LOGNAME$$; echo \\$?'"`
             if [ $EXITSTAT -eq 0 ]; then
                  rm $i
                  echo "$i is printing on $D from Host $H!"
                  /bin/rsh $H rm /usr/tmp/$REMOTENAME.$LOGNAME$$
              else
                  echo "The print command has failed! " 2>&1
              fi
              ;;
       esac
fi