/************************************************************************
 * ImageLink sample ARexx script					*
 * Take named input file and pass through ImageLink			*
 * By Eric Lavitsky, 1/28/90						*
 *									*
 * Assumes ImageLink is running with the correct modules linked		*
 * Assumes output needs no file (Targa Direct) - may be changed		*
 * Use ARexx 1.06 or later (1.10 preferred)				*
 ************************************************************************/

/************************************************************************
 *									*
 * Change the value of targatype to reflect the TARGA board you have.	*
 * Valid values would be 16, 24 or 32					*
 *									*
 ************************************************************************/
targatype = 32;

/************************************************************************
 *									*
 * Change the value of dithmethod to reflect the type of dithering you	*
 * require. Valid values would be 'none' or 'floyd'.			*
 *									*
 ************************************************************************/
dithmethod = 'none';


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 '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:
	/* No output file for Targa Direct */
	inpfil arg1;

	dither dithmethod;
	color cval targatype;

	/* Place scaling information here for modules which require it */
	iscale 0 0 0 0 0 0 0 0;
	oscale 0 0 0 0 0 0 0 0;

	/* Start the conversion */
	start;

	/* Single frame record */
	/* record 1; */

finish:
	/* Free the channel */
	unlock;

	exit;

error:
	say 'ImageLink/ARexx Error:'
	select
	 when rc = 80 then say ' LOCK: ImageLink already Locked';
	 when rc = 81 then say ' UNLOCK: Nothing to UnLock';
	 when rc = 82 then say ' LINK: No Input Module Selected';
	 when rc = 83 then say ' LINK: No Output Module Selected';
	 when rc = 84 then say ' START: No Input File';
	 when rc = 85 then say ' START: No Output File';
	 when rc = 86 then say ' START: Problem with Input Data';
	 when rc = 96 then say ' Transfer Already In Progress';
	 when rc = 97 then say ' No Such Module Exists';
	 when rc = 98 then say " No Such File or Can't Create File";
	 when rc = 99 then say ' RECORD: Problem with Transport Controller';
	end;
	/* Tell ImageLink to exit */
	signal off error;
	unlock;
	options failat 101
	quit;
	options failat 20
	exit;

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

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