/************************************************************************
 * RasterLink sample ARexx script					*
 * Read IFF Frames numbered sequentially to Caligari Files		*
 *  for frame-by-frame animation capability.				*
 * By Eric Lavitsky, 3/10/90						*
 *									*
 *	Syntax:								*
 *		1>rx script inpfile outfile num                         *
 *									*
 *		where outfile is the prefix for the caligari output	*
 *		file, which will be written in the form:		*
 *			prefix.num.6rn					*
 *		and num is the number of frames you wish to convert.	*
 ************************************************************************/

/************************************************************************
 *									*
 * 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';

/************************************************************************
 * The only other changes you may have to make would be specifically	*
 * related to the output module and image sizes being used (see below)	*
 ************************************************************************/

arg arg1 arg2 arg3

say 'args were 'arg1 arg2;

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 RasterLink */
        address command 'cd "' || cur_dir || '"' || '0A'x,
	'run RasterLink'

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

/* Find RasterLink'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 RasterLink.';
	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 RasterLink */
	version;
	say 'RasterLink version is: 'result;

	/* Select initial modules */
	inpmod 'IFF ILBM File';
	outmod 'Caligari Broadcast File';

	link;

	inpfile = arg1;
	outfile = arg2;
	images = arg3;
	say 'Converting 'images' images';

do i = 1 to images

	/* First build the input file name */
	foo = inpfile;
	j = INDEX(foo,'.');
	if (j = 0) then do;
		j = LENGTH(foo);
		foo = INSERT('.',foo,j);
		j = LENGTH(foo);
	end;
	foo = INSERT(i,foo,j);
	foo = TRIM(foo);

	inpfil foo;

	/* Then build the output file name */
	foo = outfile;
	j = INDEX(foo, '.');
	if (j = 0) then do;
		j = LENGTH(foo);
		foo = INSERT('.',foo,j);
		j = LENGTH(foo);
	end;
	foo = INSERT(i,foo,j);
	foo = TRIM(foo);
	j = LENGTH(foo);
	foo = INSERT('.6rn',foo,j);

	outfil foo;

	dither dithmethod;
	color cval targatype;

	scalem reszs;
	iscale 0 0 0 0 0 0 0 0
	oscale 512 482 0 0 512 482 12 10

	/* Start the conversion */
	start;

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

finish:

	unlink;

	/* Free the channel */
	unlock;

	/* Tell RasterLink to exit */
	signal off error;
	options failat 101
	quit;
	options failat 20

	exit;

error:
	say 'RasterLink/ARexx Error:'
	select
	 when rc = 80 then say ' LOCK: RasterLink 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 = 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 RasterLink to exit */
	signal off error;
	unlock;
	options failat 101
	quit;
	options failat 20
	exit;

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

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