#! /usr/bin/perl 

%allfiles = ( );

if ($#ARGV != 0) {
    die ("One argument please");
}

$project = $ARGV[0];
($progname = $0) =~ s/.*\///;

print $progname,": cleaning project ",$project,"\n";

open(ALLFILES, "find . -print|") || die ("find failed");

while (<ALLFILES>) {
    chop;
    $allfiles{$_} = 1;
}

wait;

die ("find failed") if ($? != 0);

delete $allfiles{"."};

open(PRCSFILES, "prcs execute $project ./ |") || die ("prcs failed");

while (<PRCSFILES>) {
    chop;
    delete $allfiles{"./" . $_};
}

wait;

die ("prcs failed") if ($? != 0);

delete $allfiles{"./" . $project . ".prj"};
delete $allfiles{"./." . $project . ".prcs_aux"};

foreach $key (keys %allfiles) {
    if(-d $key) {
        print $progname,": deleting directory $key:\n";
        system "rm", "-rv", $key;
    }
    elsif(-e $key or -l $key) {
        print $progname,": deleting ",$key,"\n";
        unlink ($key) || warn ("unlink failed on $key");
    }
}
