#!/usr/local/pkg/php/bin/php -q

<?php

// Establish connection parameters
//
	$hostname = "astraea2";   //declaring variables for mysql access
	$databasename = "gmag_ascii_files"; //all these values are 
																				 //unavailable to the user 
																				 //and thus secure
	$username = "thmsoc";   //these will have to be set for all search 
												  //forms that access the mysql database
	$password = "ishkabibel";

	if ($_SERVER['argc'] !=8) {
		die("Check arguments: Need tablename, datatime, filename, processtime, and filesize\n") ;
	}

// Assign command line input variables
//
	$table_name = $_SERVER['argv'][1] ;
	$dataTime = $_SERVER['argv'][2] . " " . $_SERVER['argv'][3] ;
	$fileName = $_SERVER['argv'][4] ;
	$processTime = $_SERVER['argv'][5] . " " . $_SERVER['argv'][6] ;
	$fileSize = $_SERVER['argv'][7] ;

	/*
	print "table_name = " . $table_name . "\n" ;
	print "dataTime = " . $dataTime . "\n" ;
	print "fileName = " . $fileName . "\n" ;
	print "processTime = " . $processTime . "\n" ;
	print "fileSize = " . $fileSize . "\n" ;
	*/
	
// Make connection to MySQL server
//
$databaselink = mysql_connect($hostname, $username, $password) 
		or die("Could not connect: " . mysql_error());

// Select particular database
//
	mysql_select_db($databasename, $databaselink) or die("Could not use database: $databasename");

// Check to see if new input path already exists in db, and
// if a matching row is found, delete it
//
$db_query = "select FileName from $table_name where FileName like \"$fileName\"" ;
$result = mysql_query($db_query)
	or die("Could not query: " . mysql_error());
$num_rows = mysql_num_rows($result);
//print "num_rows = " . $num_rows . "\n" ;
if ($num_rows != "0"){
	$db_input = "delete from $table_name where FileName like \"$fileName\"" ;
	mysql_query($db_input)
		or die("Could not query: " . mysql_error());
}

// Construct and insert new input
//
	$db_input = "insert into $table_name (DataTime,FileName,ProcessTime,FileSize)
	values (\"$dataTime\",\"$fileName\",\"$processTime\",\"$fileSize\")" ;
	mysql_query($db_input)
		or die("Could not query: " . mysql_error());

	$db_input = "alter table $table_name order by DataTime" ;
	mysql_query($db_input)
		or die("Could not query: " . mysql_error());

	$db_input = "alter table $table_name drop Id, add Id int unsigned 
		     not null auto_increment first, auto_increment = 1" ;
	mysql_query($db_input)
		or die("Could not query: " . mysql_error());

?>
