	title	Write Hand Man - Manager
	name	('WHMT')

; This is the DASMed version of the Write Hand Man Manager

_OS	macro
	rst	0
	endm

OS	equ	0000h
BDOS	equ	0005h
FCB	equ	005ch
DMA	equ	0080h

.string	equ	 9
.open	equ	15
.close	equ	16
.delete	equ	19
.wrseq	equ	21
.make	equ	22
.retdsk	equ	25
.frespc	equ	46
.RSX	equ	60

OSerr	equ	255

MyRSX	equ	122

lf	equ	0ah
cr	equ	0dh
esc	equ	1bh
$$act	equ	']'-'@'
eot	equ	'$'
$$cpy	equ	0a4h

LoMask	equ	00001111b

All	equ	11111111b

FreRec	equ	64
DmyRec	equ	56

Trigger:
	db	$$act,lf
SavDrv:
	ds	1
;
	ds	250
;
; >>>>> ENTRY <<<<<
;
MAIN:
	ld	a,(FCB)		; Get drive
	ld	(UsrDrv),a	; .. save
	or	a		; Test any
	jp	nz,GotDrv
	ld	c,.retdsk
	call	BDOS		; Get current disk
	inc	a
	ld	(UsrDrv),a
GotDrv:
	ld	a,(FCB+2)	; Test option
	cp	'N'		; .. oN
	jp	z,ON
	cp	'F'		; .. or oFf
	jp	z,OFF
	ld	de,$HELP
	ld	c,.string
	call	BDOS		; Give help
	jp	OS		; .. and exit
$HELP:
	db	lf,cr
	db	'Use:    WHMT  [d:]ON    turn on WHM '
	db	'with disk [d]'
	db	lf,cr
	db	'        WHMT      OFF   turn off WHM'
	db	lf,cr,eot
Engaged:
	ld	de,$ENGAGED
	ld	c,.string
	call	BDOS		; Tell RSX already here
	jp	OS
NoRAMspc:
	ld	de,$NO.SPC
	ld	c,.string
	call	BDOS		; Tell no space for file
	jp	OFF		; .. fall into OFF state
;
; ##### WHMT ON #####
;
ON:
	ld	c,.RSX
	ld	de,TestPB
	call	BDOS		; Test RSX
	and	LoMask
	cp	All AND LoMask
	jp	c,Engaged	; .. there is one RSX
	call	PrepFile	; Create data file
	inc	a
	jp	z,NoRAMspc	; .. not possible
	ld	de,InsPB
	ld	c,.RSX
	call	BDOS		; Install RSX
	ld	de,TestPB
	ld	c,.RSX
	call	BDOS		; Get version
	push	af		; .. save BCD byte
	rrca			; Extract high
	rrca
	rrca
	rrca
	and	LoMask
	or	'0'
	ld	($HI.VER),a
	pop	af
	and	LoMask		; Get low
	or	'0'
	ld	($LO.VER),a
	ld	de,$CLRSCR
	ld	c,.string
	call	BDOS		; Clear screen
	ld	de,$INSTALLED
	ld	c,.string
	call	BDOS		; Tell active
	_OS
;
; Prepare data file
; EXIT	Accu holds error code
;
PrepFile:
	ld	c,.frespc
	ld	e,'M'-'A'
	call	BDOS		; Get disk free space
	ld	hl,(DMA)	; Get it
	ld	de,FreRec-1
	and	a
	sbc	hl,de		; Test some records free
	jp	c,NoSpace	; .. nope
	ld	a,(FCB)		; Get drive
	ld	(SavDrv),a	; .. save
	ld	hl,$$FCB
	ld	de,FCB
	ld	bc,FCBlen
	ldir			; Copy file required
	ld	de,FCB
	ld	c,.delete
	call	BDOS		; Delete old file
	ld	hl,$$FCB
	ld	de,FCB
	ld	bc,FCBlen
	ldir
	ld	de,FCB
	ld	c,.make
	call	BDOS		; Try to make new one
	inc	a
	jp	z,NoSpace	; .. nope
	ld	de,FCB
	ld	c,.open
	call	BDOS		; Re-open it
	ld	b,DmyRec	; Set record count
	ld	c,.wrseq
	ld	de,FCB
WrRecs:
	push	bc
	push	de
	call	BDOS		; Write dummy records
	pop	de
	pop	bc
	or	a
	jp	nz,NoSpace	; .. no room
	djnz	WrRecs
	ld	c,.close
	call	BDOS		; Close file
	ld	a,(SavDrv)
	ld	(FCB),a
	xor	a		; Set success
	ret
NoSpace:
	ld	a,OSerr		; Set error
	ret
;
$$FCB:
	db	'M'-'A'+1,'WHMXWS  DAT',0
FCBlen	equ	$-$$FCB
;
$CLRSCR:
	db	esc,'H',esc,'E',eot
;
; ##### WHMT OFf #####
;
OFF:
	ld	de,TestPB
	ld	c,.RSX
	call	BDOS		; Test RSX
	and	LoMask
	cp	All AND LoMask
	jp	nc,NotEng	; .. there is no RSX
	ld	de,DelPB
	ld	c,.RSX
	call	BDOS		; Release RSX
	ld	de,$DELETE
	ld	c,.string
	call	BDOS		; .. tell done
	ld	hl,$$FCB
	ld	de,FCB
	ld	bc,FCBlen
	ldir
	ld	de,FCB
	ld	c,.delete
	call	BDOS		; Delete data file
	ret
NotEng:
	ld	de,$NOT.ENG
	ld	c,.string
	call	BDOS
	jp	OS
$NOT.ENG:
	db	'WHM not engaged',lf,cr,eot
$DELETE:
	db	'WHM disengaged',lf,cr,eot
;
	db	'Joyce version re-written by D.W.Clements 1986'
	db	eot
;
	ds	8
;
$ENGAGED:
	db	'WHM is already engaged',lf,cr,eot
$INSTALLED:
	db	'WHMT version '
$HI.VER:
	db	'5.'
$LO.VER:
	db	'0 Engaged. ',$$cpy,' 1985 '
	db	'Poor Person Software '
	db	cr,lf,lf
	db	'Distributed Exclusively in Europe by HiSoft'
	db	cr,lf,eot
$NO.SPC:
	db	'WHMT unable to find enough space on RAM disk'
	db	' please make room and retry',cr,lf,eot
TestPB:
	db	MyRSX
	db	1
	db	0
DelPB:
	db	MyRSX
	db	1
	db	2
InsPB:
	db	MyRSX
	db	13
	db	1
	db	0
UsrDrv:
	db	1
	db	0
	dw	Trigger
	dw	TOP
;
TOP:
	ds	128
	ds	1

	end	MAIN

