**********************************************************************
*
*  INIT.ASM for an Amiga printer driver for the Hewlett Packard DeskJet
*
*                    Copywrite © 1989 Creative Focus
*
*   This software is freely redistributable, as long as all code,
*   comments, and documentation are included unaltered, and no other
*   conditions are imposed on its redistribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS," AND NO GUARANTEE IS MADE OF ITS
*   FITNESS FOR ANY PURPOSE.  CREATIVE FOCUS MAY NOT BE HELD ACCOUNTABLE
*   FOR ANY DAMAGE, REAL OR IMAGINED, RESULTING FROM ITS USE OR MISUSE.
*
*                           Creative Focus
*                           Dr. Gerald Hull
*                           12 White Street
*                           Binghamton, New York  13901
*
*                           (607) 648-4082
*                           BIX:  ghull
*                           PLINK:  DRJERRY
*
***********************************************************************

SP    equr     a7    * stack pointer
LP    equr     a6    * library pointer

*  macros from exec/libraries.i

CALLLIB        macro
      ifgt     NARG-1
      fail
      endc
      jsr      \1(LP)
      endm

LINKLIB        macro
      ifgt     NARG-2
      fail
      endc
      move.l   LP,-(SP)
      move.l   \2,LP
      CALLLIB  \1
      move.l   (SP)+,LP
      endm

* another useful macro

OPENBASE       macro
      lea      \1(PC),a1
      moveq    #0,d0
      CALLLIB  _LVOOpenLibrary
      move.l   d0,\2
      endm

      xref     _LVOCloseLibrary
      xref     _LVOOpenLibrary
      xref     _AbsExecBase
      xref     _PEDData

      xdef     _Init
      xdef     _Expunge
      xdef     _Open
      xdef     _PD
      xdef     _PED
      xdef     _SysBase
      xdef     _DOSBase
      xdef     _GfxBase
      xdef     _IntuitionBase

_Init move.l   4(SP),_PD         * set up PrinterData pointer
      lea      _PEDData(PC),a0
      move.l   a0,_PED           * and PrinterExtendedData Pointer
      move.l   LP,-(SP)          * store LP
      move.l   _AbsExecBase,LP
      move.l   LP,_SysBase       * set up SysBase

      OPENBASE DNAM,_DOSBase
      beq      DERR              * if DOS didn't open

      OPENBASE GNAM,_GfxBase
      beq.s    GERR              * if Graphics didn't open

      OPENBASE INAM,_IntuitionBase
      beq.s    IERR              * if Intuition ain't there

OK    move.l   (SP)+,LP          * restore LP

_Open moveq    #0,d0             * everything's cool flag
      rts

_Expunge
      move.l   LP,-(SP)          * store LP 
      suba.l   LP,LP             * clear register

      move.l   _IntuitionBase,a1
      LINKLIB  _LVOCloseLibrary,_SysBase

IERR  move.l   _GfxBase,a1
      LINKLIB  _LVOCloseLibrary,_SysBase

GERR  move.l   _DOSBase,a1
      LINKLIB  _LVOCloseLibrary,_SysBase

      move.l   LP,d0             * if LP is clear,
      beq.s    OK                * it's Expunge & not error

DERR  moveq    #-1,d0
      move.l   (SP)+,LP
      rts

INAM  dc.b     'intuition.library',0
DNAM  dc.b     'dos.library',0
GNAM  dc.b     'graphics.library',0
      cnop     0,2

_PD            dc.l   0
_PED           dc.l   0
_SysBase       dc.l   0
_DOSBase       dc.l   0
_GfxBase       dc.l   0
_IntuitionBase dc.l   0

      end
