/************************************************************************
 * ImageLink sample ARexx script					*
 * Create frames from Sculpt take.					*
 * By Eric Lavitsky, 8/23/89 - updated 3/10/90				*
 *									*
 *	Syntax:								*
 *		1>rx script num						*
 *									*
 *		Where num is the number of frames you wish to create	*
 *		(the same number as is in the take).                    *
 *		The frames will be numbered sequentially in the order	*
 *		in which they are created.				*
 ************************************************************************/

/************************************************************************
 *									*
 * Name the file as you like.						*
 * Change this line to reflect where your images will be placed.	*
 ************************************************************************/
file = 'DH1:Anim/Clock.';


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

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:
	/* Assure the initial state */
	init;

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

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

	link;

	images = arg1;

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

	/* No input file for Sculpt Direct */
	outfil file;

	/* We're creating a 24-Bit IFF File for nice compression */
	dither NONE;
	color cval 24;

	/* These values are good for a Targa board */
	scalem RESZS;
	iscale 1024 960 0 0 1024 960 12 10
	oscale 512 480 0 0 512 480 12 10

	/* 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 '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 = 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 100
	quit;
	options failat 20
	exit;

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

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