/*Released as public domain 28-Jan-1996 by the author Dave Dustin*/

/*
 * Update English V1.09 (Shell version)
 *
 * ©1994 David Dustin / Eclipse Software
 *
 *
 * Designed to allow updating your Excelsior! LANGUAGES/ENGLISH file
 * a lot easier, by pointing out what the changes are
 *
 * History:
 *	1.0		First release
 *  1.01    Modified to handle long lines
 *  1.02    Changed
 *  1.03    Removed keyfile
 *  1.04    Cleaned up text output
 *  1.05    ARGH.  1.04 didn't work AT all
 *  1.06    Fixes tiny bug with final line of output
 *  1.07    Added STRIPIPL command, to filter colour code changes in comparisions
 *  1.08    Fixed error with lines being corrupted
 *  1.09    Now correctly adds lines that were filtered using STRIPIPL
 *
 */

DEF myargs:PTR TO LONG,rdargs
DEF oldfilename[120]:STRING,newfilename[120]:STRING,savefilename[120]:STRING,oldline2[1024]:STRING,newline2[1024]:STRING
DEF oldline[1024]:STRING,newline[1024]:STRING,templine[1024]:STRING,tempstr[2]:STRING,plural[2]:STRING
DEF linecount=0,oldfilehandle=0,newfilehandle=0,savefilehandle=0,ok=0,len=0,adl=1,changes=0,killsp=1,strip=0

PROC main()
	myargs:=[0,0,0,0]
	IF rdargs:=ReadArgs('OLDFILE/A,NEWFILE/A,TO/K,S=STRIPIPL/S',myargs,NIL)
		StrCopy(oldfilename,myargs[0],ALL)
		StrCopy(newfilename,myargs[1],ALL)
		StrCopy(savefilename,myargs[2],ALL)
		strip:=myargs[3]
		FreeArgs(rdargs)

		WriteF('[1m\s[0m\n\n',{version}+6)
		IF StrCmp(oldfilename,newfilename,ALL)=TRUE
			WriteF('Both filenames are identical\n\n')
			CleanUp(10)
		ENDIF

		IF (oldfilehandle:=Open(oldfilename,OLDFILE))=0
			WriteF('Can\at open \s as the OLDFILE\n\n',oldfilename)
			CleanUp(10)
		ENDIF

		IF (newfilehandle:=Open(newfilename,OLDFILE))=0
			WriteF('Can\at open \s as the NEWFILE\n\n',newfilename)
			Close(oldfilehandle)
			CleanUp(10)
		ENDIF

		IF savefilename[0]<>0
			IF (savefilehandle:=Open(savefilename,NEWFILE))=0
				WriteF('[0mUnable to open \s as TO file\nReverting to displaying differences\n\n',savefilename)
			ENDIF
		ENDIF

		REPEAT
			ReadStr(oldfilehandle,oldline)
			IF (ok:=ReadStr(newfilehandle,newline))<>0 THEN JUMP break
				IF adl=1
					WriteF('[0mLine [1m')
					adl:=0
				ENDIF
				INC linecount
				WriteF('\d',linecount)

				IF linecount>9 THEN killsp:=2;IF linecount>99 THEN killsp:=3
				IF linecount>999 THEN killsp:=4;IF linecount>9999 THEN killsp:=5

				IF oldline[0]=0 THEN JUMP addnew
				
				IF (StrCmp(oldline,newline,ALL))=FALSE
					IF strip=0 THEN JUMP isbad

					MOVE.L	oldline,A0
					MOVE.L	oldline2,A1
					copynoipl()

					MOVE.L	newline,A0
					MOVE.L	newline2,A1
					copynoipl()

					IF (StrCmp(oldline2,newline2,ALL))<>FALSE
						IF savefilehandle<>0
							len:=EstrLen(oldline)
							Write(savefilehandle,oldline,len)
							Write(savefilehandle,'\n',1)
						ENDIF
						JUMP nobad
					ENDIF

isbad:					INC changes
					WriteF('\n[1m1>[0m \s\n',oldline)
						WriteF('[1m2>[0m \s\n\n',newline)
						IF savefilehandle<>0
							WriteF('Use line [1m1[0m,2 or e >')
							ReadStr(stdout,tempstr)
							IF tempstr[0]=50
								len:=EstrLen(newline)
								Write(savefilehandle,newline,len)
							ELSEIF (tempstr[0]=69) OR (tempstr[0]=101)
								WriteF('Enter new line: ')
								ReadStr(stdout,templine)
								len:=EstrLen(templine)
								Write(savefilehandle,templine,len)
							ELSE
								len:=EstrLen(oldline)
								Write(savefilehandle,oldline,len)
								DEC changes
							ENDIF
								Write(savefilehandle,'\n',1)
						ENDIF
					adl:=1
				ELSE
addnew:
						IF savefilehandle<>0
							len:=EstrLen(newline)
							Write(savefilehandle,newline,len)
							Write(savefilehandle,'\n',1)
						ENDIF
	
						IF oldline[0]=0
						INC changes
							IF savefilehandle<>0
								WriteF('[0m appended\n')
							ELSE
								WriteF('[0m is new\n\s\n\n',newline)
							ENDIF
							adl:=1
						ENDIF
	
nobad:
						IF adl=0 THEN WriteF('[\dD',killsp)
				ENDIF
			IF CtrlC()=TRUE THEN WriteF('\n...Break') BUT ok:=-1
break:
		UNTIL ok<>0
		IF changes<>1 THEN plural:='s'
		WriteF('\n[1m\d[0m change\s noticed',changes,plural)
		IF savefilehandle<>0 THEN Close(savefilehandle) BUT WriteF(' and recorded')
		WriteF('\n\n')
		Close(newfilehandle)
		Close(oldfilehandle)
	ELSE
		WriteF('[1m\s[0m\n\nFor more information, please read the manual.\n\n',{version}+6)
	ENDIF
ENDPROC

PROC copynoipl()
copyloop:
	MOVE.B	(A0)+,D0
	CMP.B	#0,D0
	BEQ		copydone
	CMP.B	#10,D0
	BEQ		copydone

	CMP.B	#"\\",D0
	BNE		copyit

	MOVE.B	(A0)+,D1
	CMP.B	#"a",D1
	BLT		exit
	CMP.B	#"z",D1
	BGT		exit
	SUB.B	#32,D1
exit:

	CMP.B	#"C",D1
	BEQ		iscol
	CMP.B	#"Z",D1
	BEQ		iscol
	CMP.B	#"O",D1
	BEQ		iscol
	CMP.B	#"R",D1
	BEQ		iscol
	CMP.B	#"U",D1
	BEQ		iscol
	CMP.B	#"Q",D1
	BEQ		iscol
	MOVE.B	D0,(A1)+
	MOVE.B	D1,(A1)+
	BRA		copyloop

iscol:
	ADDQ.L	#1,A0
	BRA		copyloop
copyit:
	MOVE.B	D0,(A1)+
	BRA		copyloop
copydone:
	MOVE.B	#0,(A1)
ENDPROC

version:
CHAR '$VER: UpdateEnglish 1.09 (13.2.94)  (c) David Dustin / Eclipse Software\0'
