   include "exec/types.i"
   include "exec/initializers.i"
   include "exec/resident.i"
   include "exec/libraries.i"
   include "exec/lists.i"
   include "libraries/dos.i"

   STRUCTURE KickBase,LIB_SIZE
      BPTR kc_SegList
      APTR kc_IntuitionBase
      LABEL   kc_SIZEOF

   xref _GetWindow         ; eigene Funktionen
   xref _RemWindow
   xref _LVOOpenLibrary
   xref _LVOCloseLibrary
   xref _LVORemove
   xref _LVOFreeMem

   global _SysBase,4
   global _IntuitionBase,4
   global _KickBase,4

   CSEG

   moveq #0,d0
   rts

residentStructure:
   dc.w  RTC_MATCHWORD       ; Resident-Erkennungscode
   dc.l  residentStructure   ; Redident-Verify-Zeiger
   dc.l  End_of_ResidentCode ; Ab da wieder neue Residents
   dc.b  RTF_AUTOINIT        ; Automat. Initialisierung
   dc.b  1                   ; Library Version
   dc.b  NT_LIBRARY          ; Node-Type
   dc.b  0                   ; Priorität
   dc.l   libraryName           ; Zeiger auf   Lib-Namen
   dc.l  IDString            ; kleiner Info-String
   dc.l  InitList            ; Weitere Infos...

libraryName: dc.b   "kick.library",0
             cnop 0,2
IDString:    dc.b "kick 1.00 (26 Apr 90)",13,10,0
             cnop   0,2
intName:     dc.b   "intuition.library",0
             cnop   0,2

InitList:
   dc.l  kc_SIZEOF           ; Größe der Base
   dc.l  funcTable           ; Liste der Funktionen
   dc.l  dataTable           ; Anweisungen zum Aufbau der  Base
   dc.l  initLib             ; Init-Routine

funcTable:
   dc.l  .Open               ; Öffnen der Lib: neue Applik.
   dc.l  .Close              ; Schließen: Abmeldung einer ^
   dc.l  .Expunge            ; Lib aus dem Speicher entf.
   dc.l  .ExtFunc            ; Offen für Erweiterungen
   dc.l  .GetWindow          ; Eigene Funktionen
   dc.l  .RemWindow
   dc.l  -1                  ; Ende der Liste

dataTable:
   INITBYTE    LH_TYPE,NT_LIBRARY
   INITLONG    LN_NAME,libraryName
   INITBYTE    LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
   INITWORD    LIB_VERSION,1
   INITWORD    LIB_REVISION,0
   INITLONG    LIB_IDSTRING,IDString
   dc.l 0


initLib:
   move.l  a5,-(sp)
   move.l  d0,_KickBase
   move.l  d0,a5
   move.l  a0,kc_SegList(a5)
   move.l  a6,_SysBase
   lea     intName(pc),a1
   moveq   #0,d0
   jsr     _LVOOpenLibrary(a6)
   move.l  d0,_IntuitionBase
   move.l  d0,kc_IntuitionBase(a5)
   beq     erropen
   move.l  a5,d0
erropen:
   move.l  (sp)+,a5
   rts


.Open:
   addq.w  #1,LIB_OPENCNT(a6)
   bclr    #LIBB_DELEXP,LIB_FLAGS(a6)
   move.l  a6,d0
   rts


.Close:
   moveq   #0,d0
   subq.w  #1,LIB_OPENCNT(a6)
   bne     used
   btst    #LIBB_DELEXP,LIB_FLAGS(a6)
   beq     used
   bsr     .Expunge
used:
   rts


.Expunge:
   movem.l a5/a6,-(sp)
   tst.w   LIB_OPENCNT(a6)
   beq     ok
   bset    #LIBB_DELEXP,LIB_FLAGS(a6)
   moveq   #0,d0
   bra.s   endexpunge
ok:
   move.l  a6,a5                   ; KickBase -> A5
   move.l  4,a6
   move.l  kc_IntuitionBase(a5),a1 ; Intuition schließen
   jsr     _LVOCloseLibrary(a6)
   move.l  a5,a1                   ; Library-Node entfernen
   jsr     _LVORemove(a6)
   move.l  kc_SegList(a5),-(sp)    ; Segment-Liste merken
   move.l  a5,a1
   moveq   #0,d0
   move.w  LIB_NEGSIZE(a5),d0      ; SegList-NegSize=
   suba.l  d0,a1                   ; Memory-Start
   add.w   LIB_POSSIZE(a5),d0      ; NegSize+PosSize=
   jsr     _LVOFreeMem(a6)         ; Memory-Size
   move.l  (sp)+,d0                ; Segment-Liste holen
endexpunge:
   movem.l (sp)+,a5/a6
   rts

.ExtFunc:
   moveq     #0,d0
   rts

.GetWindow:
   movem.l d3-d4,-(sp)        ; Register retten
   movem.l a0/d0-d4,-(sp)     ; Parameter auf Stack
   jsr     _GetWindow(pc)     ; Aufruf
   lea     24(sp),sp          ; Stack restaurieren
   movem.l (sp)+,d3-d4        ; Register holen
   rts


.RemWindow:
   move.l  a0,-(sp)           ; Parameter
   jsr     _RemWindow(pc)
   addq.l  #4,sp
   rts

End_of_ResidentCode:
   end

