#!/bin/sh
# printerutil - Copied from printerinit 10/14/00  Jerry Rains
configpf=$PFCONFIG
[ $configpf ] || configpf=$PFPROG/fp/lib/config
cd $PFPROG/fp/lib
[ -f $tmpdir/tmp ] && rm -f $tmpdir/tmp
> /tmp/ptypes
for j in `ls *.prt`
do
type=`$ECHO $j | sed -e "s/\.prt//"`
desc=`sed -n "/^# */s///p" $j`
[ "$type" != "readme" ] && $ECHO "$type~$desc" >> $tmpdir/tmp
done
cat $tmpdir/tmp | sort | uniq > $tmpdir/prtr
awk -F~ ' { printf "%-10.10s%-35.35s", $1,$2 >> (tmp) } ' tmp=/tmp/ptypes $tmpdir/prtr
> /tmp/printer.tmp
[ -f $tmpdir/config.prt2 ] && rm -f $tmpdir/config.prt2
nix=`uname`
case $nix in
    SCO_SV)	{
                # Add test to make sure /usr/spool/lp/admins/lp/printers exists
		[ -d /usr/spool/lp/admins/lp/printers ] && {
                if [ -n "$configpf" ] 
                then
                    grep -e '^pfprinter.*=' $configpf >> $tmpdir/config.prt2
                    IFS="~"
                    for j in `grep -e '^printer' $configpf`
                    do
                    $ECHO "$j" >> $tmpdir/config.prt2
                    done
                    IFS=" 	
"
                    count=`grep -e '^printer' $configpf | 
                        sed -n 's/^printer\([0-9]*\).*/\1/p' | 
                        awk '
                        $1 > count   {
                        count=$1 
                        }
                    END { print count+1 } '`
                else
                    count=1
                fi
                if [ -n "$configpf" ]
                then
                    tst=`grep -e "^pfprinter.*=" $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/^pfprinter/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
                        tst=`grep -e "^pfprinter.*=" $configpf`
                    fi 
                    tst=`grep -e '^printer.*= *file' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *file/printer$count=file/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi 
                    tst=`grep -e '^printer.*= *video' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *video/printer$count=video/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi
                else
                    cat $PFDIR/fp/config.prnt > $tmpdir/config.prt2 
                fi
		p=/usr/spool/lp/admins/lp/printers
                cd $p
                ls > $tmpdir/printerlist
		for j in `ls $p`
		do
                    # Make sure $j/configuration is a valid file
                    if [ -f $p/$j/configuration ] 
                    then
		        scotype=`sed -n "s,.*model/,,p" < $p/$j/configuration`
                        if [ -n "$configpf" ]
                        then
                            fptype1=`sed -n "s/^.*= *$j *,//p" < $configpf`
                            fptype=`$ECHO $fptype1 | sed -e "s/,.*//"`
                        fi
                        if [ -f $p/$j/comment ]
                        then
                            comment=`cat $p/$j/comment`
                        else
                            comment="none"
                        fi
                        [ $scotype ] || scotype="none"
                        [ $fptype ] || fptype="none"
		        $ECHO  $j$scotype$fptype"$comment" | awk -F ' { printf "%-15.15s%-15.15s%-15.15s%-65.65s%-65.65s", $1, $2, $3, "", $4 } ' >> /tmp/printer.tmp
		        count=`expr ${count:=0} + 1`
                    fi
		done
                }
		};;  # OSR5.0.5
    UnixWare)	{
                # Add test to make sure /usr/spool/lp/admins/lp/printers exists
		[ -d /usr/spool/lp/admins/lp/printers ] && {
                if [ -n "$configpf" ] 
                then
                    grep -e '^pfprinter.*=' $configpf >> $tmpdir/config.prt2
                    IFS="~"
                    for j in `grep -e '^printer' $configpf`
                    do
                    $ECHO "$j" >> $tmpdir/config.prt2
                    done
                    IFS=" 	
"
                    count=`grep -e '^printer' $configpf | 
                        sed -n 's/^printer\([0-9]*\).*/\1/p' | 
                        awk '
                        $1 > count   {
                        count=$1 
                        }
                    END { print count+1 } '`
                else
                    count=1
                fi
                if [ -n "$configpf" ]
                then
                    tst=`grep -e '^pfprinter.*=' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/^pfprinter/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
                        tst=`grep -e '^pfprinter.*=' $configpf`
                    fi 
                    tst=`grep -e '^printer.*= *file' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *file/printer$count=file/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi 
                    tst=`grep -e '^printer.*= *video' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *video/printer$count=video/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi
                else
                    cat $PFDIR/fp/config.prnt > $tmpdir/config.prt2 
                fi
		p=/usr/spool/lp/admins/lp/printers
                cd $p
                ls > $tmpdir/printerlist
		for j in `ls $p`
		do
                    # Make sure $j/configuration is a valid file
                    if [ -f $p/$j/configuration ]
                    then
		        scotype=`sed -n "s,.*model/,,p" < $p/$j/configuration`
                        if [ -n "$configpf" ]
                        then
                            fptype1=`sed -n "s/^.*= *$j *,//p" < $configpf`
                            fptype=`$ECHO $fptype1 | sed -e "s/,.*//"`
                        fi
                        [ $scotype ] || scotype="none"
                        [ $fptype ] || fptype="none"
		        $ECHO  $j" "$scotype" "$fptype | awk ' { printf "%-15.15s%-15.15s%-15.15s%65s%65s", $1, $2, $3, "", "" } ' >> /tmp/printer.tmp
		        count=`expr ${count:=0} + 1`
                    fi
		done
                }
		};;  # Unixware
    Linux)	{
                if [ -n "$configpf" ] 
                then
                    grep -e '^pfprinter.*=' $configpf >> $tmpdir/config.prt2
                    IFS="~"
                    for j in `grep -e '^printer' $configpf`
                    do
                    $ECHO "$j" >> $tmpdir/config.prt2
                    done
                    IFS=" 	
"
                    count=`grep -e '^printer' $configpf | 
                        sed -n 's/^printer\([0-9]*\).*/\1/p' | 
                        awk '
                        $1 > count   {
                        count=$1 
                        }
                    END { print count+1 } '`
                else
                    count=1
                fi
                if [ -n "$configpf" ]
                then
                    tst=`grep -e '^pfprinter.*=' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/^pfprinter/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
                        tst=`grep -e '^pfprinter.*=' $configpf`
                    fi 
                    tst=`grep -e '^printer.*= *file' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *file/printer$count=file/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi 
                    tst=`grep -e '^printer.*= *video' $configpf`
                    if [ -z "$tst" ]
                    then
                        sed -n "/printer/s/^printer.* *= *video/printer$count=video/p" < $PFDIR/fp/config.prnt >> $tmpdir/config.prt2
		        count=`expr ${count:=0} + 1`
                    fi
                else
                    cat $PFDIR/fp/config.prnt > $tmpdir/config.prt2 
                fi
		};;  # RH 6.1
    *)		{
                $ECHO "Only SCO printerinit is implemented."
                $ECHO -n "Press Enter to continue."
                read key
		};;   # RS6000
esac

[ -f $tmpdir/printerlist ] && chown filepro $tmpdir/printerlist
[ -f /tmp/printer.tmp ] && chown filepro /tmp/printer.tmp
[ -f $tmpdir/tmp ] && chown filepro $tmpdir/tmp
[ -f $tmpdir/config.prt2 ] && chown filepro $tmpdir/config.prt2
[ -f $tmpdir/prtr ] && chown filepro $tmpdir/prtr
[ -f /tmp/ptypes ] || > /tmp/ptypes
[ -f /tmp/ptypes ] && chown filepro /tmp/ptypes

$PFPROG/fp/dxmaint printers -ra -e
$PFPROG/fp/dxmaint printertype -ra -e
[ "$debugon" = "y" ] && {
    $ECHO "End of printerutil"
    read key
    }
