#!/usr/local/bin/php -q
<?php

/*
 * Delete old files and libraries from FreeBSD base install
 * Adopted from http://freebsd.org/cgi/cvsweb.cgi/~checkout~/src/Makefile
 * "make delete-old && make delete-old-libs"
 */

$source    file('http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/ObsoleteFiles.inc');
$OLD_FILES = array();
$OLD_LIBS  = array();
$OLD_DIRS  = array();

foreach (
$source as $line) {
    if (
preg_match('/^OLD_FILES\+=(.*)/'$line$match)) {
        
array_push($OLD_FILES$match[1]);
    }
    if (
preg_match('/^OLD_LIBS\+=(.*)/'$line$match)) {
        
array_push($OLD_LIBS$match[1]);
    }
    if (
preg_match('/^OLD_DIRS\+=(.*)/'$line$match)) {
        
array_push($OLD_DIRS$match[1]);
    }
}

print 
"Removing old files\n";
foreach (
$OLD_FILES as $file) {
    if (
is_file('/'$file)) {
        print 
"Removing schg flag on /"$file ."\n";
        
exec('chflags noschg /'$file);
        
exec('rm -i /'$file);
    }
}
print 
"Old files removed\n\n";

print 
"Removing old libraries\n";
foreach (
$OLD_LIBS as $lib) {
    if (
is_file('/'$lib)) {
        print 
"Removing schg flag on /"$lib ."\n";
        
exec('chflags noschg /'$lib);
        
exec('rm -i /'$lib);
    }
}
print 
"Old libraries removed\n\n";

print 
"Removing old directories\n";
foreach (
$OLD_DIRS as $dir) {
    if (
is_dir('/'$dir)) {
        
exec('rmdir -v /'$dir);
    }
}
print 
"Old directories removed\n";