*
* Lemmings Installer
*
* (c) Brutal Deluxe, 1997
*

 lst off
 rel
 dsk Installer.l

*----------------------------------------
* MACROS
*----------------------------------------

 use 4/Ctl.Macs
 use 4/Event.Macs
 use 4/Int.Macs
 use 4/Locator.Macs
 use 4/Mem.Macs
 use 4/Menu.Macs
 use 4/Misc.Macs
 use 4/Qd.Macs
 use 4/QdAux.Macs
 use 4/Std.Macs
 use 4/Util.Macs
 use 4/Window.Macs

*----------------------------------------
* CONSTANTES
*----------------------------------------

proDOS = $e100a8

*----------------------------------------
* PROGRAMME
*----------------------------------------

 phk
 plb

 _TLStartUp
 pha
 _MMStartUp
 pla
 sta myID

*--- Version du systeme

 jsl proDOS
 dw $202a
 adrl proVERSION

 lda proVERSION+2
 and #$7fff
 cmp #$0400
 bcs okINIT1

 PushWord #0
 PushLong #verSTR1
 PushLong #verSTR2
 PushLong #errSTR1
 PushLong #errSTR2
 _TLTextMountVolume
 pla
 brl doQUIT2

*--- Chargement des outils

okINIT1 PushLong #0
 PushWord myID
 PushWord #2
 PushLong #1
 _StartUpTools
 PullLong SStopREC
 bcc okINIT2

 PushWord #0
 PushLong #tolSTR1
 PushLong #errSTR2
 PushLong #errSTR1
 PushLong #errSTR2
 _TLTextMountVolume
 pla
 brl doQUIT1

*--- Reservation memoire

okINIT2 pha
 pha
 PushLong #$10000
 PushWord myID
 PushWord #%11000000_00011100
 PushLong #0
 _NewHandle
 phd
 tsc
 tcd
 lda [3]
 sta proREAD+4
 ldy #2
 lda [3],y
 sta proREAD+6
 pld
 PullLong haREAD

*--- Affichage desktop

 _InitCursor
 _HideMenuBar

 PushWord #0
 PushWord #%11111111_11111111
 PushWord #0
 _FlushEvents
 pla

 jsr welcomeWINDOW

*----------------------------------------
* TASK MASTER
*----------------------------------------

taskLOOP PushWord #0
 PushWord #0
 PushWord #$c000
 PushWord #0
 _HandleDiskInsert
 pla
 pla

 PushWord #0
 PushWord #%11111111_11111111
 PushLong #taskREC
 _TaskMaster
 pla
 beq taskLOOP

 asl
 tax
 jsr (taskTBL,x)

 bra taskLOOP

*----------------------------------------
* GESTION DES CONTROLES
*----------------------------------------

doCONTROL lda taskREC+38
 asl
 tax
 jsr (ctrlTBL,x)
doNOT rts

*----------------------------------- Quit

doQUIT lda fgINSTALL
 bne doQUIT1

 PushWord #0
 PushWord #5
 PushLong #0
 PushLong #1
 _AlertWindow
 pla
 dec
 bne doQUIT1
 rts

doQUIT1 PushWord #1
 PushLong SStopREC
 _ShutDownTools

doQUIT2 PushWord myID
 _DisposeAll

 PushWord myID
 _MMShutDown
 _TLShutDown

 jsl proDOS
 dw $2029
 adrl proQUIT

*----------------------------------- Open

doINSTALL PushWord #120
 PushWord #40
 PushWord #2
 PushLong #$1000
 PushLong #0
 PushLong #fileLIST
 PushLong #replyPTR
 _SFGetFile2

 lda replyPTR
 bne doINSTALL1
 rts

doINSTALL1 jsr testNAME
 bcs doINSTALL
 jsr testPREFIX

 jsl PAINT01

 jsr thermoWINDOW

 lda #lemmDISK2
 ldx #3
 jsr handleCOPY
 bcs doINSTALL2

 lda #lemmDISK3
 ldx #4
 jsr handleCOPY
 bcs doINSTALL2

 inc fgINSTALL
 jsr shutWINDOW

 PushWord #0
 PushWord #5
 PushLong #0
 PushLong #2
 _AlertWindow
 pla
 rts

doINSTALL2 jsr shutWINDOW

 PushWord #0
 PushWord #0
 PushLong #0
 PushWord proERR
 _ErrorWindow
 pla
 rts

*--- Verifie que l'on a bien LemminGS

testNAME ldx lemmNAME2
]lp lda lemmNAME2,x
 cmp lemmREAL2,x
 bne testNAME1
 dex
 dex
 bne ]lp
 clc
 rts
testNAME1 sec
 rts

*--- Enleve LemminGS de la chaine du prefixe

testPREFIX lda pathNAME2
 sec
 sbc #8
 tay

 ldx #0

 sep #$20
]lp lda lemmLVL,x
 beq testPREFIX1
 sta pathNAME2+2,y
 inx
 iny
 bra ]lp

testPREFIX1 sty pathNAME2
 sty pathLENGTH

 rep #$20
 rts

*---

add2path sep #$20

 ldx #0
 ldy pathNAME2

]lp lda fileNAME2+2,x
 sta pathNAME2+2,y
 iny
 inx
 cpx fileNAME2
 bne ]lp

 sty pathNAME2

 rep #$20
 rts

*----------------------------------------
* ROUTINES DE COPIE
*----------------------------------------

handleCOPY jsr handlePREFIX
 bcc handleCOPY1
 rts

handleCOPY1 lda #1
 sta proGETDIRENTRY+6
 sta proGETDIRENTRY+8

]lp jsl proDOS
 dw $201c
 adrl proGETDIRENTRY
 bcs handleCOPY2

 lda proTYPE
 ora proEOF+2
 cmp #$0006 ; binary file < 64kb
 bne ]lp

 jsr infoWINDOW

 jsr handleREAD
 bcs handleCOPY3
 jsr handleWRITE
 bcs handleCOPY3

 lda pathLENGTH
 sta pathNAME2
 bra ]lp

handleCOPY2 stz proERR
 clc

handleCOPY3 php
 jsl proDOS
 dw $2014
 adrl proGETDIRCLOSE
 plp
 rts

*--- Wait for disk to be inserted

handlePREFIX sta proSETPREFIX+4
 sta proGETDIROPEN+4
 stx handlePREFIX1+4

 jsl proDOS
 dw $2009
 adrl proSETPREFIX

]lp jsl proDOS
 dw $2010
 adrl proGETDIROPEN
 bcc handlePREFIX2

 sta proERR

 PushWord #0
 PushWord #5
 PushLong #0
handlePREFIX1 PushLong #3
 _AlertWindow
 pla
 dec
 beq ]lp
 sec
 rts

handlePREFIX2 jsl PAINT01
 jsl PAINT02

 lda proGETDIROPEN+2
 sta proGETDIRENTRY+2
 sta proGETDIRCLOSE+2
 clc
 rts

*----------------------------------------
* LECTURE DES FICHIERS
*----------------------------------------

handleREAD lda #fileNAME2
 sta proOPEN+4

 jsl proDOS
 dw $2010
 adrl proOPEN
 bcs handleREAD1

 lda proOPEN+2
 sta proREAD+2
 sta proCLOSE+2

 lda proEOF
 sta proREAD+8

 jsl proDOS
 dw $2012
 adrl proREAD

 sta proERR
 php
 jsl proDOS
 dw $2014
 adrl proCLOSE
 plp
 rts

handleREAD1 sta proERR
 rts

*----------------------------------------
* ECRITURE DES FICHIERS
*----------------------------------------

handleWRITE jsr add2path

 lda proEOF
 sta proCREATE+16

 jsl proDOS
 dw $2001
 adrl proCREATE
 bcs handleWRITE1

 lda #pathNAME2
 sta proOPEN+4

 jsl proDOS
 dw $2010
 adrl proOPEN
 bcs handleWRITE1

 lda proOPEN+2
 sta proREAD+2
 sta proCLOSE+2

 jsl proDOS
 dw $2013 ; WRITE
 adrl proREAD

 sta proERR
 php
 jsl proDOS
 dw $2014
 adrl proCLOSE
 plp
 rts

handleWRITE1 sta proERR
 rts

*----------------------------------------
* ROUTINES DES FENETRES
*----------------------------------------

welcomeWINDOW PushLong #0
 PushLong #0
 PushLong #1
 PushLong #PAINT01
 PushLong #0
 PushWord #2
 PushLong #$0001
 PushWord #$800e
 _NewWindow2
 PullLong wiWELCOME
 rts

*---

thermoWINDOW PushLong #0
 PushLong #0
 PushLong #1
 PushLong #PAINT02
 PushLong #0
 PushWord #2
 PushLong #$0002
 PushWord #$800e
 _NewWindow2
 PullLong wiTHERMO

 PushLong #0
 PushLong wiTHERMO
 PushWord #0
 PushLong #ctlTHERMO
 _NewControl2
 PullLong haTHERMO

 PushLong #245 ; 245 fichiers
 PushLong haTHERMO
 _SetCtlTitle

 PushLong haTHERMO
 _DrawOneCtl

 stz nbFILE
 rts

*--- Print name of file

infoWINDOW lda fileNAME2
 xba
 sta fileNAME2

 PushLong wiTHERMO
 _SetPort

 PushWord #8
 PushWord #12
 _MoveTo

 PushLong #loadSTRING
 _DrawString

 PushLong #fileNAME3
 _DrawString

 lda fileNAME2
 xba
 sta fileNAME2

*---

 inc nbFILE

 PushWord nbFILE
 PushLong wiTHERMO
 PushLong #1
 _SetCtlValueByID
 rts

*---

shutWINDOW PushLong wiTHERMO
 _CloseWindow
 rts

*----------------------------------- Rafraichissement

PAINT01 PushLong wiWELCOME
 _DrawControls
 rtl

*---

PAINT02 PushLong wiTHERMO
 _DrawControls
 rtl

*----------------------------------------
* DONNEES DU PROGRAMME
*----------------------------------------

nbFILE ds 2
fgINSTALL ds 2

*----------------------------------- Fenetres

wiWELCOME ds 4
wiTHERMO ds 4

*----------------------------------- Control Manager

haTHERMO ds 4

ctlTHERMO dw 8
 adrl 1
 dw $0010
 dw $0008
 dw $001a
 dw $018a
 adrl $87ff0002
 dw $0001
 dw $1000
 ds 4
ctlTHERMO1 ds 2
ctlTHERMO2 ds 2

*----------------------------------- Memory manager

myID ds 2
SStopREC ds 4

haREAD ds 4

*----------------------------------- Tool locator

verSTR1 str 'System is too old'
verSTR2 str 'Requires System 6'
tolSTR1 str 'Error while loading tools'
errSTR1 str 'Quit'
errSTR2 str ''

*----------------------------------- Window manager

taskREC ds 2 ; wmWhat           +0
 ds 4 ; wmMessage        +2
 ds 4 ; wmWhen           +6
 ds 4 ; wmWhere          +10
 ds 2 ; wmModifiers      +14
 ds 4 ; wmTaskData       +16
 adrl $001fffff ; wmTaskMask       +20
 ds 4 ; wmLastClickTick  +24
 ds 2 ; wmClickCount     +28
 ds 4 ; wmTaskData2      +30
 ds 4 ; wmTaskData3      +34
 ds 4 ; wmTaskData4      +38
 ds 4 ; wmLastClickPt    +42

taskTBL da doNOT ; Null
 da doNOT ; mouseDownEvt
 da doNOT ; mouseUpEvt
 da doNOT ; keyDownEvt
 da doNOT
 da doNOT ; autoKeyEvt
 da doNOT ; updateEvt
 da doNOT
 da doNOT ; activateEvt
 da doNOT ; switchEvt
 da doNOT ; deskAccEvt
 da doNOT ; driverEvt
 da doNOT ; app1Evt
 da doNOT ; app2Evt
 da doNOT ; app3Evt
 da doNOT ; app4Evt
 da doNOT ; wInDesk
 da doNOT ; wInMenuBar
 da doNOT ; wCLickCalled
 da doNOT ; wInContent
 da doNOT ; wInDrag
 da doNOT ; wInGrow
 da doNOT ; wInGoAway
 da doNOT ; wInZoom
 da doNOT ; wInInfo
 da doNOT ; wInSpecial
 da doNOT ; wInDeskItem
 da doNOT ; wInFrame
 da doNOT ; wInactMenu
 da doNOT ; wInClosedNDA
 da doNOT ; wInCalledSysEdit
 da doNOT ; wInTrackZoom
 da doNOT ; wInHitFrame
 da doCONTROL ; wInControl
 da doNOT ; wInControlMenu

ctrlTBL da doNOT
 da doQUIT ; Quit button
 da doINSTALL ; Install button
 da doNOT ; Static Text

*----------------------------------- GS/OS

proERR ds 2

proCREATE dw 6
 adrl pathNAME2 ; Pathname
 dw $c3 ; RequestAccess
 dw $06 ; FileType
 ds 4 ; AuxType
 ds 2 ; StorageType
 ds 4 ; EOF

proSETPREFIX dw 2
 dw 0 ; PrefixNum
 adrl lemmDISK2 ; Pathname

proGETDIROPEN dw 2
 ds 2 ; Id
 adrl lemmDISK2 ; Pathname

proOPEN dw 2
 ds 2 ; Id
 adrl fileNAME2 ; Pathname

proREAD dw 4
 ds 2 ; Id
 ds 4 ; Where
 ds 4 ; Length
 ds 4 ; Read

proGETDIRCLOSE dw 1
 ds 2 ; Id

proCLOSE dw 1
 ds 2 ; Id

proGETDIRENTRY dw 8
 ds 2
 ds 2
 dw 1
 dw 1
 adrl fileNAME
 ds 2
proTYPE ds 2
proEOF ds 4

proQUIT dw 2
 ds 4 ; PathName
 ds 2 ; Flags

proVERSION dw 1
 ds 2 ; Version

*----------------------------------- Standard File

fileLIST dw $0001
 dw $8000
 dw $00b3
 adrl $db07

replyPTR ds 2 ; good
 ds 2 ; fileType
 ds 4 ; auxType
 dw 0 ; nameRefDesc
 adrl lemmNAME ; nameRef
 dw 0 ; pathRefDesc
 adrl pathNAME ; pathRef

*---

lemmNAME dw 16 ; LemminGS name
lemmNAME2 ds 14
lemmREAL2 strl 'LemminGS'

lemmLVL asc 'LemminGS.Data:LVL:'00

lemmDISK2 strl '/LemminGS.Disk2/LemminGS.Data/LVL'
lemmDISK3 strl '/LemminGS.Disk3/LemminGS.Data/LVL'

*---

fileNAME dw 128 ; Name of file
fileNAME2 ds 1
fileNAME3 ds 125

*---

pathLENGTH ds 2

pathNAME dw 512
pathNAME2 ds 510

*---

loadSTRING str 'Copying file : '
