#
#	references.pl - create or follow dynamic references
#
#	Daniel Smith, dansmith@autodesk.com, Mid Olympics, February 1992

# these lines for SoftList...
$cmds{"dynamic_ref_expand"} = '&dynamic_ref_expand';
$cmds{"reference_menu"} = '&reference_menu';
$cmds {"find_references"} = '&find_references';


%reference_menu_cmds = (
    '?',    '$arg = "r"; eval $cmds{"help"}',
);



sub reference_menu {
		&do_callbacks("references");

		print TTYOUT <<"+++";
$clear_str

                References Menu $init_message 

                total references: $total_references

        ?       help


+++

		print TTYOUT "\tyour choice >> "; &flush;
	# for debugging.

	foreach (keys %dynamic_references) {
	print TTYOUT "\n$dynamic_key_field{$_} = \"$dynamic_references{$_}\"\n";
		
		&dynamic_ref_expand($dynamic_references{$_});

	}
		read (TTYIN, $ans, 1);
		eval ($add_menu_cmds{$ans}) || 
				print TTYOUT "hey, no such option: $ans\n";

	1;
}


sub dynamic_ref_expand {
	local ($dynamic_path) = @_;
	local (@keyword);
	local ($found_it) = 0;

	($keyword, $ignore) = split('/', $dynamic_path);
	$keyword =~ s/^dynamic:\s+//;
	$remainder = substr($dynamic_path, index($dynamic_path, "/"));

	if ($dynamic_paths{$keyword}) {
		foreach (split(':', $dynamic_paths{$keyword})) {
			$real_path = $_ . $remainder;
			if (-e $real_path) {
				$found_it++;
				last;
			}
		}
		$found_it && print TTYOUT "\nwe get \"$real_path\" \n";
	}
	#return $found_it;
	1;
}


sub find_references {
	local ($pushd_arg[0]) = $DIRS{'DATA'};
	%dynamic_references = ();
	$total_references = 0;
	&pushd (*main_dir_stack, *pushd_arg);
	foreach $file (@found_records) {
		local (@cur_dynamic_lines) = ();
		open (CUR_FILE, $file);
		while (<CUR_FILE>) {

			# change this line so as not to be softlist dependent
			if (m!^package:!) {
				chop;
				s/^package:\s+//;
				$dynamic_key_field{$file} = $_;
			}
			
			if (m!^dynamic:!) {
				chop;	# no NL spoken here, nor "dynamic:"
				s/^dynamic:\s+//;	# and the WS goes too
				push(@cur_dynamic_lines, $_);
				$total_references++;
			}
		}

		if ($#cur_dynamic_lines != -1) {
			$dynamic_references{$file} = (@key_field,
							@cur_dynamic_lines);
		}
		close(CUR_FILE);
	}
	if ($total_references) {
		$reference_str = "R       references";
		$reference_hdr_str = ", with $total_references reference";
		if ($total_references > 1) {
			$reference_hdr_str .= 's';
		}
		$search_menu_cmds{'R'} = '&reference_menu';
	} else {
		$reference_str = '';
		$reference_hdr_str = '';
		$search_menu_cmds{'R'} = '0';
	}
	&popd (*main_dir_stack);
	1;

}

#	references_init - usually called from search_menu.pl
sub references_init {
	$refs_init = 1;
}
1;
