/* ImageLink sample ARexx script */
/* Read IFF Frames numbered sequentially */
/* By Eric Lavitsky, 8/23/89 */

arg arg1

cur_dir = pragma(d,'')

/* Turn on result code passing */
options results;
options failat 20

call addlib 'rexxsupport.library',0,-30;
list = showlist(p);
beg = pos('ILINK',list);

if (beg = 0) then
	/* Need to start ImageLink */
        address command 'cd "' || cur_dir || '"' || '0A'x,
	'run imagelink'

/* Make sure WaitForPort is in your path! */
address command 'SYS:bin/WaitForPort ILINK';

/* Find ImageLink's ARexx port and attempt to lock a */
/* private communications channel with it */
address 'ILINK' lock;

if (rc ~= 0) then do;
	say 'Could not lock ImageLink.';
	exit;
end;

/* Result contains the private communications channel */
address value result;

signal on error;
signal on syntax;
signal on break_c;

main:
	/* Assure the initial state */
	init;

	/* Get the version number for ImageLink */
	version;
	say 'ImageLink version is: 'result;

	/* Select initial modules */
	inpmod 'IFF ILBM File';
	outmod 'Targa Direct';

	link;

	/* Set this to the number of images you're reading */
	images = 1;

	/* Name the file as you like */
	file = 'RAM:SculptFile.';

do i = 1 to images
	j = POS('.',file);
	file = DELSTR(file,j+1);
	file = INSERT(i,file,j);
	file = TRIM(file);

	/* No output file for Targa Direct */
	inpfil file;

	/* We're working with a Targa-16 */
	dither floyd;
	color cval 16;

	/* These values are good for a Targa board */
	iscale 512 480 0 0 1 1
	oscale 512 480 0 0 1 1

	/* Start the conversion */
	start;

	/* Single frame record here and loop till done */
	say 'Frame 'i' Complete';
end;

finish:

	unlink;

	/* Free the channel */
	unlock;

	/* Tell ImageLink to exit */
	signal off error;
	options failat 100
	quit;
	options failat 20

	exit;

error:
	say 'Error: 'rc
	unlock;
	exit;

syntax:
	unlock;
	say 'ImageLink aborted script: syntax error'
	exit;

break_c:
	say 'Halted by ^C';
	unlock;
	exit;
