#!/usr/bin/perl

use strict;

my $PWD;

if (!defined($ARGV[0])) {
  print "Usage:   perl fclower folder\n";
  exit(0);
}

if (!opendir(TEST, $ARGV[0])) {
  print "Can't open directory $ARGV[0]:$!\n";
  exit(0);
}

lower_dir($ARGV[0]);

sub lower_dir {
  my $dir = shift;
  my $entry;
  my @allentries;

  # move down into the directory
  if (!chdir($dir)) {
    print "Can't cd to directory $dir:$\n";
    exit(0);
  }
  
  # get all entries in the directory
  if (!opendir(DIR, ".")) {
    print "Can't open directory $dir:$\n";
    exit(0);
  }
  @allentries = readdir DIR;

  # go through each entry, if it is a file, rename it, if it is a directory,
  # decsend recursively
  foreach $entry (@allentries) {
    # we want to ignore . and .. entries
    if ($entry eq "." || $entry eq "..") {
      next;
    }
    if (-d $entry) {
      lower_dir($entry);
    }
    rename($entry, lc($entry));  
  }

  # we've got to move back up out of the directory after we are done
  # or recursion won't work
  if (!chdir("..")) {
    print "Can't cd up to parent directory:$!\n";
    exit(0);
  }
}

