#! /usr/bin/perl -w
use strict;
use subs qw(uppercase_dir);

#   This program is free software developed by M.S.Praveen for Controlnet India
#   Pvt. Ltd; You can redistribute it and/or modify.
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#   Edit history
#   Who            When      Version     What
#   M.S.Praveen    22/02/01  1.0         coded in brute style, no checking
#   M.S.Praveen    23/02/01  1.2         Added messages and warnings
#   M.S.Praveen    23/02/01  1.3         Added restore feature incase if 
#                                        applied to wrong directory
#   D.K.Alcorn     02/04/01  1.4         Made script more "perl-esque" and added 
#                                        recursive feature
#
########################################################################################
# Usage:
# Function :  This program converts the filles in this directory to all upper case.
#             Also created a RESTORE_FILES to restore back to previous state.
#
#     copy this script to you the directory you wanted to convert or copy it to any 
#     directory in your PATH. Execute this file from the directory you wanted to 
#     convert to all upper case.
# 
#     To restore back to previous state execute RESTORE_FILES using any shell
########################################################################################

my $RESTORE_FILES = ".RESTORE_FILES"; # let's hide this file because it's ugly.
my $recurse = 0;					  # convert subdirectories too
my $usage =<<EOF;
Usage:
$0 [-r] [-h]

Converts all filenames in directory to uppercase.  '$RESTORE_FILES' script is
created to restore filenames to original case.

  -r      Convert subdirectories
  -h      Print this message and exit.
EOF

foreach my $arg (@ARGV) {
  if ($arg =~ /-r/) { $recurse = 1; }
  else { print $usage; exit; }
}
	 
print ("Converting file in the current directory to Caps\n");
#check if previous $RESTORE_FILES exist.
if (-e "$RESTORE_FILES") {                    
  print ("restore file exist! Please delete \"$RESTORE_FILES\" and re-run...\n\n");
  print ("Warning : if you delete \"$RESTORE_FILES\" you will not be able to restore\n");
  print ("to the previous file names\n\n");
  print ("Note: To restore back the previous file names execute \n");
  die ("sh $RESTORE_FILES or csh $RESTORE_FILES\n\n"); 
}

my $dir = `pwd`;
chomp $dir;
uppercase_dir $dir, $recurse;
chdir $dir;

print ("\nNote: To restore back the previous file names execute \n");
print ("sh $RESTORE_FILES or csh $RESTORE_FILES\n\n");

sub uppercase_dir {
  my $dir = shift;
  my $recurse = shift;
  chdir $dir or die "Can't change to $dir, $!\n";

  # RESTORE_FILES will have the commands for renaming the files to their original names.
  if (-w '.') {
	 if (-e $RESTORE_FILES && ! -w _) {
		die "Unable to create file \"$dir/$RESTORE_FILES\", $!\n";
	 }
  } else {
	 die "Can't write '$RESTORE_FILES' in '$dir', $!\n";
  }

  # i have to grab all the directory entries this way because of recursion
  # the directory handle isn't unique when this function is called recursively
  opendir DIR, $dir or die "Can't read $dir, $!\n";
  my @entries = readdir DIR;
  closedir DIR;

  my $restore_file;				  # string to store "un-do" commands
  foreach my $filename (@entries) {
	 next if $filename =~ /^\.{1,2}$/;	# skip '.' and '..'
	 next if $filename =~ /$RESTORE_FILES/;
	 if (-d "$dir/$filename" && $recurse) { 
		$restore_file .= "cd $filename\nsh $RESTORE_FILES\n";
		uppercase_dir "$dir/$filename";
	 } else {
		my $filename_upper = $filename;
		$filename_upper =~ tr/a-z/A-Z/;
		if ($filename ne $filename_upper) {
		  print "Convering '$dir/$filename' to '$dir/$filename_upper'\n";
		  $restore_file .= "mv $filename_upper $filename\n";
		  rename $filename, $filename_upper;
		}
	 }
  }

  if ($recurse) { $restore_file .= "cd ..\n"; }

  # printing to the RESTORE_FILE is done this way because of recursion. The filehandle
  # isn't unique if this function is called recursively
  open RESTORE, ">$RESTORE_FILES", or die "Can't write to '$RESTORE_FILES', $!\n";
  print RESTORE $restore_file;
  close RESTORE;
  
  if ($recurse) { chdir "$dir/.."; }
}