#!/usr/bin/perl

use strict;

foreach (@ARGV){

print "killin empty directories under \/$_\n";
if(deleteemptydir($_)==1){
	print "deleting top dir $_\n";
	rmdir("$_") || die "cannot rmdir $_";
	}
}

###############################################################################

sub deleteemptydir {
my ($dirpath)= @_;
#print "CLEANING: $dirpath\/\n";
opendir(DIR, $dirpath) or return;

my @filelist=readdir(DIR);

closedir(DIR);
my $numoffiles=0;
my $i;
foreach $i (@filelist) {
	next if (($i eq ".") or ($i eq ".."));
	if ($i eq "incoming"){
		$numoffiles=$numoffiles +1;
		next;
		}
	my $f=$dirpath."\/".$i;
	if (-d $f) {
		if(deleteemptydir($f)==1){
			print "deleting dir $f\n";
			rmdir("$f") || die "cannot rmdir $f";
			}
		else {
			$numoffiles = $numoffiles +1;
			}
		}
	else {
		$numoffiles = $numoffiles +1;
		}
	}
if ($numoffiles == 0) {
	return 1;
	}
else {
	return 0;
	}
}

