/* ImageLink sample ARexx script */
/* By Eric Lavitsky, 8/10/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;

	images = 1;

loop:
	/* Loop through directory entries here for input */

	/* The Targa Direct Module doesn't need a file name */
	inpfil 'PICS:24-Bit/seat';
	/* outfil 'RAM:out.tst'; */

	/* We're using a Targa-16 */
	dither none;
	color cval 16;

	/* Start the conversion */
	start;

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

	images = images - 1;
	if (images = 0) then signal finish;

	signal loop;

finish:

	unlink;

	/* Free the channel */
	unlock;

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

	exit;

error:
	say 'ImageLink/ARexx Error: 'rc
	unlock;
	/* Tell ImageLink to exit */
	signal off error;
	options failat 100
	quit;
	options failat 20
	exit;

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

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