
version	macro
	dc.b	' v1.0 '
	endm

	include	exec/memory.i
	include	dos/dos.i
	include	dos/datetime.i
	include	libraries/dosxref.i
	include	libraries/execxref.i

	include	include/macros.i

	structure	datas,0
		aptr	dosbase
		aptr	stringbuffer
		label	datas_SIZEOF


stringbufferlen	equ	512

	section	code,code

	movea.l	a0,a5		; save the command line ptr.

	move.l	#datas_SIZEOF,d0
	move.l	#MEMF_ANY!MEMF_CLEAR,d1
	move.l	4,a6
	call	AllocMem
	move.l	d0,a4
	tst.l	d0
	beq	cleanup

	openlib	dos,0,cleanup

	move.l	#stringbufferlen,d0
	move.l	#MEMF_ANY!MEMF_CLEAR,d1
	call	AllocMem
	move.l	d0,stringbuffer(A4)
	beq	cleanup

	move.l	#datestamp,d1
	move.l	dosbase(a4),a6
	call	DateStamp

	cmp.l	#"boot",(A5)
	beq	setboottime
	bsr	displayuptime
	bra	cleanup
setboottime
	bsr	initializeboottime

;	************************************************************

cleanup	move.l	4,a6
	closlib	dos

	move.l	stringbuffer(a4),d0
	beq	_100
	move.l	d0,a1
	move.l	#stringbufferlen,d0
	call	FreeMem

_100	move.l	a4,d0
	beq	_999
	move.l	d0,a1
	move.l	#datas_SIZEOF,d0
	call	FreeMem

_999	moveq	#0,d0
	rts

filename	dc.b	'ENV:UpTime.log',0
dosname		dc.b	'dos.library',0
		even

datestamp	ds.l	3
datestamp2	ds.l	3

str	dc.b	' The system has been up without reboots:',10
	dc.b	' %ld days, %ld hours, %ld minutes and %ld seconds.',10,0
	dc.b	'$VER: Uptime'
	version
	dc.b	'- Jarkko Vatjus-Anttila <quaid@kempele.fi>',0
	even
proc	move.b	d0,(a3)+
	rts

;	**************************************************************

initializeboottime
	move.l	#filename,d1		; just write the current time
	move.l	#MODE_NEWFILE,d2	; into s:Uptime.log
	move.l	dosbase(A4),a6
	call	Open
	move.l	d0,d7
	beq	.error
	move.l	d0,d1
	move.l	#datestamp,d2
	move.l	#12,d3
	call	Write
	move.l	d7,d1
	call	Close
.error	rts

;	****************************************************************

displayuptime
	move.l	#filename,d1
	move.l	#MODE_OLDFILE,d2	; read the clock values stored
	move.l	dosbase(A4),a6		; in the last reboot and
	call	Open			; compare the values to the
	move.l	d0,d7			; current ones.
	beq	error2
	move.l	d0,d1
	move.l	#datestamp2,d2
	move.l	#12,d3
	call	Read
	move.l	d7,d1
	call	Close

	lea	datestamp2(pc),a0	; original time
	lea	datestamp(pc),a1	; current time

	move.l	8(a0),d0		; NOF tics.
	move.l	8(a1),d1
	sub.l	d0,d1
	bpl	minutenotchanged
	add.l	#60*50,d1
	sub.l	#1,4(a1)
minutenotchanged
	divu.w	#50,d1
	and.l	#$ffff,d1
	move.l	d1,-(sp)	; seconds	

	move.l	4(a0),d0
	move.l	4(a1),d1
	sub.l	d0,d1
	bpl	daynotchanged
;	move.l	4(a1),d1
	add.l	#1440,d1		; 1440 mins = 24 hrs = 1 day
;	sub.l	d0,d1
	sub.l	#1,(a1)
daynotchanged
	move.l	d1,d3
	divu.w	#60,d1
;	and.l	#$ffff,d1
	moveq	#0,d2
	move.w	d1,d2		; hours
;	move.l	d0,-(sp)	; hours
	mulu.w	#60,d1
	sub.l	d1,d3		; minutes
	move.l	(a0),d0
	move.l	(a1),d1
	sub.l	d0,d1		; days

	move.l	d3,-(sp)
	move.l	d2,-(sp)
	move.l	d1,-(sp)
	lea	str(pc),a0
	move.l	sp,a1
	lea	proc(pc),a2
	move.l	stringbuffer(a4),a3
	move.l	4,a6
	call	RawDoFmt		; format the information string
	add.l	#16,sp

	move.l	dosbase(a4),a6
	call	Output
	move.l	d0,d1
	beq	error2
	move.l	stringbuffer(A4),d2
	move.l	d2,a0			; and display it in the cli. If
	moveq	#0,d3			; the program was started from
lp	addq.l	#1,d3			; shell.
	tst.b	(a0)+
	bne	lp
	subq.l	#1,d3
	call	Write
error2	rts

	end

