%Z%******************************************************************
%Z%    Filename : %M%
%Z%
%Z%     Version : %I%
%Z%
%Z%       Usage : base2co-lpd pipe-file "command"
%Z%
%Z%   Arguments : 1: name of named pipe (FIFO) from which to suck data 
%Z%               2: command to execute (usually lp) each time data   
%Z%                  are found on the pipe.  Use the shell's quoting 
%Z%                 mechanism if there are spaces (eg, 'lp -C -d x').
%Z%
%Z%   Returns   : should never return (continuously loops) except if
%Z%               incorrect args.
%Z%             : stderr will report errors
%Z%
%Z%   Purpose   : This program continuously loops, opening up a named
%Z%               pipe, waiting for data to arrive (open() blocks
%Z%               in kernel until there is a writer for the pipe), and
%Z%               only if there is at least some data does this
%Z%               program start a print job (eliminates null print
%Z%               jobs).
%Z%
%Z%   Notes     : This should still be run from init[tab] in "respawn"
%Z%               mode in case it dies for some reason, then will be
%Z%               restarted, despite that it SHOULD never exit.
%Z%
%Z%               To save process table slots, the print command (the 
%Z%               second arg) should begin with the string "exec ".
%Z%
%Z%  COPYRIGHT (C) Base 2 Companies, Inc. 1996, 1997
%Z%******************************************************************


**********************************************************************
QUICK START INSTRUCTIONS:
**********************************************************************

1 ) Create device node pipe file in /dev/ as root. Name it well.
    A) mknod /dev/b2lp0 p 
       This creates the pipe check man pages on mknod.

2 ) Test command from command line as root. 
    A) # base2co-lpd /dev/b2lp0 'lp -d lp0 -cs' &
       Note use -s for suppressing print job number.
            use -c to copy the file into the spooler instead of linking.
            use -d to specify Unix lp spooler name.
       Note use ampersand to send to the background.

3 ) Test pipe file by catting any file direct to the pipe file. 
    A) e.g.  cat /etc/motd >/dev/b2lp0 
       Note this should print the message of the day file.

4 ) When your sure this is working.
    A) Kill the base2co-lpd process you started.
    B) edit /etc/inittab and add a line near the physical port if the 
            printer has one.
       Give it a unique id.
       And Set it to respawn.
    C) e.g.

blp1:respawn:/usr/iwi/bin/base2co-lpd /dev/b2lp0 'lp -d lp0 -cs' >/dev/null 2>&1

       Note: see init man pages. any changes made to inittab should be put in
             the file that builds it in /etc/init.d directory.


**********************************************************************
EXAMPLE 1
**********************************************************************

Given:  Using Accuplus [D]irect printer type of name "LP01" device "/dev/lp0"
        Using Accuplus [A]pplication spooler type of name "SP01"

        flow:  ACCUPLUS -->SP01->LP01->/dev/lp0->paper_report

        Using IQ3 to print reports to:

            Unix lp spooler printer named "lp0" of device "/dev/lp0"
            Unix lp spooler printer named "lp1" of device "/dev/lp1"

        flow:  IQ3 -->lp->/dev/lp0->paper_report

Problem: print jobs crash together on the printer lp0
Reason: ACCUPLUS wants a printer to open.

So give it a file instead!

Make a "pipe" file to tell LP01 to use as a device

mknod /dev/b2lp0 p

Change LP01 set up in accuplus:
    change device to /dev/b2lp0

test base2co-lpd by typing the command:

/usr/bin/base2co-lpd /dev/b2lp0 'lp -dlp0 -cs'

        flow:  ACCUPLUS -->SP01->LP01->/dev/b2lp0->base2co-lpd->lp->lp0->paper_report
Benefits:
   No more crashed reports.
   Clears user screen quickly.
   Free's up printer device in accuplus quickly.


**********************************************************************
EXAMPLE 2
**********************************************************************

Given:  Using Accuplus [D]irect printer type of name "LP01" device "/dev/lp0"
        Using Accuplus [A]pplication spooler type of name "SP01"

        flow:  ACCUPLUS -->SP01->LP01->/dev/lp0->paper_report

        Unix lp spooler printer named "lp0" of device "/dev/lp0"
        Unix lp spooler printer named "lp1" of device "/dev/lp1"

        flow: lp->/dev/lp0->paper_report

Problem: You want a single report to print on copy to lp0 and two
         copies to lp1
Reason: Different forms and paper loaded in different places.


Make a "pipe" file to use as a device

mknod /dev/lp_split p

ADD a printer called LP05 in accuplus:
    set device to /dev/lp_split
    set type to Direct
ADD a printer called SP05 in accuplus:
    set device to LP05
    set type to Application

test base2co-lpd by typing the command:

/usr/bin/base2co-lpd /dev/lpsplit \
'tee /tmp/split.$$|lp -d lp0 -cs;lp -d lp1 -cs -n2 /tmp/split.$$;rm /tmp/split.$$'&

flow:
ACCUPLUS -->SP05->LP05->/dev/lp_split->base2co-lpd->lp->lp0->paper_report
                                          !---->lp->lp1->paper_report

Benefits:
   No more crashed reports.
   Clears user screen quickly.
   Free's up printer device in accuplus quickly.
   multiple copies.


**********************************************************************
EXAMPLE 3
**********************************************************************

Same as above but mail a copy to user joe_b@sonwil.attmail.com 
instead of printing 2 copies on the printer lp1

/usr/bin/base2co-lpd /dev/lpsplit \
                'tee /tmp/split.$$|lp -d lp0 -cs; \
                 mailx -s"Report File" joe_b@sonwil.attmail.com </tmp/split.$$;\
                 rm /tmp/split.$$' &

flow:
ACCUPLUS -->SP05->LP05->/dev/lp_split->base2co-lpd->lp->lp0->paper_report
                                          !---->mail->joe_b@sonwil.attmail.com

Benefits:
   Clears user screen quickly.
   Free's up printer device in accuplus quickly.
   facilitates mail, any program, and any shell or program you write.


**********************************************************************
PERMANENT IMPLEMENTATION
**********************************************************************

Once you have tested a command you can put an entry inittab or a startup script.

*********************
ADD AS STARTUP SCRIPT
*********************
create a file called S99b2c-lpd in /etc/rc2.d directory

put your command in this file:
/usr/bin/base2co-lpd /dev/b2lp0 "lp -dlp0 -cs" >/dev/null 2>&1 &

make it executable
chmod 777 S99b2c-lpd

if you can, link it to a startup script filename:
ln /etc/rc2.d/S99b2c-lpd /usr/bin/start_b2c-lpd

**************
ADD TO INITTAB
**************

create a file called b2c-lpd in /etc/conf/init.d/
enter a line like this one:
b201:234:respawn:/usr/bin/base2co-lpd /dev/b2lp0 "/bin/lp -dlp0 -cs" >/dev/null 2>&1

then edit /etc/inittab and read in this file.
vi /etc/inittab
type :$<return>
type :r /etc/conf/init.d/b2c-lpd<return>

then kill the base2co-lpd that is running. It should restart automatically.

***************
IMPORTANT NOTES
***************

Unix can be particular about many things. Some limitations have been found. 

-- Some security set-ups require a non-root user to start the daemon. 
   Consequently, the above startup method must be modified to run as a 
   regular user. Add a user to the system called base2co. Then use the 
   su command to change users from root to base2co before executing 
   base2co-lpd.
   e.g.
 su base2co -c "/usr/bin/base2co-lpd /dev/b2lp0 '/bin/lp -dlp0 -cs' >/dev/null 2>&1"

-- You can redirect standard error to a file to debug
   e.g.
       /usr/bin/base2co-lpd /dev/b2lp0 '/bin/lp -dlp0 -cs' >/dev/null 2>/tmp/b2err
or 
       su base2co -c "/usr/bin/base2co-lpd /dev/b2lp0 '/bin/lp -dlp0 -cs' >/dev/null 2>/tmp/b2err"
