#!/app/apache/modules/1.3/php/5.0.3/bin/php -q
<?php
	include("/home/pyrot/.html/scripts/sqlinfo.inc");
	mysql_connect($host, $user, $pass);
	mysql_select_db("pyrot");

	$date = getdate();
	$mm = $date["mon"];
	$dd = $date["mday"]+3;
	$yyyy = $date["year"];
	$ymd = date("Y-m-d", mktime(0,0,0,$mm,$dd,$yyyy));

	$contents = getcontents($ymd);
	if ($contents != "") {
		$contents.="Vnliga hlsningar:\n\n        Pyrot LTU";

		$result = mysql_query("SELECT * FROM maillista");
		if ($r = mysql_fetch_array($result)){
			do{
				$bcc .= $r["mail"].",";
			} while($r = mysql_fetch_array($result));

			$contents.="\n\n"."-------\nFr att g ur listan: g till ";
			$contents.="http://www.pyrot.com/index.php?page=remove_maillist.php";

			mail("info@pyrot.com", "Information om kommande skjutningar", $contents, "BCC: $bcc\nFrom: \"Pyrot LTU\" <info@pyrot.com>");
		}
	}

	$dd = $date["mday"]+8;
	$ymd = date("Y-m-d", mktime(0,0,0,$mm,$dd,$yyyy));

	$contents = getcontents($ymd);
	if ($contents != "") {
		$pyromail .= "Fljande information kommer att skickas ut\n";
		$pyromail .= "till maillistan om fem dagar:\n";
		$pyromail .= "------------------------------------------\n\n";

		$pyromail .= $contents;

		$pyromail .= "------------------------------------------\n";
		$pyromail .= "    /// cron@brother\n";

		mail("info@pyrot.com", "Dags att maila om skjutning.", $pyromail, "From: \"Cron@brother\" <info@pyrot.com>");
	}

	function getcontents($thedate) {
		$res = mysql_query("SELECT * FROM event where datum = \"$thedate\"");
		$contents = "";
		if ($r = mysql_fetch_array($res)){
			$contents = "Skjutningar p gng fr pyrot:\n\n";
			do {
				$contents.=$r["rubrik"] . "\n";
				$contents.=$r["info"] . "\n";
				$contents.="Plats : " . $r["plats"] . "\n";
				$contents.="Datum : " . $thedate . "\n";
				$contents.="Tid   : " . $r["tid"] . "\n\n";
			} while ($r = mysql_fetch_array($res));
		}
		return $contents;
	}
?>
