#!/usr/local/bin/perl

###############################################################################
#							STRIP.PL
#
# Version: 1.0
# Last Updated: 26 NOV 98
#
# This program was written by Michael E. Schechter (mschechter@earthlink.net).
#
# You may copy this under the terms of the GNU General Public
# License or the Artistic License which is distributed with
# copies of Perl v5.x for UNIX.
#
# This script will strip comments and blank lines out of a Perl script.  Note
# that it only strips comments out that start at the beginning of a line, with
# only white space between the new line and the beginning of the comment.  It
# will not remove the shebang ( #! ) at the beginning of the Perl script.
#
# To use this script, run it from the command line, providing the directories
# containing files that end in the .pl extension that you would like to strip
# comments and blank lines out of.  Do not include trailing slashes.
#
# For instance, on a Unix system, type:
#
#			strip.pl "/cgi-bin/my/dir" "/cgi-bin/my/next/dir"
#
# On a Windows system, type:
#
#			perl strip.pl "c:\temp" "c:\temp\dir"
#
# Each file ending in .pl in the directories you specify will have their
# comments and blank lines stripped out.  The script will provide a report.txt
# file in each directory that it goes into, to tell you what files it modified.
# You will also be provided with a report at runtime after each directory has
# been processed.  The original file will still remain, with a .commented
# added to it.
###############################################################################

foreach $path_dir (@ARGV) {
	$path_dir =~ s/\\/\//g;
	opendir(DIR, $path_dir);
	@files_to_strip = grep { /\.pl$/i } readdir (DIR);
	closedir(DIR);

	foreach $file (@files_to_strip) {
    	open (STRIPFILE, "$path_dir/$file") || die "$!, stopped";
	    open (TEMPFILE, ">$path_dir/temp.txt") || die "$!, stopped";

	    while (<STRIPFILE>) {
    	    $_ =~ s/\r//g;
        	if (/^\#\!/) {
            	print TEMPFILE $_;
	        }
    	    unless (/^(\s)*\#(.*)\n/ || /^\n/) {
        	    print TEMPFILE $_;
	        }
    	}

	    close (STRIPFILE) || die "$!, stopped";
    	close (TEMPFILE) || die "$!, stopped";
	    rename ("$path_dir/$file", "$path_dir/$file.commented") || die "$!, stopped";
    	rename ("$path_dir/temp.txt", "$path_dir/$file") || die "$!, stopped";
	}

	open (REPORTFILE, ">$path_dir/report.txt") || die "$!, stopped";
	print REPORTFILE "The following files were processed\:\n";
	foreach $file (@files_to_strip) {
    	print REPORTFILE "$path_dir/$file\n";
	}
	close (REPORTFILE);

	open (REPORTFILE, "$path_dir/report.txt") || die "$!, stopped";
	while(<REPORTFILE>) {
		print "$_";
	}
	close (REPORTFILE);
}
