<?php
// PHP script to create Parsix watch file table rows.
// Coyright 2007 Alan Baghumian / GNU/GPL
// Based on Debian DEHS http://svn.debian.org/wsvn/dehs

$vars=$_GET["vars"];
uscan($vars);

// Display warnings?
$show_warnings=false;

function uscan($vars){
	$arrvars=explode("*", $vars);
	$path = $arrvars[0];

	$arrarchs=explode("-", $arrvars[1]);

        if(!empty($arrarchs[0])){
              $archs .= "<font color=red>".$arrarchs[0]."</font>&nbsp;&nbsp;";
        }

        if(!empty($arrarchs[1])){
              $archs .= "<font color=green>".$arrarchs[1]."</font>&nbsp;&nbsp;";
        }

        if(!empty($arrarchs[2])){
              $archs .= "<font color=blue>".$arrarchs[2]."</font>";
        }

	# work around the plus bug
	$path=str_replace("KKK", "+", $path); 

	$uscan_res=shell_exec("/usr/bin/uscan --report --dehs --check-dirname-level 0 ".$path);

        # work around the plus bug
	$uscan_res=str_replace("+", "KKK", $uscan_res); 

	$p = xml_parser_create();
	xml_parse_into_struct($p, $uscan_res, $vals, $index);
	xml_parser_free($p);

	$pkg=$vals[$index['PACKAGE'][0]][value];
	$pkg=str_replace("KKK", "+", $pkg); 

        $upstream=$vals[$index['UPSTREAM-VERSION'][0]][value];
	$up_url=$vals[$index['UPSTREAM-URL'][0]][value];
	$up_url=str_replace("KKK", "+", $up_url); 
	$parsix=$vals[$index['DEBIAN-UVERSION'][0]][value];
	$status=$vals[$index['STATUS'][0]][value];
	$warnings=$vals[$index['WARNINGS'][0]][value];
	if ($upstream=='') $upstream=null;
	if ($up_url=='')  $up_url=null;
	if ($parsix=='') $parsix=null;
	if ($status=='') $status=null;
	if ($warnings=='') $warnings=null;

	if($upstream == $parsix){
		$color="green";
	}else{
		$color="red";
	}

	switch($status)
	{
		case "up to date":
			$bk_color="#00ff00";
			break;
		case "Newer version available":
			$bk_color="#ffff00";
			break;
		case "Debian version newer than remote site":
			$bk_color="#ffcc99";
			break;
	}	
	
	if($pkg !='' || $pkg !=null){
		if($upstream != null){
			echo "<tr bgcolor=white><td><b>".$pkg."</b></td><td><a href=".$up_url.">".$upstream."</a></td><td><font color=".$color.">".$parsix."</font></td><td align=center>".$archs."</td><td bgcolor=".$bk_color.">".$status."</td></tr>";
		}else{
			if($show_warnings){
				echo "<tr bgcolor=white><td><b>".$pkg."</b></td><td colspan=4 bgcolor=#ff9966>".$warnings."</td></tr>";
			}
		}
	}
}	
?>
