#!/bin/php
<?php
echo "Claim flock... ";
$fp_lock = fopen("/var/run/update_prefetch_binpkgs_delta.lock","w+");
flock($fp_lock,LOCK_EX);
echo "done\r\n";
$repeatcounter=0;
echo "Retrieve urls for fetching binary packages...\r\n";
shell_exec("emaint -f binhost 2>&1");
$fetchdata = fetch_data();
echo "We need to fetch ".count($fetchdata["urls"])." urls to get packages of ".$fetchdata["size"]." KiB\r\n";
echo "Start fetching...\r\n";
$cnt_success=0;
$cnt_wget=0;
$cnt_bytes_data=0;
$cnt_bytes_overhead=0;
$cnt_bytes_wget=0;
foreach($fetchdata["urls"] as $url){
    while(true){
        $success_one=false;
        $call_wget=false;
        echo "Calling ".$url."...\r\n";
        $cmd = shell_exec("diffget.php ".$url);
        echo $url.":\r\n";
        $cmdlines = explode("\n",$cmd);
        foreach($cmdlines as $cmdline){
        	$cmdline = trim($cmdline);
        	echo "\t".$cmdline."\r\n";

            if(stripos($cmdline,"Package already available! DONE!")!==false){
                $cnt_success++;
                $success_one=true;
            }
            if(stripos($cmdline,"Successfully delivered")!==false){
                $cnt_success++;
                $success_one=true;
            }
        	if(stripos($cmdline,"Server overhead: ")!==false){
        	    $exp = explode(": ",$cmdline);
        	    $cnt_bytes_overhead+=$exp[1];
        	}
        	if(stripos($cmdline,"Transferred Data Size: ")!==false){
        	    $exp = explode(": ",$cmdline);
        	    $cnt_bytes_data+=$exp[1];
        	}
        	if(stripos($cmdline,"Optimize under 1% - please load original file!")!==false){
        	    $success_one=true;
                $call_wget=true;
        	}
        	if(stripos($cmdline,"No same packages on srv and local! DONE!")!==false){
        	    $success_one=true;
                $call_wget=true;
        	}
        	if(stripos($cmdline,"We dont have a local package - must do a complete download")!==false){
        	    $success_one=true;
                $call_wget=true;
        	}
        }
        if($success_one){
                if($call_wget){
                    echo "DONE OK (before wget): ".$url." -> start wget download...\r\n";
                    $cwd = getcwd();
                    chdir("/tmp");
                    echo "Downloading ".$url."...\r\n";
                    $explode_url = explode("/",$url);
                    $explode_filename=$explode_url[count($explode_url)-1];
                    $explode_pkgname=$explode_url[count($explode_url)-2];
                    $explode_cat=$explode_url[count($explode_url)-3];
                    if(file_exists($explode_filename)){
                        unlink($explode_filename);
                    }
                    $retcode_wget=-1;
                    passthru("wget ".$url,$retcode_wget);
                    if($retcode_wget!=0){
                        echo "WGET RETCODE==".$retcode_wget." - must be 0 - DIE\r\n";
                        exit(1);
                    }
                    $size = filesize($explode_filename);
                    if($size>0){
                        $cnt_wget++;
                        $cnt_bytes_wget+=$size;
                    }
                    else{
                        echo "Filesize==0 - something wrong :(\r\n";
                        exit(1);
                    }
                    $target_filename="/var/cache/binpkgs/".$explode_cat."/".$explode_pkgname."/".$explode_filename;
                    $target_directory="/var/cache/binpkgs/".$explode_cat."/".$explode_pkgname;
                    $target_filename_use=$target_filename;
                    $filename_counter=1;
                    while(true){
                        if(file_exists($target_filename_use)){
                            $target_filename_use=str_replace(".gpkg.tar",$filename_counter.".gpkg.tar",$target_filename);
                            $filename_counter++;
                        }
                        else{
                            break;
                            if($filename_counter>1000){
                                echo "ENDLESS LOOP DETECTED - DIE :(\r\n";
                                exit(1);
                            }
                        }
                    }

                    echo "placing package in ".$target_filename_use."...\r\n";
                    @mkdir($target_directory,0755,true);
                    copy($explode_filename,$target_filename_use);
                    touch($target_filename_use,filemtime($explode_filename));
                    unlink($explode_filename);


                    chdir($cwd);
                    echo "DONE OK (after wget): ".$url." -> goto next...\r\n";
                }
                else{
                    echo "DONE OK (w/o wget): ".$url." -> goto next...\r\n";
                }

                break;
        }
        else{
                if($repeatcounter>(4*24)){ // Try it 24 hours long...
                    echo "DONE NOK: ".$url." -> goto next, because 24hours trying without success...\r\n";
                    break;
                }
                else{
                    echo "DONE NOK: ".$url." -> repeat in 15min...\r\n";
                    sleep(60*15);
                    $repeatcounter++;
                }
        }
    }
}

if($cnt_success>0 || $cnt_wget>0){
    shell_exec("emaint -f binhost 2>&1");
    shell_exec("eclean --time-limit=100y packages -u 2>&1");
    $fetchdata_after = fetch_data();
}
else{
    $fetchdata_after=$fetchdata;
}
echo "Statistics:\r\n";
echo "Packages Total before (emerge): ".count($fetchdata["urls"])."\r\n";
echo "Packages Total after (emerge): ".count($fetchdata_after["urls"])."\r\n";
echo "Remaining Transfer before (emerge): ".round($fetchdata["size"],0)." KiB\r\n";
echo "Remaining Transfer after (emerge): ".round($fetchdata_after["size"],0)." KiB\r\n";
echo "Packages Successfully for diffget.php: ".$cnt_success."\r\n";
echo "Packages Successfully for wget: ".$cnt_wget."\r\n";
echo "Packages Successfully for emerge: ".(count($fetchdata["urls"])-count($fetchdata_after["urls"]))."\r\n";
echo "Decreased KiB for emerge: ".round(($fetchdata["size"]-$fetchdata_after["size"])/1,0)." KiB\r\n";
echo "Invested delta files: ".round($cnt_bytes_data/1024,0)." KiB (".$cnt_bytes_data.")\r\n";
echo "Invested overhead requests: ".round($cnt_bytes_overhead/1024,0)." KiB (".$cnt_bytes_overhead.")\r\n";
echo "Invested wget: ".round($cnt_bytes_wget/1024,0)." KiB (".$cnt_bytes_wget.")\r\n";
echo "Invested total: ".round(($cnt_bytes_overhead+$cnt_bytes_data+$cnt_bytes_wget)/1024,0)." KiB\r\n";
echo "Saved transfer: ".( round(($fetchdata["size"]-$fetchdata_after["size"])/1,0) - round(($cnt_bytes_overhead+$cnt_bytes_data+$cnt_bytes_wget)/1024,0) )." KiB\r\n";
if(round(($fetchdata["size"]-$fetchdata_after["size"])/1,0)>100){
    echo "Saved transfer: ".round(( round(($fetchdata["size"]-$fetchdata_after["size"])/1,0) - round(($cnt_bytes_overhead+$cnt_bytes_data+$cnt_bytes_wget)/1024,0) ) / round(($fetchdata["size"]-$fetchdata_after["size"])/1,0) *100,0)." %\r\n";
}
fclose($fp_lock);
echo "DONE!\r\n";

function fetch_data(){
    $exec = shell_exec("emerge --update --deep --newuse --autounmask --autounmask-continue --rebuilt-binaries --fetchonly --pretend --verbose world");
    $exec = explode("\n",$exec);
    $part_urls=false;
    $size=-1;
    $urls=array();
    foreach($exec as $line){
	$line=trim($line);
	if(substr($line,0,7)=="Total: "){
	    $part_urls=true;
	    $exp = explode("Size of downloads: ",$line);
	    $size=$exp[1];
	    $size = str_replace(" KiB","",$size);
	    $size = str_replace(".","",$size);
	}
	elseif($part_urls){
	    if(!empty($line)){
		if(substr($line,0,4)!="http") continue;
		if(stripos($line,".gpkg.tar")===false) continue;
		$urls[]=$line;
	    }
	}
    }
    if($size==-1){
	echo "Can't determine size :(\r\n";
	exit(1);
    }
    return(array("size"=>$size,"urls"=>$urls));
}
