#!/usr/bin/php
<?php
$dir = opendir("/etc/portage-reinstall.d");
while($file=readdir($dir)){
    if($file=="." || $file=="..") continue;
    if($file==".keep") continue;
    if(substr($file,-4)!=".php") continue;
    include("/etc/portage-reinstall.d/".$file);
}
closedir($dir);

$reinstalls=array();
$reconfigs=array();

foreach($install_order as $item){
    echo "Checking reinstall of ".$item["reinstall"]." if ".$item["must_prev_installed"]." is later installed... ";
    $itime1 = get_install_time($item["reinstall"]);
    $itime2 = get_install_time($item["must_prev_installed"]);
    if($itime1<1){
	echo $item["reinstall"]." is not installed on system!\r\n";
	continue;
    }
    if($itime2<1){
	echo $item["must_prev_installed"]." is not installed on system!\r\n";
	continue;
    }
    $deltatime = $itime2-$itime1;
    $fstime_file = str_replace("/","_",$item["reinstall"]);
    $fstime_reconfig = @filemtime("/var/lib/portage-reinstalls/".$fstime_file);
    $deltatime_reconfig=$itime2-$fstime_reconfig;
    if($deltatime_reconfig<0 && isset($item["only_reconfig"])){
	$deltatime_reconfig*=-1;
	echo "ok, ".$item["reinstall"]." was reconfigured ".$deltatime_reconfig."s after ".$item["must_prev_installed"]."!\r\n";
	continue;
    }
    elseif($deltatime<0){
	$deltatime*=-1;
	echo "ok, ".$item["must_prev_installed"]." was installed ".$deltatime."s before ".$item["reinstall"]."!\r\n";
	continue;
    }
    elseif($deltatime>0){
        if(isset($item["only_reconfig"]) && $item["only_reconfig"]){
        	echo "MUST RECONFIG!, ".$item["must_prev_installed"]." was installed ".$deltatime."s after ".$item["reinstall"]."!\r\n";
        	if(!in_array($item["reinstall"],$reconfigs)){
        	    $reconfigs[]=$item["reinstall"];
        	}
        	continue;
        }
        else{
        	echo "MUST REINSTALLED!, ".$item["must_prev_installed"]." was installed ".$deltatime."s after ".$item["reinstall"]."!\r\n";
        	if(!in_array($item["reinstall"],$reinstalls)){
        	    $reinstalls[]=$item["reinstall"];
        	}
        	continue;
        }
    }
}

$didit=false;
if(count($reconfigs)>0){
    $buildstr = "emerge --config";
    foreach($reconfigs as $reinstall){
	$qwhich = "=`qwhich -I -p -v -l ".$reinstall."`";
	$buildstr.=" ".$qwhich;
    }
    if(stripos($buildstr,"sys-kernel/pw-kernel")!==false){
    	echo "sys-kernel/pw-kernel detected: mounting boot partitions rw...\r\n";
    	if(file_exists("/boot/efi")) shell_exec("mount -o remount,rw /boot/efi");
    	if(file_exists("/boot/firmware")) shell_exec("mount -o remount,rw /boot/firmware");
	@mkdir("/run/needrestart");
	@mkdir("/run/needrestart/system");
	@touch("/run/needrestart/system/mount-boot-efi");
	@touch("/run/needrestart/system/mount-boot-firmware");
    }
    echo "\r\nCalling in 5s: ".$buildstr."\r\n\r\n";
    sleep(5);
    $resultcode=-1;
    passthru($buildstr,$resultcode);
    if($resultcode==0){
    	foreach($reconfigs as $reinstall){
    		$reinstall = str_replace("/","_",$reinstall);
	    	if(!is_dir("/var/lib/portage-reinstalls")) mkdir("/var/lib/portage-reinstalls");
	    	touch("/var/lib/portage-reinstalls/".$reinstall);
	    	echo "touching /var/lib/portage-reinstalls/".$reinstall."...\r\n";
    	}
    }
    echo "DONE [result=".$resultcode."]\r\n\r\n";
    $didit=true;
}
if(count($reinstalls)>0){
    $buildstr = "emerge --oneshot";
    foreach($reinstalls as $reinstall){
	$buildstr.=" ".$reinstall;
    }
    if(stripos($buildstr,"sys-kernel/pw-kernel")!==false){
    	echo "sys-kernel/pw-kernel detected: mounting boot partitions rw...\r\n";
    	if(file_exists("/boot/efi")) shell_exec("mount -o remount,rw /boot/efi");
    	if(file_exists("/boot/firmware")) shell_exec("mount -o remount,rw /boot/firmware");
    }
    echo "\r\nCalling in 5s: ".$buildstr."\r\n\r\n";
    sleep(5);
    passthru($buildstr);
    echo "\r\nDONE\r\n\r\n";
    $didit=true;
}
if(!$didit){
    echo "Nothing TODO!\r\n\r\n";
}


function get_install_time($pkg){
    $exec = shell_exec("qlist --installed --verbose =".$pkg);
    $exec = trim($exec);
    if(empty($exec)) return(0);

    $itime=0;

    $exec = explode("\n",$exec);
    foreach($exec as $atom){
	$atom = trim($atom);

	$pkgdir="/var/db/pkg/";
	$time = filemtime($pkgdir."/".$atom);
	if($time>$itime) $itime=$time;
    }
    return($itime);
}
