'***********************  
'* COMPILER DIRECTIVES * 
'*********************** 
$APPTYPE CONSOLE
$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"

$OPTION ICON "c2icon.ico"

'************************** 
'* DECLARE SUBS/FUNCTIONS * 
'************************** 

'********************* 
'* PROGRAM CONSTANTS * 
'********************* 
TYPE TOldBound
	RealName AS STRING * 100
	BoundName AS STRING * 100
	BoundType AS INTEGER
	ExtraInfo AS INTEGER
	BoundFile AS STRING * 500
END TYPE

DIM Bindings(500) AS TOldBound

TYPE TOldOptions
	CommandColour AS LONG
	ReturnedColour AS LONG
	BackGroundImage AS STRING*500
	KeyModCTRL AS INTEGER
	KeyModALT AS INTEGER
	KeyModSHIFT AS INTEGER
	Key AS STRING*1
	DropPercentScreen AS INTEGER
	DropPixelsPerIteration AS INTEGER
	Font AS STRING*50
	CLIBackColour AS INTEGER
	CLIForeColour AS INTEGER
END TYPE

DIM Options AS TOldOptions

'******************** 
'* GLOBAL VARIABLES * 
'******************** 

'***************** 
'* FORM CREATION * 
'***************** 

'************* 
'* FUNCTIONS * 
'************* 

'******** 
'* SUBS * 
'******** 

'********************* 
'* MAIN PROGRAM CODE * 
'********************* 
'Just prompt, then open and convert a bindings file... 
PRINT "CfW1 to CfW2 bindings convertor"
PRINT "Written by Daniel Foote."
PRINT
PRINT "This program converts your old bindings file that went with CfW1"
PRINT "into the new CfW2 format. All your options are preserved, and"
PRINT "where the CfW2 has new options, the defaults are used."
PRINT "Please note that this operation is unable to be reversed, so make"
PRINT "a backup copy of the origional CfW file first."
PRINT

PRINT "Press any key to select the file..."
DIM K$ AS STRING
DO
	K$ = INKEY$
LOOP UNTIL K$ <> ""

DIM OpenDlg AS QOpenDialog
DIM File AS QFileStream

DIM Dummy AS INTEGER
DIM Dummy2 AS INTEGER
DIM Dummy$ AS STRING

OpenDlg.Caption = "Choose file to convert..."
OpenDlg.Filter = "CfW files (*.cfw)|*.cfw|All Files (*.*)|*.*"
OpenDlg.FilterIndex = 1
IF OpenDlg.Execute THEN
	'Load and convert... 
	PRINT 
	PRINT "Opening file..."
	File.Open(OpenDlg.FileName,fmOpenRead)
	PRINT "Reading file..."
	Dummy$ = File.ReadLine
	File.ReadUDT(Options)
	FOR Dummy = 1 TO 500
		IF File.EOF THEN
			Dummy2 = Dummy - 1
			EXIT FOR
		END IF
		File.ReadUDT(Bindings(Dummy))
	NEXT Dummy
	PRINT "Finished reading file, preparing to convert....
	File.Close
	KILL OpenDlg.FileName
	File.Open(OpenDlg.FileName,fmCreate)
	PRINT "Writing options back to file..."
	File.WriteLine("COMMANDMSGCOLOUR="+STR$(Options.CommandColour))
	File.WriteLine("RETURNEDMSGCOLOUR="+STR$(Options.ReturnedColour))
	File.WriteLine("CLIBACKCOLOUR="+STR$(Options.CLIBackColour))
	File.WriteLine("CLIFORECOLOUR="+STR$(Options.CLIForeColour))
	File.WriteLine("KEYMODCTRL="+STR$(Options.KeyModCTRL))	
	File.WriteLine("KEYMODALT="+STR$(Options.KeyModALT))	
	File.WriteLine("KEYMODSHIFT="+STR$(Options.KeyModSHIFT))	
	File.WriteLine("KEY="+RTRIM$(Options.Key))
	File.WriteLine("DROPPERCENTSCREEN="+STR$(Options.DropPercentScreen))	
	File.WriteLine("DROPPIXELSPERITERATION="+STR$(Options.DropPixelsPerIteration))
	File.WriteLine("FONT="+RTRIM$(Options.Font))
	PRINT "Options which are new in CfW2 are not written to file. They will"
	PRINT "be reset to the default next time the CfW2 is run. You should run"
	PRINT "CfW2 soon on this file to set all the options to file."
	PRINT "Writing bindings back to file..."
	IF Dummy2 >= 200 THEN
		PRINT "It would appear that you have more than 200 bindings. CfW2 only"
		PRINT "supports up to 200 bindings. Everything after the 200th binding"
		PRINT "will not be recorded."
	END IF
	FOR Dummy = 1 TO Dummy2
		File.WriteLine("BIND "+RTRIM$(Bindings(Dummy).BoundName)+" "+CHR$(34)+RTRIM$(Bindings(Dummy).RealName)+CHR$(34)+" "+STR$(Bindings(Dummy).BoundType)+" "+CHR$(34)+RTRIM$(Bindings(Dummy).BoundFile)+CHR$(34)+" "+STR$(Bindings(Dummy).ExtraInfo))
	NEXT Dummy
	PRINT "Completed restoration, finalising....
	File.Close
	PRINT "Totally completed. Please use the CfW file soon to repair the file to"
	PRINT "fully working order (it will work now, however)..
	PRINT
END IF

'Quit... 
PRINT "Thankyou for using this program. Should you have any problems,"
PRINT "e-mail me at d_foote@spyring.com"
PRINT "Otherwise, have fun with CfW2!!"
END