<?php

function CompileCommand() {

	global $PROFILEDATADIR;
	global $ListNOption;
	global $TopNOption;
	global $IPStatOption;
	global $IPStatOrder;
	global $IPStatArg;
	global $LimitScale;
	global $OutputFormatArg;

	$profile = $_SESSION['profile'];

	if ( !isset($_SESSION['channels'])  ) {
		$_SESSION['channels'] = array();
	}
	$channels = $_SESSION['channels'];

	// get the sources selected for processing
	$_src = array();
	$args = '';

	for ($i=0; $i<count($channels); $i++ ) {
		if ( $_SESSION['p_sources'][$i] == 1 ) 
			$_src[] = $channels[$i];
	}

	// From the argument checks, we know at least one source is selected
	if ( count($_src) > 1 ) {
		// multiple sources
		$args = "-M $PROFILEDATADIR/$profile/" . implode(':', $_src);
		if ( $_SESSION['tleft'] == $_SESSION['tright'] ) {
			// a single 5 min timeslice
			$tslot1 = UNIX2ISO($_SESSION['tleft']);
			$subdirs = SubdirHierarchy($_SESSION['tleft']);
			if ( strlen($subdirs) == 0 )
				$args .= " -r nfcapd.$tslot1";
			else
				$args .= " -r $subdirs/nfcapd.$tslot1";
		} else {
			// several 5 min timeslices
			$tslot1 = UNIX2ISO($_SESSION['tleft']);
			$subdirs1 = SubdirHierarchy($_SESSION['tleft']);
			$tslot2 = UNIX2ISO($_SESSION['tright']);
			$subdirs2 = SubdirHierarchy($_SESSION['tright']);
			if ( strlen($subdirs1) == 0 ) 
				$args .= " -R nfcapd.$tslot1:nfcapd.$tslot2";
			else
				$args .= " -R $subdirs1/nfcapd.$tslot1:$subdirs2/nfcapd.$tslot2";
		}
	} else {
		// single source
		if ( $_SESSION['tleft'] == $_SESSION['tright'] ) {
			// a single 5 min timeslice
			$tslot1 = UNIX2ISO($_SESSION['tleft']);
			$subdirs = SubdirHierarchy($_SESSION['tleft']);
			$args .= " -r $PROFILEDATADIR/$profile/" . $_src[0] . "/$subdirs/nfcapd.$tslot1";
		} else {
			// several 5 min timeslices
			$tslot1 = UNIX2ISO($_SESSION['tleft']);
			$subdirs1 = SubdirHierarchy($_SESSION['tleft']);
			$tslot2 = UNIX2ISO($_SESSION['tright']);
			$subdirs2 = SubdirHierarchy($_SESSION['tright']);
			if ( strlen($subdirs1) == 0 ) 
				$args .= " -R $PROFILEDATADIR/$profile/" . $_src[0] . "/nfcapd.$tslot1:nfcapd.$tslot2";
			else
				$args .= " -R $PROFILEDATADIR/$profile/" . $_src[0] . "/$subdirs1/nfcapd.$tslot1:$subdirs2/nfcapd.$tslot2";
		}
	}

	// process stat request
	if ( array_key_exists('dostat', $_POST) ) {
		$_tmp = $_SESSION['p_topN'];
		$args .= " -n " . $TopNOption[$_tmp];
		// -s record
		if ( $_SESSION['p_flowstat'] == 1 ) {
			$args .= " -s record";
			// has a ober by option
			$_tmp = $_SESSION['p_floworder'];
			$args .= "/" . $IPStatOrder[$_tmp];
		}
		// -s <somestat>
		if ( $_SESSION['p_ipstat'] == 1 ) {
			$_tmp = $_SESSION['p_stattype'];
			$args .= " " . $IPStatArg[$_tmp];
			// has a ober by option
			$_tmp = $_SESSION['p_statorder'];
			$args .= "/" . $IPStatOrder[$_tmp];
		}
		if ( $_SESSION['p_IPv6_long2'] > 0 )
			$args .= ' -6';
		if ( $_SESSION['p_output_format2'] ) {
			$_tmp = $_SESSION['p_output_format2'];
			$args .= ' ';
			$args .= $OutputFormatArg[$_tmp];
		}
		// limits -L/-l
		if ( $_SESSION['p_limitoutput'] == 1 ) {
			if ( $_SESSION['p_limitwhat'] == 1 )
				$args .= " -L ";
			else
				$args .= " -l ";
			if ( $_SESSION['p_limithow'] == 1 )
				$args .= '-';
			$args .= $_SESSION['p_limitsize'];
			if ( $_SESSION['p_limitscale'] > 0 )
				$args .= $LimitScale[$_SESSION['p_limitscale']];
		}
	} 

	// process list request
	if ( array_key_exists('dolist', $_POST) ) {
		if ( $_SESSION['p_longoutput1'] > 0 )
			$args .= ' -o extended';
		if ( $_SESSION['p_timesorted'] > 0 )
			// sort the flows from all sources
			$args .= ' -m';
		$_tmp = $_SESSION['p_listN'];
		$args .= " -c " . $ListNOption[$_tmp];
		if ( $_SESSION['p_aggregate'] == 1 )
			$args .= " -a";
		// v6 long and output format
		if ( $_SESSION['p_IPv6_long1'] > 0 )
			$args .= ' -6';
		if ( $_SESSION['p_output_format1'] ) {
			$_tmp = $_SESSION['p_output_format1'];
			$args .= ' ';
			$args .= $OutputFormatArg[$_tmp];
		}
	}
	$args .= " " . $_SESSION['combinedfilter'];
	print "<pre>\n";
	print_r("nfdump $args");
	print "</pre>\n";
	return "$args";

} // End of CompileCommand


?>
