
version	macro
	dc.b	' 1.0 '
	endm

	include	exec/memory.i
	include	dos/dos.i

	include	own/jvamacros.i
	include	libraries/execxref.i
	include	libraries/dosxref.i



	structure datas,0
		aptr	dosbase

		aptr	inputfileptr
		aptr	outputfileptr

		aptr	inputfilename
		aptr	outputfilename

		aptr	buffer
		long	blocksize

		label	datas_SIZEOF


	movea.l	a0,a5			; put NULL in the end of command
	clr.b	-1(a0,d0)		; line

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

	openlib	dos,0,cleanup

	lea	title(pc),a0
	bsr	printclimsg

	move.l	a5,a0
	bsr	parsecommandline
	tst.l	d0
	beq	parse_noerror
	lea	error1(pc),a0
	bsr	printclimsg
	lea	error2(pc),a0
	bsr	printclimsg
	bra	cleanup

parse_noerror
	move.l	outputfilename(a4),a0		; add the .000 suffix to the
	lea	tempbuffer,a1			; end of the outputfilename.
split_copyloop
	move.b	(a0)+,(a1)+
	bne	split_copyloop
	move.b	#".",-1(a1)		; we could do move.l #".000",(a1)+
	move.b	#"0",(a1)+		; but that would crash the program
	move.b	#"0",(a1)+		; under 000/010.
	move.b	#"0",(a1)+
	move.b	#0,(a1)+
	move.l	#tempbuffer,outputfilename(A4)


	move.l	inputfilename(a4),d1
	move.l	#MODE_OLDFILE,d2
	move.l	dosbase(A4),a6
	call	Open			; open the inputfile
	move.l	d0,inputfileptr(a4)
	bne	split_cont0
	lea	error4(pc),a0
	bsr	printclimsg
	bra	cleanup

split_cont0
	move.l	blocksize(a4),d0
	move.l	#MEMF_ANY!MEMF_CLEAR,d1	
	move.l	4,a6
	call	AllocMem		; alloc mem for buffer.
	move.l	d0,buffer(a4)
	bne	split_loop
	lea	error3(pc),a0
	bsr	printclimsg
	bra	cleanup

split_loop
	move.l	inputfileptr(a4),d1
	move.l	buffer(a4),d2
	move.l	blocksize(a4),d3	; read a block
	move.l	dosbase(A4),a6
	call	Read
	move.l	d0,d5

	move.l	outputfilename(A4),d1
	move.l	#MODE_NEWFILE,d2	; open new outputfile
	call	Open
	move.l	d0,d6
	bne	split_cont1
	lea	error5(pc),a0
	bsr	printclimsg
	bra	cleanup

split_cont1
	move.l	d6,d1
	move.l	buffer(A4),d2		; write the block
	move.l	d5,d3
	call	Write

	move.l	d6,d1			; close output
	call	Close

	cmp.l	blocksize(a4),d5	; check if we need to continue.
	bmi	cleanup

	move.l	outputfilename(A4),a0
spl_lp1	tst.b	(a0)+
	bne	spl_lp1

	add.b	#1,-2(a0)		; increase suffix number.
	cmp.b	#"9"+1,-2(a0)
	bmi	split_loop
	move.b	#"0",-2(a0)
	add.b	#1,-3(a0)
	cmp.b	#"9"+1,-3(a0)
	bmi	split_loop
	move.b	#"0",-3(a0)
	add.b	#1,-4(a0)
	bra	split_loop

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

cleanup	move.l	a4,d0
	beq	_999

	move.l	dosbase(A4),a6
	move.l	outputfileptr(a4),d1
	beq	_100
	call	Close

_100	move.l	inputfileptr(a4),d1
	beq	_101
	call	Close

_101	move.l	4,a6
	closlib	dos

	move.l	buffer(A4),d0
	beq	_102
	move.l	d0,a1
	move.l	blocksize(a4),d0
	call	FreeMem

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

_999	moveq	#0,d0
	rts

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

parsecommandline
	cmp.b	#"-",(a0)			; parsecommandline takes
	bne	parse_findinputfilename		; care of the parameters.
	cmp.b	#"b",1(a0)
	bne	p_error

	adda.l	#2,a0
	move.l	a0,a1
p_lp1	tst.b	(a1)
	beq	p_error
	cmp.b	#" ",(a1)+
	bne	p_lp1
	move.l	a1,a0
	suba.l	#2,a1
;	bra	parse_findinputfilename

	moveq	#0,d1
	moveq	#1,d2
p_lp2	moveq	#0,d0
	move.b	(a1),d0
	cmp.b	#"b",d0
	beq	p_sizefound
	sub.l	#"0",d0
	bmi	p_error
	cmp.b	#10,d0
	bpl	p_error
	mulu.w	d2,d0
	add.l	d0,d1
	mulu.w	#10,d2
	suba.l	#1,a1
	bra	p_lp2

p_sizefound
	mulu.w	#1024,d1
	move.l	d1,blocksize(a4)


parse_findinputfilename
	cmp.b	#34,(a0)
	beq	parse_findquote
	move.l	a0,inputfilename(a4)
p_lp3	tst.b	(a0)
	beq	p_error
	cmp.b	#" ",(a0)+
	bne	p_lp3
	move.b	#0,-1(a0)
	suba.l	#1,a0
	bra	parse_findoutputfilename

parse_findquote
	adda.l	#1,a0
	move.l	a0,inputfilename(a4)
p_lp4	tst.b	(a0)
	beq	p_error
	cmp.b	#34,(a0)+
	bne	p_lp4
	move.b	#0,-1(a0)


	suba.l	#1,a0
parse_findoutputfilename			; ignore extra spaces
	adda.l	#1,a0				; between filenames.
	cmp.b	#" ",(a0)
	beq	parse_findoutputfilename

	cmp.b	#34,(a0)
	beq	parse_findquote2
	move.l	a0,outputfilename(a4)
p_lp5	tst.b	(a0)
	beq	parse_exit
	cmp.b	#" ",(a0)+
	bne	p_lp5
	move.b	#0,-1(a0)
	bra	parse_exit

parse_findquote2
	adda.l	#1,a0
	move.l	a0,outputfilename(a4)
p_lp6	tst.b	(a0)
	beq	p_error
	cmp.b	#34,(a0)+
	bne	p_lp6
	move.b	#0,-1(a0)


parse_exit
	tst.l	blocksize(a4)
	bne	parse_exit2
	move.l	#500*1024,blocksize(a4)
parse_exit2
	moveq	#0,d0
	rts

p_error	moveq	#-1,d0
	rts

pcp	move.b	d0,(a3)+
	rts

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

printclimsg
	push	all
	move.l	a0,a5

	move.l	dosbase(A4),a6
	call	Output
	move.l	d0,d1

	move.l	a5,a0
	moveq	#0,d3
pcm_lp1	addq.l	#1,d3
	tst.b	(a0)+
	bne	pcm_lp1
	subq.l	#1,d3
	move.l	a5,d2

	call	Write
	pull	all
	rts


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

dosname		dc.b	'dos.library',0
title		dc.b	'[1mSplit'
		version
		dc.b	'[0mby Jarkko Vartjus-Anttila <quaid@kempele.fi>',10,0
error1		dc.b	'USAGE: [-bBLOCKSIZE] inputfile outputfile',10,0
error2		dc.b	'** Parse error',10,0
error3		dc.b	'** Unable to alocate memory',10,0
error4		dc.b	'** Unable to open inputfile',10,0
error5		dc.b	'** Unable to open outputfile',10,0

	section	bss,bss

tempbuffer
	ds.b	512

