#
# radbserver-init.pl
#
# Define some global constants, setup some global variables. 
#


sub radbserverInit {

	local($msg) = '';
	local($alldbs,$k,$v) = ('','','');

# command codes
# Be sure to update the command list arrays (below) AllCmdCodes, IndexCmdCodes, etc, as needed
# also update the Usage() text.

	($GetASInLinesCode,$GetASOutLinesCode) = (101,102);
	($GetRoutesByOriginCode,$GetRoutesByCommunCode,$ExpandASMacroCode) = (103,104,105);
	($GetRoutesByASMetricCode,$GetRoutesByCountryCode) = (106,107);
	($DumpAllRoutesCode,$GetObjectCode) = (108,109);
	($GetObjAttrCode,$FindObjsCode,$SpecialSearchCode) = (110,111,112);
	($EndSessionCode,$GetSpecialCommRoutesCode,$SetSourceCode) = (113,114,115);
	($GetAVTokens) = (116);
	$DebugShowCode = 122;		# z will call up debug show command

	($AddRouteOriginCode,$DeleteRouteOriginCode) = (65,66);
	($AddRouteASMetricCode,$DeleteRouteASMetricCode) = (67,68);
	($AddRouteCommunCode,$DeleteRouteCommunCode) = (69,70);
	($AddRouteCountryCode,$DeleteRouteCountryCode) = (71,72);
	($AddSpecialCode,$DeleteSpecialCode) = (73,74);
	$ReIndexCode = 82;

#
# command list arrays
#
	@AllCmdCodes = (65 .. 74, 82 , 101 .. 116, 122 );
	@ChangeCmdCodes = ( 65 .. 74, 82 );
	@IndexCmdCodes = ($GetRoutesByCommunCode,$GetRoutesByOriginCode,$GetRoutesByASMetricCode,
						$GetRoutesByCountryCode,$GetAllRoutesCode,$SpecialSearchCode);
	@AddCmdCodes = ($AddRouteOriginCode,$AddRouteASMetricCode,$AddRouteCommunCode,
					   $AddRouteCountryCode,$AddSpecialCode);

#
# return codes & other constants
#
	($DataLenCode,$DataReturnCode,$EODReturnCode) = (65,66,67);
	($KeyNotFoundCode,$MultipleInOneDBCode,$ErrorReturnCode) = (68,69,70);
	($asType,$commType,$asmacroType) = ("an","am","cm");

#
# escape character: if command starts
#  - one:  do one of the protocol commands
#  - two:  fork and keep processing protocol commands until quit command arrives (or time out)
#
	$EscapeChar = '!';

	$BangBangTimeOut = 3600;   # give them longer to cogitate before closing session...
	$NormalTimeOut	= 60;

#
# setup the log file.
#
	($LogPath) = '';
	($QRYLOG,$ERRLOG,$UILOG) = ('QRYLOG','ERRLOG','UILOG'); # names in LOGFILE associative array from conf file.

	if ( $opt_L ) {
		$LogPath = $opt_L;
		$LOGFILE{$QRYLOG} = "$LogPath/qrylog";
		$LOGFILE{$ERRLOG} = "$LogPath/errlog";
		$LOGFILE{$UILOG}  = "$LogPath/uilog"; # for update index commands
		print STDERR "\nLogging to $LOGFILE{$QRYLOG}, $LOGFILE{$ERRLOG}, $LOGFILE{$UILOG} .\n";
	}
	else {
		print STDERR "\nYou must specify a path to the log files, eg -L/f/rrdb/bin/radbserver.logs\n\n";
		exit -1;
	}

#
# these hosts are allowed to do updates and reindexing
#
	@TrustedHosts = ('radb.ra.net','radb2.ra.net','toad.merit.edu','cat.isi.edu',
						'fox.merit.edu','twain.merit.edu');

#
# constants for the db processing
#
	$BuiltRouteIndicies = 0;		# set to 1 when indices are rebuilt, so we don't do it again
	$DBIPath = "/usr/users/rlr/radbserver-indexes";
	if ( $opt_f ) {
		$DBIPath = $opt_f;
	}
	$SpecialCommRoutesPath = "/ra/radberver/radbserver.indexes"; # &GetSpecialCommRoutes looks here for special commun list files

#
# The prefixes for different kinds of values in the indexes (assoc arrays).
# Prepend when storing values and when doing the searches.
#
# ===> if you add a prefix, or commands that use a prefix, change GetIndexPrefix().
#
	$RoutesOfOriginPrefix	= "ROO";
	$RoutesOfCommPrefix		= "ROC";
	$RoutesOfCountryPrefix	= "ROK";
	$RoutesOfASMetricPrefix	= "ROAM";
	$SpecialSearchPrefix    = "SS";
	$ErrorPrefix			= "XXX";

	$LOOKinetrtrKey         = "LOOK=ir"; # key for inetrtr list lookups via Special-Search

#
# setup sources and dbfiles for those sources, then establish links to index files
#
	@sources = ();
	@dbfiles = ();

	$CheckIndexesExist = 1;
	$CheckIndexesOnStartup = $CheckIndexesExist;
	$CheckIndexesOnStartup = 0 if $opt_N; # we are creating them, so don't have to exist yet!

# setup list of *all* db names in conf file...

	$alldbs = '';
	while ( ($k,$v) = each %DBFILE ) {
		if ( $alldbs ) { $alldbs .= ",$k"; }
		else           { $alldbs = "$k"; }
	}

	if ( $opt_S ) {
		if ( ($msg=&SetSources($opt_S,*sources,*dbfiles,$CheckIndexesOnStartup)) ) {
			print STDERR "\n\nERROR from SetSources on -S from run-par: $msg\n";
			exit -1;
		}
	}
	elsif ( $opt_A ) {
		if ( ($msg=&SetSources($alldbs,*sources,*dbfiles,$CheckIndexesOnStartup)) ) {
			print STDERR "\n\nERROR from SetSources using -A: $msg\n";
			exit -1;
		}
	}
	else {
		if ( ($msg = &SetSources(join(',', split(/\s+/,$ALLLOOK) ),*sources,*dbfiles,$CheckIndexesOnStartup)) ) {
			print STDERR "\n\nERROR from SetSources on ALLLOOK: $msg\n";
			exit -1;
		}
	}

#
# setup list of special db community names...
#
# *If* this is done with indexed lookups...
# These are used as keys to value which is list
# of *all* (not-withdrawn) routes in each db.
# These are stored in the gdbm file $DBIPATH/REGROUTES
# and referenced via associative array %RouteCommunities.
# 
# *else* it just causes scans of associated db files.
#
# IRR-ROUTES is special for union of ALLLOOK list of db's.
#
	%RouteCommunities = ();
	@RouteCommunityNames = ();
	while ( ($k,$v) = each %DBFILE ) {
		push( @RouteCommunityNames, "$k-ROUTES" );
	}
	push( @RouteCommunityNames, "IRR-ROUTES" );

#
# build new indexes if asked for...
#
	if ( $opt_N ) {
   		print STDERR "\nBuilding indexes:\n";
		$opt_d = 1;
		&BuildRouteIndices( *dbfiles, 1 );
		exit;
	}


} # end radbserverInit 

1;
