/* 
   $VER: BurnIT_CDCover.rexx 1.0 (20.10.99)
   Copyright 1999 Michael Siegel

   Description:
   Convert BurnIT CDDB ReadDiscIDs to CD covers plus inlays

   **** USE ENTIRELY AT YOUR OWN RISK ! ****
*/

PARSE ARG infilename
OPTIONS RESULTS

/* Constants */

cover_width        = 120.65
cover_heigth       = 120.65
cover_space        = 2

inlay_side_width   = 7
inlay_side_space   = 1
inlay_side_top     = 5
inlay_side_length  = 108

inlay_middle_width = 137
inlay_heigth       = 118
inlay_middle_space = 5

cover_x1           = 12.7
cover_y1           = 25.4
cover_x2           = cover_x1 + cover_width
cover_y2           = cover_y1 + cover_heigth

inlay_left_x1      = cover_x1
inlay_left_y1      = cover_y1 + cover_heigth
inlay_left_x2      = inlay_left_x1 + inlay_side_width
inlay_left_y2      = inlay_left_y1 + inlay_heigth

inlay_middle_x1    = inlay_left_x2
inlay_middle_y1    = inlay_left_y1
inlay_middle_x2    = inlay_middle_x1 + inlay_middle_width
inlay_middle_y2    = inlay_left_y2

inlay_right_x1     = inlay_middle_x2
inlay_right_y1     = inlay_middle_y1
inlay_right_x2     = inlay_right_x1 + inlay_side_width
inlay_right_y2     = inlay_middle_y2

/*******************************************************************/

tno_start_x = 32.7
tno_start_y = 161.4

/*******************************************************************/

/* Init */

tno         = 1
max_len     = 0

/*
infilename='dh3:cddb.txt'
*/

/* Test Pagestream */

IF ~SHOW('Ports', 'PAGESTREAM') THEN
DO
	ADDRESS COMMAND	'RUN Pagestream3:Pagestream3'
	ADDRESS COMMAND 'SYS:RexxC/WaitForPort Pagestream'
END

IF ~SHOW('Ports', 'PAGESTREAM') THEN
	RETURN 1

ADDRESS 'PAGESTREAM'

/* Init CDDB Data */

IF ~OPEN(infile, infilename, 'R') THEN
	RETURN 9

composer = READLN(infile)
titel    = READLN(infile)
titel    = STRIP(DELSTR(titel, LASTPOS('[', titel)))
titel    = replace_text(titel)

/* Count Lines */

DO UNTIL EOF(infile)

	titel_line    = READLN(infile)

	IF titel_line = "" THEN
		BREAK

	titel_line    = STRIP(titel_line)
	titel_time    = STRIP(SUBSTR(titel_line, LASTPOS('[', titel_line)))
	titel_line    = STRIP(DELSTR(titel_line, LASTPOS('[', titel_line)))

	len           = LENGTH(titel_line)

	IF len > max_len THEN DO
		max_len = len
	END

	tno_titel.tno = replace_quot(titel_line)
	tno_time.tno  = titel_time
	tno           = tno + 1
END

anzahl_tno = tno - 1
tno_heigth = 100 / anzahl_tno

IF tno_heigth > 10 THEN
	tno_heigth = 10

/* Init Pagestream */

'NEWDOCUMENT "BurnIT CDCover"'
'NEWMASTERPAGE "Default Master Page" 210mm 297mm SINGLE PORTRAIT'
'SETDIMENSIONS 210mm 297mm SINGLE PORTRAIT'
'SETMARGINGUIDES 12.7mm 12.7mm 25.4mm 25.4mm'
'SETCOLUMNGUIDES 1 1mm'
'SETDOCUMENTSTATUS UNCHANGED'
'OPENWINDOW "View 1" PAGE 1'

'OPENBUSYREQUESTER MESSAGE "Preparing CDCover..." THERMOMETER ENABLED ABORT ENABLED TOTAL 100 CURRENT 0'

busyreq = RESULT

/*
'FILLED OFF OBJECT'
'SETFILL BASIC FILL OBJECT'
'SETFPATTERN NONE FILL OBJECT'
'SETCOLORCOUNT 1 FILL OBJECT'
'SETCOLORSTYLE Black COLORNUMBER 0 FILL OBJECT'
'SETCOLORTINT 100% COLORNUMBER 0 FILL OBJECT'
'SETSCREEN 4 DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT DEFAULT OBJECT'
'STROKED OFF OBJECT'
'SETSTROKECOUNT 1 OBJECT'
'SETSTROKE SOLID STROKENUMBER 0 OBJECT'
'SETSTROKEWEIGHT 0.5pt STROKENUMBER 0 OBJECT'
'SETSTROKEJOIN MITER 11° STROKENUMBER 0 OBJECT'
'SETSTROKEBEGIN FLAT STROKENUMBER 0 OBJECT'
'SETSTROKEEND FLAT STROKENUMBER 0 OBJECT'
'SETSTROKECAP BUTT STROKENUMBER 0 OBJECT'
'SETFILL BASIC STROKENUMBER 0 OBJECT'
'SETFPATTERN NONE STROKENUMBER 0 OBJECT'
'SETCOLORCOUNT 1 STROKENUMBER 0 OBJECT'
'SETCOLORSTYLE Black COLORNUMBER 0 STROKENUMBER 0 OBJECT'
'SETCOLORTINT 100% COLORNUMBER 0 STROKENUMBER 0 OBJECT'
*/

/* CD Cover */

x1       = cover_x1
y1       = cover_y1
x2       = cover_x2
y2       = cover_y2
cd_space = cover_space

'DRAWBOX 'x1'mm 'y1'mm 'x2'mm 'y2'mm NORMAL'

test = set_busy(7)

IF test=1 THEN
	CALL clean_up

/* CD Cover Text */

'DRAWTEXTOBJ 'x1 + cd_space'mm 'y1 + cd_space'mm INFRONT'

txtid = RESULT

'SELECTTEXT AT 'x1 + cd_space'mm 'y1 + cd_space'mm FRONTMOST'

'SETALIGNMENT CENTER'
'SETBOLD OFF'

main_composer = replace_text(composer)

CALL print_text(main_composer)

'INSERT ":"'
'INSERTCONTROL NEWLINE'

'SETBOLD ON'

main_titel = replace_text(titel)

CALL print_text(main_titel)

/* 2x sonst macht es Pagestream nicht richtig...!!! */
/* Das Objekt wird beim ersten mal zu klein Resized */

'EDITTEXTOBJ POSITION 'x1 + cd_space'mm 'y1 + cd_space'mm 'x2 - cd_space'mm 'y2 - cd_space'mm OBJECTID 'txtid
'EDITTEXTOBJ POSITION 'x1 + cd_space'mm 'y1 + cd_space'mm 'x2 - cd_space'mm 'y2 - cd_space'mm OBJECTID 'txtid

test = set_busy(14)

IF test = 1 THEN
	CALL clean_up

/* CD Inlay Left Side */

x1        = inlay_left_x1
y1        = inlay_left_y1
x2        = inlay_left_x2
y2        = inlay_left_y2
cd_space  = inlay_side_space
cd_top    = inlay_side_top
cd_length = inlay_side_length
cd_width  = inlay_side_width

'DRAWBOX 'x1'mm 'y1'mm 'x2'mm 'y2'mm NORMAL'

test = set_busy(21)

IF test = 1 THEN
	CALL clean_up

/* CD Inlay Left Side Text */

'DRAWTEXTOBJ 'x1 + cd_space'mm 'y2 - cd_top'mm INFRONT'

txtid = RESULT

'SELECTTEXT AT 'x1 + cd_space'mm 'y2 - cd_top'mm FRONTMOST'

'SETBOLD ON'
'INSERT "'composer' - "'
'SETBOLD OFF'
'INSERT "'titel'"'

'EDITTEXTOBJ POSITION 'x1 + cd_space'mm 'y2 - cd_top'mm 'x1 + cd_length'mm 'y2 - cd_top + cd_width - 2 * cd_space'mm ROTATE 90° ABOUT 'x1 + cd_space'mm 'y2 - cd_top'mm OBJECTID 'txtid

test = set_busy(29)

IF test = 1 THEN
	CALL clean_up

/* CD Inlay Right Side */

x1     = inlay_right_x1
y1     = inlay_right_y1
x2     = inlay_right_x2
y2     = inlay_right_y2

'DRAWBOX 'x1'mm 'y1'mm 'x2'mm 'y2'mm NORMAL'

test = set_busy(36)

IF test = 1 THEN
	CALL clean_up

/* CD Inlay Right Side Text */

'DRAWTEXTOBJ 'x2 - cd_space'mm 'y1 + cd_top'mm INFRONT'

txtid = RESULT

'SELECTTEXT AT 'x2 - cd_space'mm 'y1 + cd_top'mm FRONTMOST'

'SETBOLD ON'
'INSERT "'composer' - "'
'SETBOLD OFF'
'INSERT "'titel'"'

'EDITTEXTOBJ POSITION 'x2 - cd_space'mm 'y1 + cd_top'mm 'x2 - cd_length'mm 'y1 + cd_top - cd_width + 2 * cd_space'mm ROTATE -90° ABOUT 'x2 - cd_space'mm 'y1 + cd_top'mm OBJECTID 'txtid

test = set_busy(43)

IF test = 1 THEN
	CALL clean_up

/* CD Inlay Middle */

x1        = inlay_middle_x1
y1	  = inlay_middle_y1
x2	  = inlay_middle_x2
y2	  = inlay_middle_y2
cd_space  = inlay_middle_space

'DRAWBOX 'x1'mm 'y1'mm 'x2'mm 'y2'mm NORMAL'

test = set_busy(50)

IF test = 1 THEN
	CALL clean_up

/* CD Inylay Text */

'DRAWTEXTOBJ 'x1 + cd_space'mm 'y1 + cd_space'mm INFRONT'

txtid = RESULT

'SELECTTEXT AT 'x1 + cd_space'mm 'y1 + cd_space'mm FRONTMOST'
'SETBOLD OFF'
'INSERT "'composer' - "'
'SETBOLD ON'
'INSERT "'titel'"'
'EDITTEXTOBJ POSITION 'x1 + cd_space'mm 'y1 + cd_space'mm 'x2 - cd_space'mm 'y1 + 2 * cd_space'mm OBJECTID 'txtid

prozentadd = 25 / anzahl_tno
prozent = prozentadd + 50

max_fontwidth = 0

/* Loop Tracks */

DO i = 1 TO anzahl_tno

	/* Write TNO # */

	'DRAWTEXTOBJ 'x1 + cd_space'mm 'tno_start_y'mm INFRONT'

	txtid = RESULT

	'SELECTTEXT AT 'x1 + cd_space'mm 'tno_start_y'mm FRONTMOST'
	'SETALIGNMENT CENTER'
	'INSERT " 'i' "'
	'STROKED ON OBJECTID 'txtid

	IF tno_heigth > 4 THEN
		'EDITTEXTOBJ POSITION 'x1 + cd_space'mm 'tno_start_y'mm 'tno_start_x + 5'mm 'tno_start_y + tno_heigth - 2'mm OBJECTID 'txtid
	ELSE
		'EDITTEXTOBJ POSITION 'x1 + cd_space + 10'mm 'tno_start_y'mm 'x1 + cd_space + 15'mm 'tno_start_y + tno_heigth - 2'mm OBJECTID 'txtid

	/* Write TNO Titel */

        len = (LENGTH(tno_titel.i) * 94) / max_len

	'DRAWTEXTOBJ 'tno_start_x + 10'mm 'tno_start_y'mm INFRONT'

	txtid = RESULT

	'SELECTTEXT AT 'tno_start_x + 10'mm 'tno_start_y'mm FRONTMOST'
	'INSERT "'tno_titel.i'"'

        'GETTEXTOBJ POSITION txtpos OBJECTID 'txtid

	txtheight1 = txtpos.bottom - txtpos.top
	txtwidth1  = txtpos.right - txtpos.left

	'EDITTEXTOBJ POSITION 'tno_start_x +  10'mm 'tno_start_y'mm 'tno_start_x + len'mm 'tno_start_y + tno_heigth - 2'mm OBJECTID 'txtid

	IF len = 94 THEN DO

		/* Get max fontwidth */

	        'GETTEXTOBJ POSITION txtpos OBJECTID 'txtid

		txtheight2 = txtpos.bottom - txtpos.top
		txtwidth2  = txtpos.right - txtpos.left

		fontwidth = (98 * txtheight1 * txtwidth2) / (txtheight2 * txtwidth1)

		IF fontwidht > max_fontwidth THEN
			max_fontwidth = fontwidth
	END

	/* Write TNO Time */

	'DRAWTEXTOBJ 'tno_start_x + 100'mm 'tno_start_y'mm INFRONT'

	txtid = RESULT

	'SELECTTEXT AT 'tno_start_x + 100'mm 'tno_start_y'mm FRONTMOST'
	'INSERT "'tno_time.i'"'
	'EDITTEXTOBJ POSITION 'tno_start_x +  100'mm 'tno_start_y'mm 'tno_start_x + 115'mm 'tno_start_y + tno_heigth - 2'mm OBJECTID 'txtid

	tno_start_y = tno_start_y + tno_heigth

	test = set_busy(prozent)

	IF test = 1 THEN
		CALL clean_up

	prozent = prozent + prozentadd
END

/* Set same fontwidth */

tno_start_y = 161.4

DO i = 1 TO anzahl_tno

	'SELECTTEXT AT 'tno_start_x + 10'mm 'tno_start_y'mm FRONTMOST'
	'SELECTTEXT ALL'

	IF max_fontwidth > 0 THEN
		'SETTYPEWIDTH 'fontwidth''

	tno_start_y = tno_start_y + tno_heigth

	test = set_busy(prozent)

	IF test = 1 THEN
		CALL clean_up

	prozent = prozent + prozentadd
END

set_busy(100)

'REFRESH ON'
'REFRESHWINDOW'

'CLOSEBUSYREQUESTER 'busyreq''
/*
'CLOSEWINDOW "View 1" PAGE 1'
*/

CLOSE(infile)
RETURN 0

/* Replace Text */

replace_text:

text      = ARG(1)
testindex = 0

DO WHILE POS(',', text, testindex + 1) > testindex

	testindexold = POS(',', text, testindex + 1)

	text         = DELSTR(text, testindexold, 1)
	text         = INSERT('\n', text, testindexold - 1)
	testindex    = testindexold + 2
END

testindex = 0

DO WHILE POS('(', text, testindex + 1) > testindex

	testindexold = POS('(', text, testindex + 1)

	text         = INSERT('\n', text, testindexold - 1)
	testindex    = testindexold + 2
END

testindex = 0

DO WHILE POS('/', text, testindex + 1) > testindex

	testindexold = POS('/', text, testindex + 1)

	text         = DELSTR(text, testindexold, 1)
	text         = INSERT('\n', text, testindexold - 1)
	testindex    = testindexold + 2
END

testindex = 0

DO WHILE POS('"', text, testindex + 1) > testindex

	testindexold = POS('"', text, testindex + 1)

	text         = DELSTR(text, testindexold, 1)
	text         = INSERT('''', text, testindexold - 1)
	testindex    = testindexold + 1
END

RETURN text

/* Replace Quot */

replace_quot:

text      = ARG(1)
testindex = 0

testindex = 0

DO WHILE POS('"', text, testindex + 1) > testindex

	testindexold = POS('"', text, testindex + 1)
	text         = DELSTR(text, testindexold, 1)
	text         = INSERT('''', text, testindexold - 1)
	testindex    = testindexold + 1
END

RETURN text

/* Print Text */

print_text:

text      = ARG(1)
testindex = 0

DO WHILE POS('\n', text, testindex + 1) > testindex

	testindexold = POS('\n', text, testindex + 1)

	p_text       = STRIP(SUBSTR(text, 1, testindexold - 1))
	text         = DELSTR(text, 1, testindexold + 1)

	'INSERT "'p_text'"'
	'INSERTCONTROL NEWLINE'

	testindex = testindexold + 2
END

'INSERT "'text'"'

RETURN

/* Set Busy */

set_busy:

value = ARG(1)

'SETBUSYREQUESTER 'busyreq' CURRENT 'value''
'GETBUSYREQUESTER 'busyreq''

RETURN RESULT

/* Clean Up */

clean_up:

'CLOSEBUSYREQUESTER 'busyreq''
/*
'CLOSEWINDOW "View 1" PAGE 1'
*/

CLOSE(infile)

EXIT
