	objfile	'PProtect'
	macfile	'dlg/dlg.i'
	macfile	'dlg/user.i'
	macfile	'dlg/input.i'

	comment	®

	 Register usage

	A7 = SP				D7 = RDArgs
	A6 = Dosbase			D6 = Old Flags
	A5 = Data Pointer		D5 = Password Strleng + Log FH
	A4 = DLGBase			D4 = 
	A3 = 				D3 = 
	A2 = 
	Rest are scratch

	®

	movea.l	(4).w,a6
	CLEARDT					;Clear Data Area
	CHECKVERSION37				;Make sure WB2.0
	suba.l	a4,a4				;No DLGLib here
	OPENDOS					;Open dos.library
	movea.l	d0,a6
	READARGS				;Parse the Command line
	move.l	d0,d7
	bne.b	.argok	
	lea	(Usage,pc),a0
	move.l	a0,d1
	jsr	(_LVOPutStr,a6)
	bra	.noArgs

.argok	OPENDLGLIB				;Open DLG.library
	tst.l	d0
	beq	.noDlg
	movea.l	d0,a4				;Copy DLGBase
	lea	(port,pc),a0			;Room for our string
	exg	a4,a6				;DLGbase
	jsr	(_LVOGetDevName,a6)		;Get port name
	exg	a4,a6				;Dosbase back
	tst.l	d0
	beq.b	.portok
	lea	(Noport,pc),a0
	move.l	a0,d1
	jsr	(_LVOPutStr,a6)			;Print out error
	bra	.noport
.portok	lea	(RamFile,pc),a0			;Ram file
	lea	(UserData,pc),a1		;The User Struct
	lea	(port,pc),a2
	exg	a4,a6
	jsr	(_LVOReadUser,a6)
	exg	a4,a6
	tst.l	d0
	bne.b	.uok
	lea	(NoUser,pc),a0
	move.l	a0,d1
	jsr	(_LVOPutStr,a6)
	bra	.noport
.uok	lea	(port,pc),a0
	exg	a4,a6
	jsr	(_LVOBCPend,a6)
	lea	(port,pc),a0
	push	a0
	moveq	#0,d0
	jsr	(_LVOTSetFlags,a6)		;Get old flags
	move.l	d0,d6
	movea.l	(sp),a0
	moveq	#4,d0
	jsr	(_LVOTSetFlags,a6)		;Set Raw mode
	pop	a0
	moveq	#1,d0
	jsr	(_LVOTUnSetFlags,a6)
	exg	a4,a6
	lea	(MyQuery,pc),a0
	lea	(UInfo,pc),a1
	lea	(UserData,pc),a2
	move.l	a2,(ui_User,a1)
	lea	(RamFile,pc),a2
	move.l	a2,(ui_Ram,a1)
	lea	(prompt,pc),a2
	move.l	a2,(qu_prompt,a0)
	lea	(NULL,pc),a2
	move.l	a2,(qu_template,a0)
	move.l	a2,(qu_defstring,a0)
	lea	(passwrd,pc),a2
	move.l	a2,(qu_string,a0)
	lea	(valid,pc),a2
	move.l	a2,(qu_valid,a0)
	moveq	#100,d0
	move.w	d0,(qu_length,a0)
	move.w	d0,(qu_typelength,a0)
	moveq	#QUERY_HIDDEN|QUERY_NODEFEDT,d0
	move.l	d0,(qu_flags,a0)
	exg	a4,a6
	jsr	(_LVODLGQuery,a6)	
	btst	#0,d6				;Check if Echo On
	beq.b	.noecho
	lea	(port,pc),a0
	moveq	#1,d0
	jsr	(_LVOTSetFlags,a6)
.noecho	btst	#2,d6				;Check if Raw mode
	bne.b	.raw
	lea	(port,pc),a0
	moveq	#4,d0
	jsr	(_LVOTUnSetFlags,a6)		;Restore CLI mode
.raw	lea	(port,pc),a0
	jsr	(_LVOBCResume,a6)
	exg	a4,a6
	lea	(MyLF,pc),a0
	move.l	a0,d1
	jsr	(_LVOPutStr,a6)
	lea	(args,pc),a0
	movea.l	(4,a0),a0			;get Password
	push	a0
	bsr	Strlen				;Get length
	move.l	d0,d5
	lea	(passwrd,pc),a0
	push	a0
	bsr	Strlen
	popm	a0/a1				:get back strings
	cmp.l	d0,d5				;see if same
	bne.b	.pwerr				;no then error
.lp1	move.b	(a0)+,d0
	beq	.ok				;end of string then passed
	move.b	(a1)+,d1
	cmp.b	d0,d1
	beq.b	.lp1
.pwerr	lea	(errw,pc),a0
	move.l	a0,d1
	jsr	(_LVOPutStr,a6)
	lea	(lname,pc),a0
	move.l	a0,d1
	move.l	#MODE_READWRITE,d2
	jsr	(_LVOOpen,a6)			;Open log file
	move.l	d0,d5				;Copy FH
	beq.b	.nof
	move.l	d5,d1
	moveq	#0,d2
	moveq	#OFFSET_END,d3
	jsr	(_LVOSeek,a6)

	lea	(Date,pc),a0
	exg	a4,a6
	jsr	(_LVOMDate,a6)			;Get date
	exg	a4,a6
	lea	(args,pc),a0
	move.l	(a0),d0
	push	d0				;Save MenuItem
	lea	(RamFile,pc),a0
	push	a0				;Save name
	lea	(Date,pc),a0
	push	a0				;Save date
	move.l	d5,d1
	lea	(Log,pc),a0
	move.l	a0,d2
	move.l	sp,d3
	jsr	(_LVOVFPrintf,a6)
	lea	(3*4,sp),sp
	move.l	d5,d1
	jsr	(_LVOClose,a6)


.nof	exg	a4,a6
	lea	(port,pc),a0
	jsr	(_LVOTKill,a6)
	exg	a4,a6
	bra.b	.noport

.ok	exg	a4,a6
	bsr.b	Clear
	exg	a4,a6
.noport	movea.l	a4,a1
	push	a6
	movea.l	(4).w,a6
	jsr	(_LVOCloseLibrary,a6)
	pop	a6
.noDlg	move.l	d7,d1
	beq.b	.noArgs
	jsr	(_LVOFreeArgs,a6)
.noArgs	movea.l	a6,a1
	movea.l	(4).w,a6
	jsr	(_LVOCloseLibrary,a6)
	rts

Clear	moveq	#0,d0
	lea	(UserData,pc),a0
	move.b	(ud_Ansi_Flag,a0),d0
	jsr	(_LVOClr,a6)
	rts

Strlen	moveq	#0,d0
.lp1	move.b	(a0)+,d1
	beq.b	.end
	addq.l	#1,d0
	bra.b	.lp1
.end	rts



template
	db	'M=MENUITEM/A,P=PASSWORD/A',0
Noport	db	'Could not determine port, Aborting...',10,0
NoUser	db	'Could not get User Info, Aborting...',10,0
prompt	db	'Please Enter Password: ',0
errw	db	'Sorry Wrong Password, Logging Details and Bye Bye....',10,0
Log	db	'%s: %s, Failed Password for %s',10,0
dosname	db	'dos.library',0
dlgname	db	'dlg.library',0
lname	db	'logs:PProtect.log',0
Usage	db	'Usage: PProtect M=MENUITEM P=PASSWORD',10
	db	'        Where MenuItem is logged when incorrect PassWord'
MyLF	db	10,0
valid	db	'abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=,./?'
	db	'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' `',13,10,8,0
NULL	db	0

	quad
DT
args	dx.l	1
	dx.l	1
Errcode	dx.l	1
UserData dx.b	ud_Sizeof
RamFile	dx.b	ri_Sizeof
UInfo 	dx.b	ui_SIZEOF
MyQuery	dx.b	qu_Sizeof
port	dx.b	8
passwrd	dx.b	200
Date	dx.b	30
DTEND
	end
