#!/bin/php
<?php

$tree = build_tree();
echo "The following packages are HEADs (=no rev deps) and have the right name-syntax to use it in world-file:\r\n";
foreach($tree as $pkg=>$revdeps){
    if(count($revdeps)<1 && (substr($pkg,0,17)=="virtual/pw-basic-" || substr($pkg,0,17)=="virtual/pw-addon-")){
	echo "- ".$pkg."\r\n";
    }
}
echo "\r\n";
echo "[TODO] The following packages are HEADs (=no rev deps) but dont follow the right name-syntax - please rename, remove or add reverse dependency:\r\n";
foreach($tree as $pkg=>$revdeps){
    if(count($revdeps)<1 && !(substr($pkg,0,17)=="virtual/pw-basic-" || substr($pkg,0,17)=="virtual/pw-addon-")){
	echo "- ".$pkg."\r\n";
    }
}
echo "\r\n";
echo "[TODO] The following packages have reverse deps (= no HEAD Package), but thats not allowed because of their naming - please rename or remove reverse dependency:\r\n";
foreach($tree as $pkg=>$revdeps){
    if(count($revdeps)>0  && (substr($pkg,0,17)=="virtual/pw-basic-" || substr($pkg,0,17)=="virtual/pw-addon-")){
	echo "- ".$pkg."\r\n";
    }
}

function build_tree(){
    echo "Calculation...";
    $all = get_all_pkg();
    
    $deptree=array();
    foreach($all as $pkg){
	$deptree[$pkg]=array();
    }

    foreach($all as $pkg){
	$dependons = get_dependencys($pkg);
	echo "x";
	foreach($dependons as $dependon){
	    if(!in_array($dependon,$all)) continue;
	    if(in_array($pkg,$deptree[$dependon])) continue;
	    $deptree[$dependon][]=$pkg;
	}
    }
    echo " done!\r\n";
    return($deptree);
}

function get_all_pkg(){
    $txt = shell_exec("equery --no-color --no-pipe list --overlay-tree \"*::petersweeze\" --format=\"\\\$category/\\\$name\"");
    echo "c";
    $txt = explode("\n",$txt);
    $pkglist=array();
    foreach($txt as $line){
	$line=trim($line);
	if(empty($line)) continue;
	if(substr($line,0,1)=="*") continue;
	if(in_array($line,$pkglist)) continue;
	$pkglist[]=$line;
    }
    return($pkglist);
}

/*
function get_all_pkg(){
    $cats = scandir(".");
    $pkglistmaster=array();
    foreach($cats as $cat){
	if($cat=="." || $cat==".." || substr($cat,0,1)=="." || $cat=="eclass" || $cat=="profiles" || $cat=="metadata" || is_file($cat)) continue;
	$pkgs = scandir($cat);
	foreach($pkgs as $pkg){
	    if($pkg=="." || $pkg=="..") continue;
	    $pkglistmaster[]=$cat."/".$pkg;
	}
    }
    return($pkglistmaster
}
*/

function get_dependencys($query){
    global $_CACHE_DEP;
    if(isset($_CACHE_DEP[$query])){
	return($_CACHE_DEP[$query]);
    }
    $txt = shell_exec("equery --no-pipe --no-color depgraph --linear ".$query."::petersweeze");
    echo "e";
    $txt = explode("\n",$txt);
    $pkglist=array();
    foreach($txt as $line){
	$line = trim($line);
	$pos1 = stripos($line,"[  1]");
	$pos2 = stripos($line,"(");
	if($pos1===false || $pos2===false) continue;
	$pkgatom = substr($line,$pos1+5,$pos2-$pos1-5);
	$pkgatom=trim($pkgatom);
	$pkgname=qatom($pkgatom);
	if(in_array($pkgname,$pkglist))continue;
	$pkglist[]=$pkgname;
    }
    $_CACHE_DEP[$query]=$pkglist;
    return($pkglist);
}
function qatom($atom){
    global $_CACHE_ATOM;
    if(isset($_CACHE_ATOM[$atom])){
	return($_CACHE_ATOM[$atom]);
    }
    $pkgname=shell_exec("qatom --format \"%{CATEGORY}/%{PN}\" \"".$atom."\"");
    echo "q";
    $pkgname=trim($pkgname);
    $_CACHE_ATOM[$atom]=$pkgname;
    return($pkgname);
}
