//	This script simply sets things up and then calls the built-in
//	'write_script' function.
//
//  Copyright (c) 1991, Eric Rosenquist, Strata Software
//
//	Parameters to the write_script() function are as follows:
//
//	error = write_script(file_handle, end_key, thresh_typed, thresh_wait, prompt_length);
//
//	Where:
//
//	'error' is an integer, with any value other than 0 indicating an
//		error when writing to the output file.
//
//	'file_handle' is a handle to an open file.  Script commands will
//		be sent to this file.  The special file handles -1, -2, and -3
//		(CONSOLE, PORT, PRINTER) are allowed although it would not be
//		wise to send the script output to the current PORT.
//
//	'end_key' is the scan code of the key that signals an end to
//		script writing mode.  Shift-UNDO will also end script writing
//		mode, but the output script will be incomplete.
//
//	'thresh_typed' is a value in 10ths of a second that is used by the
//		function to determine when a piece of user input is
//		'complete'.  If the time between two typed characters is less
//		than this value, the characters will be sent together as part
//		of the same response.  Note that a Carriage Return always
//		terminates a response - this value is needed for hosts that do
//		not require a CR at the end of a response.  Setting the value
//		too low will cause the script to have a lot of small
//		prompt/response pairs, especially if the user is a slow
//		typist.  Setting the value too high can cause separate
//		responses to be grouped into one.
//
//	'thresh_wait' is a value in 10ths of a second.  The generated
//		script will include an explicit wait() call if the time
//		elapsed between a prompt and a response or before an
//		unprompted response is greater than this interval.  This is
//		really only useful if the host cannot tolerate any typeahead
//		or requires some time delay between the end of a prompt and
//		the sending of the response.
//
//	'prompt_length' is the maximum number of character from a host
//		prompt that should be used.  A value of 0 means to use the
//		entire prompt.  A positive value causes that last 'prompt_length'
//		characters to be used, while a negative value causes the first
//		'prompt_length' characters to be used.

string	directory[128];
string	filename[16];
string	pathname[128];

  /////////////////////////////////
 //         open_file           //
/////////////////////////////////
function open_file() returns int
	int		handle;
	int		open_it;

	directory[0] = file_get_drive();
	directory[1] = ':';
	strcat(file_get_dir(0, directory + 2), "\\*.BTS");

	filename[0] = pathname[0] = 0;

	repeat
		open_it = TRUE;
		if !fsel(directory, filename, pathname, "BackTALK Script To Create?")  or  !filename[0] then
			exit(0);
		endif
		if file_exists(pathname) then
			if alert("[3][ Overwrite existing file? | ][ Yes | No ]") == 2 then
				open_it = FALSE;
			endif
		endif
		if open_it then
			if (handle = file_create(pathname)) < 0 then
				alert("[3][ Error! | The file could not | be created.][ Cancel ]");
				exit(handle);
			endif
			return handle;
		endif
	until FALSE;
endfunction


  /////////////////////////////////
 //			main				//
/////////////////////////////////
function main()
	int		file_handle;

	file_handle = alert("[0][ Send script output | to a file, the screen, | or the printer? ][File|Screen|Printer]");
	if file_handle == 2 then
		file_handle = -1;
	else
		if file_handle == 3 then
			file_handle = -3;
		else
			-- Prompt for a file name
			file_handle = open_file();
		endif
	endif
	show_message("Type ^Z to exit", FALSE);	// ^Y on some foreign keyboards
	file_puts("function main()\r\n", file_handle);
	if not write_script(file_handle, 0x2C1A, 15, 20, 20) then
		file_puts("endfunction\r\n", file_handle);
		if file_handle >= 0 then
			file_close(file_handle);
		endif
	endif
	show_message(NULL, FALSE);
endfunction
