#!/usr/bin/perl -w

# rmcomment - Perl script to remove comments from source codes
# Copyright (c) 2004 rmn <rmn_km yahoo com>
# Distributed under the revised BSD License

# 20040916-20040918

use strict;
use File::Find;

my $ext_c='c|cpp|h|java|js|css';
my $ext_xml='xml|htm|html|xhtml|xul|dtd|rdf';
my $ext_perl='pl|perl|properties';

find(\&do_removal,'.');

sub do_removal(){
	return if !-f;
	
	open(F,$_);
	my $Content=join('',<F>);
	close(F);
	
	# // /*C-style comments*/
	# Only //'s in the front will be checked.
	# There will be problems with /* */ texts in strings.
	
	if(/\.(?:$ext_c)$/){
		$Content=~s!^\s*//.*$!!gm;
		$Content=~s!/\*.*?\*/!!gs;
	}
	
	# <!--XML-style comments-->
	
	elsif(/\.(?:$ext_xml)$/){
		$Content=~s/<!--.*?-->//gs;
	}
	
	# #Perl-style comments
	# Only #'s in the front will be checked.
	# There will be problems with hash signs in here documents.
	
	elsif(/\.(?:$ext_perl)$/){
		$Content=~s/^\s*#.*$//gm;
	}
	
	# Remove all empty lines
	$Content=~s/^\s*\n//s;
	$Content=~s/\n\s*\n/\n/gs;
	
	open(F,">$_");
	print F $Content;
	close(F);
}
