title   savstamp.asm for DateStamper systems
;
; Name:		SAVSTAMP
; Author:	bridger mitchell (Plu*Perfect Systems)
; Date:		1/16/88
; Version:	0.2
; Requirements:	ZCPR3 external environment
;
; This utility saves the DateStamper(tm) create datestamp of
; the specified file and (later) replaces the create datestamp
; of a specified file with the saved stamp.  It can be used
; to maintain the original create date for a file that is
; repeatedly modified.
;
; SAVSTAMP uses the last 5 bytes of the user area of the
; ZCPR3 message buffer.
;
; Linkage: 	SAVSTAMP.COM <- SAVSTAMP,Z3LIB,SYSLIB,DSLIB
;	A high-memory type-3 environment version can be
;	created by linking to an origin of, e.g., 8000h.
;
; DateStamper is Copyright (c) 1985-1987 by Plu*Perfect Systems.
;
; Patterned after Carson Wilson's similar utility for
; z80dos v. 1 style timestamps.
;
;
VERSION equ 02
;
msgOFF	equ	22h		;offset to z3msg addr in env.
userOFF	equ	40h		;offset in z3msg to user bytes
;
CR	equ	0dh
LF	equ	0ah
SPACE	equ	20h

bdos	equ	5
fcb1	equ	5ch
user1	equ	fcb1 + 13
fcb2	equ	6ch
;
EXTRN	whrenv,putud,getud
EXTRN	findck,cktdf,fstnxt,gettd,settd	;DSLIB routines

	CSEG

origin:
	jp	start
	db	'Z3ENV'
	db	3		;type 3
;
envaddr:dw	0FE00h		;any non-zero addr
	dw	origin
;
start:	ld	(savesp),sp
	ld	sp,stack
	call	putud		;save current du
	ld	a,(fcb1+1)
	cp	SPACE
	jp	z,help
	cp	'/'
	jr	nz,start1
	ld	a,(fcb1+2)
	cp	'/'
	jp	z,help
;
start1:	ld	hl,(envaddr)	;is this a z system?
	call	whrenv
	ld	a,h
	or	l
	jp	z,noenv		;..no
	ld	(envaddr),hl
	call	findck		;is DateStamper running?
	jp	z,nods		;..no
;
startok:ld	a,(user1)	;log into file's DU:
	ld	e,a
	ld	c,20h
	call	bdos
	ld	a,(fcb1)
	or	a
	jr	z,skip
	dec	a
	ld	e,a		;select drive
	xor	a
	ld	(fcb1),a
	ld	c,0eh
	call	bdos
;
skip:	call	cktdf		;is datestamp file on drive?
	jp	z,nodsfile	;..no	
;
	ld	c,17		;is file on disk?
	ld	de,fcb1
	call	fstnxt
	jp	c,nofile	;..no	
;
	ld	a,(fcb2+1)	;set-stamp requested?
	cp	SPACE
	jr	nz,doset	;..yes
;
; Get file's Datestamp.
;
doget:	ld	a,b		;no, get file's datestamp
	ld	hl,tdbuf
	call	gettd
	jp	z,tderr
	call	getbuf		;get ptr to hi ram buffer to hold
	ex	de,hl		; the create datestamp
	ld	hl,tdbuf	; move it to secure storage
	ld	bc,5
	ldir
	jp	xit
;
;
; Set File's Datestamp from saved stamp
;
doset:	ld	hl,tdbuf
	push	bc		;save index
	push	de		;save record number
	ld	a,b
	call	gettd		;get this file's stamp to local tdbuf
	pop	de
	pop	bc
	jp	z,tderr
	push	bc		;save index
	push	de		;save record number
	call	getbuf		;set hl-> hi-ram buffer
	ld	de,tdbuf	;retrieve original create date
	push	de		
	ld	bc,5		;   into buffer
	ldir
	pop	hl		;-> 15-byte buffer
	pop	de
	pop	bc
	ld	a,b
	call	settd		; set datestamp with orig. create date
	jp	z,settderr
;
xit:	call	getud		;restore DU:
	ld	sp,(savesp)
	ret
;
; return hl = addr of 5-byte buffer in protected memory
;
getbuf:	ld	hl,(envaddr)
	ld	de,msgOFF
	add	hl,de
	ld	a,(hl)
	inc	hl
	ld	h,(hl)
	ld	l,a
	or	h
	jp	z,nobuf
	ld	de,userOFF+10h-5	;use last 5 bytes of user area
	add	hl,de
	ret
	
help:	ld	de,helpmsg
	jp	errxit
;
noenv:	ld	de,msg0
	jr	errxit
;
nods:	ld	de,msg1
	jr	errxit
;
nofile:	ld	de,msg2
	jr	errxit
;
nodsfile:
	ld	de,msg3
	jr	errxit

tderr:	ld	de,msg4
	jr	errxit
settderr:
	ld	de,msg5
	jr	errxit
;
nobuf:	ld	de,msg6
;
errxit:	ld	c,9
	call	bdos
	jp	xit
;
msg0:	db	CR,LF,'Not ZCPR3$'
msg1:	db	CR,LF,'No DateStamper$'
msg2:	db	CR,LF,'File not found$'
msg3:	db	CR,LF,'Not a DateStamper disk$'
msg4:	db	CR,LF,'Error accessing time/date file$'
msg5:	db	CR,LF,'Error updating time/date file$'
msg6:	db	CR,LF,'No z3msg buffer$'
;
helpmsg
 db CR,LF
 db 'SAVSTAMP v. '
 db (VERSION/10) + '0','.',(VERSION MOD 10) + '0'
 db ' (DateStamper tool)'
 db CR,LF
 db 'usage:  SAVESTAMP [du:]filename.typ   -- save create stamp'
 db CR,LF
 db '        SAVESTAMP [du:]filename.typ X -- set create date from saved stamp'
 db '$'	

	DSEG

tdbuf:	ds	15
;
savesp:	ds	2
	ds	36
stack:	ds	0
	end
ld	de,userOFF+10h-5	;use last 5 bytes of user area
	add	hl,de
	ret
	
help:	ld	de,helpmsg
	jp	errxit
;
no