# this needs attention...Sat Sep 19 18:07:18 PDT 1992

# and it still does... Tue Nov  3 15:27:16 PST 1992


#       update $cmds so that we just load once
$cmds{"batch_menu"} = '&batch_menu';
$cmds{"batch_input"} = '&batch_input';
$cmds{"batch_found_record_output"} = '&batch_found_record_output';
$cmds{"batch_output"} = '&batch_output';
$cmds{"batch_file_input"} = '&batch_file_input';
$cmds{"batch_file_output"} = '&batch_file_output';

%batch_menu_cmds = (
		    '?',	'$arg = "b"; eval $cmds{"help"}',
		    'O',	'&batch_found_record_output',
		    'i',	'&batch_input',
		    'o',	'&batch_output',
		    '!',	'&quick_shell',
		    ' ',	'&pop_level',
		    '@',        'eval $cmds{"eval_perl_expression"}',
		    '-',	'&push_previous_menu',
		    );

$batch_menu_cmds{pack("c", 27)} = '&main_menu';         # ESC


#	batch_menu - produce new records or write a report
sub batch_menu {
    push(@menu_stack, "&batch_menu");
    while ($menu_stack[$#menu_stack] =~ "&batch_menu") {
	print TTYOUT <<"+++";
$clear_str
		Batch Input/Output Menu $init_message


	?       help

	i	input new records from file
	o	output all records to a file
	O	output only recently found records to a file

	current in:	$current_batch_file_in
	current out:	$current_batch_file_out

+++
        print TTYOUT "\tyour choice >> "; &flush;
	read(TTYIN, $ans, 1);
	eval($batch_menu_cmds{$ans}) || 
	    print TTYOUT "hey, no such option: $ans\n";

    }
    1;
}


#	these three called from a menu... 
sub batch_input {
    local($pushd_arg[0]) = $DIRS{'DATA'};
    $force_input = $current_batch_file_in;
    &file_prompt;
    $current_batch_file_in = $filename_history[$#filename_history];
    &pushd(*main_dir_stack, *pushd_arg);
    &batch_file_input ($current_batch_file_in);
    &popd(*main_dir_stack);
    1;
}


#	batch_found_record_output - output just the most recently found files
sub batch_found_record_output {
    $batch_found_output = 1;
    &batch_output;
    $batch_found_output = 0;
}
	
sub batch_output {
    local($pushd_arg[0]) = $DIRS{'DATA'};
    $force_input = $current_batch_file_out;
    &file_prompt;
    $current_batch_file_out = $filename_history[$#filename_history];
    &pushd(*main_dir_stack, *pushd_arg);
    &batch_file_output ($current_batch_file_out);
    &popd(*main_dir_stack);
    1;
}


#	batch_file_input - make up new records from an incoming stream
#
#	This first pass should break up things pretty well.  This is either
#	called from an interactive menu via batch_input, or from a
#	command line option a la crontab

#	BUG: I've added to the standard input filename ("-") so that you
#	can't access it.  If you use it more than once it core dumps.
sub batch_file_input {
    local($batch_file) = @_;

    # standard input case...
    if ($batch_file =~ /- sigh, standard in is broken if used twice.../) {
	print TTYOUT <<"+++";


	You've selected standard input for batch input.  Type or paste your
entries here.  When you are done, enter your End Of File character
(which usually will be Control-D) on a line by itself.

enter records:
+++
        unless (open(BATCH_INPUT, "< -")) {
	    warn "can't open $batch_file: $!\n";
	    sleep 2;
	    return 1;
	}
        

				# # this is all hosed... 
        &cbreak_off;
	} else { # filename case...

		# temporary for 2.0
		if ($batch_file =~ /-/) {
			warn "don't use standard input for now...\n";
			sleep 2;
                        return 1;
		}

		unless (open(BATCH_INPUT, $batch_file)) {
                        warn "\ncan't open $batch_file: $!\n";
                        sleep 2;
                        return 1;
		}
	}
        $/ = "";
	$rec_num = 1;

	while (<BATCH_INPUT>) {
                $/ = "\n";
		undef ($desc_str);

		# no doubt this is a slow way of assigning variables...I'm
		# wide open to suggestions... (horrors! pg 96)
		@cur_rec = split (/^/);
		foreach $line (@cur_rec) {
			chop ($line);
			if ($line =~ /^package:/) {
				($package_name = $line) =~ s=^package:\s==;
				next;
			} elsif ($line =~ /^contact:/) {
				($contact_name = $line) =~ s=^contact:\s==;
				next;
			} elsif ($line =~ /^location:/) {
				($package_location = $line) =~ s=^location:\s==;
				next;
			} else {
				$desc_str .= $line;
			}
		}

		$/ = "";
		if (&update == 0) {
			last;
		} else {
			print TTYOUT "\ndid $package_name";
		}
		print TTYOUT "\n";
	}
	&cbreak_on;
	close (BATCH_INPUT);
        &do_fork('&init_filenames', 'filenames_done', 'QUIT');
	return 1;
}


sub batch_file_output {
    local($batch_file) = @_;
    local(@the_files);

    unless (open(BATCH_OUTPUT, "> $batch_file")) {
	warn "can't open $batch_file: $!\n";
	sleep 2;
	return 1;
    }

    if ($batch_found_output) {
	@keys_or_files = @found_records;
	print BATCH_OUTPUT <<"+++";

	These records matched the search pattern:

		$search_pattern

+++
    } else {
	@keys_or_files = sort keys %all_packages;
    }
    
    foreach $output_key (@keys_or_files) {
	print TTYOUT "working on $output_key\n";
	if ($batch_found_output) {
	    open (CUR_IN, "< $DIRS{'DATA'}/$output_key") ||
		warn "cannot open: $!\n";
	} else {
	    open (CUR_IN, "< $DIRS{'DATA'}/$all_packages{$output_key}") ||
		warn "cannot open: $!\n";
	}
	while (<CUR_IN>) {
	    print BATCH_OUTPUT $_;
	}
	close(CUR_IN);
    }
    close(BATCH_OUTPUT);
    1;
}	

