#!/usr/bin/perl -w

#written: 20050129
#status : seems to work

$help[0] = "rmemptydirs - remove empty directories                                ";
$help[1] = "syntax: rmemptydirs [-r] [-v] <directoryname>                         ";
$help[2] = "option -r makes it recurse over subdirectories.                       ";
$help[3] = "option -v makes it verbose (for debugging rmemptydirs).               ";
$help[4] = "if -r is used and a directory contains a symlink to another directory,";
$help[5] = "  then this symlink is not followed.                                  ";
$help[6] = "if <directoryname> itself is empty, it is not removed.                ";

# utils :

sub ph { foreach $help (@help) { print("$help\n"); } }
sub es { print("Error: ",@_,"\n  $!\n"); }
sub v  { my( $msg ); if( $verbose ){ foreach $msg (@_) { print($msg, "\n"); } } }

# get arguments :

$verbose = 0 ; $recurse = 0 ;
foreach $arg (@ARGV) {
  if(    $arg eq "-v" ){ $verbose = 1 ; }
  elsif( $arg eq "-r" ){ $recurse = 1 ; }
  elsif( -d $arg      ){ $dirname = $arg ; }
  else                 { print("syntaxerror: unrecognized argument $arg\n");
			 ph(); exit(0); }
  }
unless( defined( $dirname )){
  print("syntaxerror: no directoryname specified\n"); ph(); exit(0); }

# subs :

sub isemptydir { # returns 1 if decidedly empty, 0 otherwise
  my( $dir, @lines );
  $dir = $_[0] ;
  unless( opendir(DIRH, $dir) ){ es("cant read $dir"); return(0); }
  $/="\n"; @lines = readdir(DIRH); closedir( DIRH );
  if( @lines ){ v("read $dir"); }
  else        { es("can't read from $dir"); return(0); }

  foreach $line (@lines) {
    if( $line eq "."     ){ next ; }
    if( $line eq ".."    ){ next ; }
    return(0);
    }
  return(1);
  }

sub dodir { # returns 1 if decidedly empty, 0 otherwise
  my( $dir, @lines, $fullname, $count );
  $dir = $_[0] ;
  unless( opendir(DIRH, $dir) ){ es("cant read $dir"); return(0); }
  $/="\n"; @lines = readdir(DIRH); closedir( DIRH );
  if( @lines ){ v("read $dir"); }
  else        { es("can't read from $dir"); return(0); }

  $count = 0 ;
  foreach $line (@lines) {
    if( $line eq "."     ){ next ; }
    if( $line eq ".."    ){ next ; }
    $fullname = $dir . "/" . $line ; $count++ ;
    if(     -l $fullname ){ next ; }
    unless( -d $fullname ){ next ; }
    if( ! $recurse ){
      if( isemptydir( $fullname ) ){
        if(0== system("rmdir $fullname") ){ v("removed empty dir $fullname"); }
        else                           { es("couldn't remove empty dir '$fullname'"); }
        }
      else                             { v("non-empty dir: $fullname"); }
      }
    else { # recurse
      if( dodir( $fullname ) ){
        if(0== system("rmdir $fullname") ){ v("removed empty dir $fullname"); $count-- ; }
        else                           { es("couldn't remove empty dir '$fullname'"); }
        }
      else                             { v("non-empty dir: $fullname"); }
      }
    }
  if( $count > 0 ){ return(0); } else { return(1); }
  }

# main :

if( substr( $dirname, 0, 1 ) eq "/" ){ $fulldirname = $dirname ; }
else { $pwd = `/bin/pwd` ; chomp( $pwd ); $fulldirname = $pwd . "/" . $dirname ; }

dodir( $fulldirname );
exit(0);
