#!/bin/php
<?php
// specific times for special pkgs - kernel/firmwares/... (if Manifest)

$pkgs_orig = scan_repo("/var/db/repos/petersweeze");
$pkgs = scan_repo("/var/db/repos/petersweeze",true,true);
echo "Detected ".count($pkgs_orig)." packages\r\n";
echo "Detected ".count($pkgs)." packages (excluded dev-node/* + bootstrap-toolchain/*)\r\n";
echo "Must act on ".(ceil(count($pkgs)/52))." per week, to touch every pkg once a year\r\n";
$todo_pkgs=array();
$oldest_pkg=array();
$pkg_with_time=array();
foreach($pkgs as $pkg){
    if($pkg=="sys-apps/pw-bootstrap-phase1") continue; // Empty Folder because of dummy for bootstrap creation on buildhost
    $exec = shell_exec("svn info /var/db/repos/petersweeze/".$pkg);
    $date = explode("Last Changed Date: ",$exec);
    $date = substr($date[1],0,10);
    $time = strtotime($date);

    $pkg_with_time[]=array($pkg,$time);
}

echo "TODO-List:\r\n";

$lasttime=0;
for($i=0;$i<(ceil(count($pkgs)/52));$i++){
    $oldpkg = get_oldest_pkg($lasttime,$pkg_with_time);
    $lasttime=$oldpkg[1];
    echo date("d.m.Y",$oldpkg[1]).": ".$oldpkg[0]."\r\n";
}


function get_oldest_pkg($cmptime,$pkg_with_time){
    $foundtime=0;
    $foundpkg="";
    foreach($pkg_with_time as $pkgarr){
	$pkg=$pkgarr[0];
	$time=$pkgarr[1];
	if($time>$cmptime){
	    if(empty($foundpkg) || $time<$foundtime){
		$foundpkg=$pkg;
		$foundtime=$time;
	    }
	}
    }
    return(array($foundpkg,$foundtime));
}

echo "\r\n\r\nChecklist:\r\n";
echo "- New upstream package available?\r\n";
echo "- Folder Structure ok (TODO.txt available)?\r\n";
echo "- Delete old ebuilds?\r\n";
echo "- Create new verbump if older than one year?\r\n";
echo "- Really needed by dependency?\r\n";
echo "- In other repository available?\r\n";
echo "- Seperate Task needed for continues updating?\r\n";
echo "- Copyright in ebuild till current year?\r\n";
echo "- Own package? no usage of php-cli, use inherit pwphpbytecode?\r\n";
echo "- Use framework for etc-autoupdate/portage-reinstall over pw-build-meson?\r\n";
echo "- Use of pw-build-meson and pcc_embedfile?\r\n";
echo "- Source code is structured and use classes and libraries?\r\n";

function scan_repo($dir,$skip_dev_node=false,$skip_bootstrap=false){
    $cats=array();
    $scan = scandir($dir);
    foreach($scan as $file){
	if($file=="." || $file==".." || $file=="eclass" || $file=="metadata" || $file==".svn") continue;
	if($skip_dev_node && $file=="dev-node") continue;
	if($skip_bootstrap && $file=="bootstrap-toolchain") continue;
	if(is_dir($dir."/".$file)){
	    $cats[]=$file;
	}
    }

    $pkgs=array();
    foreach($cats as $cat){
	$scan = scandir($dir."/".$cat);
	foreach($scan as $file){
	    if($file=="." || $file=="..") continue;
	    if(is_dir($dir."/".$cat."/".$file)){
		$pkgs[]=$cat."/".$file;
	    }
	}
    }
/*
    $pkg_details=array();
    foreach($pkgs as $pkg){
	$pkg_oldest=-1;
	$pkg_newest=-1;
	$pkg_cnt=0;
	$scan = scandir($dir."/".$pkg);
	foreach($scan as $file){
	    if($file=="." || $file==".." || $file=="Manifest") continue;
	    if(substr($file,-7)!=".ebuild") continue;
	    $filemtime=filemtime($dir."/".$pkg."/".$file);
	    if($filemtime>$pkg_newest)$pkg_newest=$filemtime;
	    if($filemtime<$pkg_oldest || $pkg_oldest==-1)$pkg_oldest=$filemtime;
	    $pkg_cnt++;
	}
	
	$filemtime=filemtime($dir."/".$pkg);
	if($filemtime>$pkg_newest)$pkg_newest=$filemtime;
	if($filemtime<$pkg_oldest || $pkg_oldest==-1)$pkg_oldest=$filemtime;
	
	$pkg_details[$pkg]=array(
	    "oldest"=>$pkg_oldest,
	    "oldest_txt"=>date("d.m.Y",$pkg_oldest),
	    "newest"=>$pkg_newest,
	    "newest_txt"=>date("d.m.Y",$pkg_newest),
	    "count"=>$pkg_cnt
	);
    }
*/
    return($pkgs);
}