#!/usr/bin/perl

# Based (loosely) on Wall & Schwartz _Programming_Perl_  pg 56.

# Start by doing everything in the current directory.
# Recursive calls to sub dodir {} will handle all subdirs.

&dodir('.');

sub dodir {
	# Each recursion level needs its own local $dir variable, that it
	# can assign to, without affecting its parent's $dir variable.
	# The local() declarations will provide separate variables for each.
	local($dir) = @_;            # Remember the &dodir() argument

	# If you need other variables, declare them like so:
	# local($localvara,$localvarb);   # Declare, but with no defined values

	# Get the list of files in the current directory.
	# Store in the array-variable @filenames, as an array of strings.
	# Remember these will be just filenames, without paths.

	opendir(DIR,'.') || die "Can't open $dir";
	local(@filenames) = readdir(DIR);
	closedir(DIR);

	# put each filename, in turn, into $filename, and deal with it
	foreach $filename (@filenames) {
		next if $filename eq '.';       # eq is string compare
		next if $filename eq '..';

		$fullname = "$dir/$filename";
		# construct full path/file name

		# lstat is like stat, but if the object is a link, lstat checks
		# the link itself, rather than the link's target file.
		lstat($filename);
		
		# Just as there exists the special $_ scalar variable, which lots
		# of Perl functions implicitly assign to or read from, there
		# also exists a special filehandle variable called _ (yes, just
		# an underscore), which retains a handle to the most recently
		# statused file/link.
		
		# Test if the most recently stat'd file is a directory.
		if ( ! ( -d _ ) ) {
			# Handle case of simple file
			# print "File:          $fullname\n";
			# Or you might send a command to the shell, by uncommenting:
			# system( "touch /home/foobar/Mail/$fullname.txt" );
			# change color of background in HTML documents
                          $old = $filename;
                          $new = "jnk.html";
                          open(OLD, "< $old")         or die "can't open $old: $!";
                          open(NEW, "> $new")         or die "can't open $new: $!";
                          while (<OLD>) {
                          # change $_
#                           s/\<body bgcolor=\"\#99CCFF\"/\<body bgcolor=\"\#CCFFFF\"/g;
                            s/pics\/spectra/..\/pics\/spectra/g;
#                           s/supercluster/galaxy supercluster/g;
                          # then print $_ to the new file...
                            print NEW $_             or die "can't write $new: $!";
                          }
                          close(OLD)                 or die "can't close $old: $!";
                          close(NEW)                 or die "can't close $new: $!";
                          unlink($old)               or die "can't remove $old: $!";
                          rename($new, $old)         or die "can't rename $new to $old: $!";
		}
		else {
			# Handle case of directory
			# print "Directory:     $fullname\n";
			# Or you might send a command to the shell, by uncommenting:
			# system( "mkdir /home/foobar/Mail/$fullname.txt" );
			# Then recursively handle the directory's contents...
			chdir $filename || die "Can't cd to $fullname";
			&dodir($fullname);
			chdir '..';
		}
	}
}
