#!/usr/bin/perl
use DBI;
require "config.pl";
$ref = "Ref.txt";
$localtime = localtime(time);
open LOG, ">>PURE.log";
print LOG "#----------- Parameter Update $localtime\n"; 
$dns = "DBI:mysql:$mysql_db:$mysql_host";
$dbh = DBI-> connect($dns,$mysql_user,$mysql_pwd);
$sth = $dbh -> prepare("select u_id from user");
$sth -> execute();
while (@user = $sth -> fetchrow_array()){
	$sth2 = $dbh -> prepare("select u_para_update from user where u_id = '$user[0]'");
	$sth2 -> execute();
	@date = $sth2 -> fetchrow_array();
	@update = localtime((stat("temp/$user[0].parameter.txt"))[9]);
	$year = $update[5]+1900;
	$month = $update[4]+1;
	$day = $update[3];
	split(/\-/,$date[0]);
	if(!-s "temp/$user[0].parameter.txt" or  $year < $_[0] or $month < $_[1] or ( $year == $_[0] and $month == $_[1] and $day <= $_[2])){
		print LOG "Update $user[0]'s parameter\n";
		#----------------- Create Medline File --------------
		$tmpfile = time;
		$sth2 = $dbh -> prepare("select i_full from interest where i_uid = '$user[0]' order by i_date desc");
		$sth2 -> execute();
		open OUT, ">temp/$tmpfile";
		while (@text = $sth2 -> fetchrow_array()){
			print OUT "$text[0]\n";
		}
		close OUT;
		system("perl train.pl $iterate $tmpfile $max_train_data $user[0]");
		system("perl calc_score.pl $user[0] $ref"); 
		system("perl calc_meansd.pl $user[0] $ref");
		unlink("temp/$user[0].$ref.score.txt");
		unlink("temp/$tmpfile");
	}
}
close LOG;
exit;


