#!/usr/bin/perl
#----------------------------------------------
#
#	bcm - get, compile (with modifications)
#		  package. For use in porting packages
#		  to win32 using minGW/gnu tools.
#
#	Beau E. Cox <beau@aloha.com>
#				<http://www.beaucox.com>
#
#	September 24, 2002
#
#----------------------------------------------

use strict;
use warnings;
use File::Copy;
use File::Path;
use Getopt::Long;
use Pod::Usage;

# - program information
	my %pi = (
		name		=> 'bcm',
		function	=> 'port/make utility',
		author		=> 'Beau E. Cox',
		contact		=> '<beau@aloha.com>',
		version		=> '0.0.1',
		vdate		=> '24 Sep 2002',
		);

# - options 
	my $verbose = 0;
	my $man = 0;
	my $help = 0;
	my $continue_after_error = 0;
	my $building_dll = 0;
	my $using_dll = 0;
	my $building_exe = 0;
	my $command = '';
	my $target_dir = './';
	my $target_name = '';
	my $target_type = '';
	my $target_out_dir = '';
	my $target_obj_dir = '';
	my $target_lib_dir = '';
	my $target_include_dir = '';
	my $package_dir = '';
	my $mod_dir = './mod';
	my $test_dir = './test';
	my $home_dir = './';
	my @excl_dirs = ();
	my @excl_files = ();
	my @c_exts = ('c', );
	my @c_h_exts = ('h', );
	my @cpp_exts = ('cpp', 'cxx', 'c++', );
	my @cpp_h_exts = ('hpp', 'hxx', 'h++', );
	my @other_exts = ();
	my @file_exts = ();
	my @excl_exts = ();
	my @pkg_exts = ();

	my $c_compiler = 'gcc';
	my $cflags = '-Wall -c';
	my $cpp_compiler = 'g++';
	my $cppflags = '-g -O2';
	my @includes = ();
	my @defines = ();
	my @undefines = ();

	my $log_file_name = '';

	GetOptions (
		"verbose|v"					=> \$verbose,
		"manual|man"				=> \$man,
		"help|h|?"					=> \$help,
		"continue-after-error"		=> \$continue_after_error,
		"building_dll|bdll"			=> \$building_dll,
		"using_dll|udll"			=> \$using_dll,
		"building_exe|bexe"			=> \$building_exe,
		"target-dir|td=s"			=> \$target_dir,
		"target-name|tn=s"			=> \$target_name,
		"target-type|tt=s"			=> \$target_type,
		"package-dir|pkg=s"			=> \$package_dir,
		"mod-dir=s"					=> \$mod_dir,
		"test-dir=s"				=> \$test_dir,
		"home-dir=s"				=> \$home_dir,
		"exclude-dirs|xdirs=s"		=> \@excl_dirs,
		"exclude-files|xfiles=s"	=> \@excl_files,
		"c-exts=s"					=> \@c_exts,
		"c-h-exts=s"				=> \@c_h_exts,
		"cpp-exts=s"				=> \@cpp_exts,
		"cpp-h-exts=s"				=> \@cpp_h_exts,
		"other-exts=s"				=> \@other_exts,
		"exclude-exts|xexts=s"		=> \@excl_exts,
		"c_compiler=s"				=> \$c_compiler,  
		"cflags=s"					=> \$cflags,      
		"cpp_compiler=s"			=> \$cpp_compiler,
		"cppflags=s"				=> \$cppflags,    
		"includes=s"				=> \@includes,    
		"defines=s"					=> \@defines,     
		"undefines=s"				=> \@undefines,   
		"log_file_name=s"			=> \$log_file_name,
		) || pod2usage (2);

# - get command
	$command = shift @ARGV;
	$command = 'missing' unless $command;
	if ("dll" =~ m/^$command/) {
		$building_dll = 1;
		$using_dll = 0;
		$building_exe = 0;
		}
	elsif ("exe" =~ m/^$command/ || "lib" =~ m/^$command/) {
		$building_dll = 0;
		$using_dll = 0;
		$building_exe = 1;
		}

	if ($package_dir) {
		$package_dir =~ s{\\}{/}g;
		$package_dir .= '/' unless ($package_dir=~ m{/$});
		}

	$target_dir = './' unless $target_dir;
	$target_dir =~ s{\\}{/}g;
	$target_dir .= '/' unless ($target_dir=~ m{/$});

	$test_dir =~ s{\\}{/}g;
	$test_dir .= '/' unless ($test_dir=~ m{/$});

	$home_dir =~ s{\\}{/}g;
	$home_dir .= '/' unless ($home_dir=~ m{/$});

	$mod_dir = '$target_dir/' unless $mod_dir;
	$mod_dir =~ s{\\}{/}g;
	$mod_dir .= '/' unless ($mod_dir=~ m{/$});

	@excl_dirs = split (/,/, join (',', @excl_dirs));
	for (@excl_dirs) { s{\\}{/}g; $_ .= '/' unless (m{/$}); }
	push @excl_dirs, $test_dir, $mod_dir;
	@excl_files = split (/,/, join (',', @excl_files));

	@c_exts = split (/,/, join (',', @c_exts));
	for (@c_exts) { $_ = '.' . $_ unless (/^\./); }
	@c_h_exts = split (/,/, join (',', @c_h_exts));
	for (@c_h_exts) { $_ = '.' . $_ unless (/^\./); }
	@cpp_exts = split (/,/, join (',', @cpp_exts));
	for (@cpp_exts) { $_ = '.' . $_ unless (/^\./); }
	@cpp_h_exts = split (/,/, join (',', @cpp_h_exts));
	for (@cpp_h_exts) { $_ = '.' . $_ unless (/^\./); }
	@other_exts = split (/,/, join (',', @other_exts));
	for (@other_exts) { $_ = '.' . $_ unless (/^\./); }
	push @file_exts, @c_exts, @c_h_exts, @cpp_exts, @cpp_h_exts, @other_exts;

	@excl_exts = split (/,/, join (',', @excl_exts));
	for (@excl_exts) { $_ = '.' . $_ unless (/^\./); }

	push @defines, 'BUILDING_DLL' if ($building_dll);
	push @defines, 'USING_DLL' if ($using_dll);
	push @defines, 'BC_WIN32';
	unless ($target_type) {
		if ($building_dll || $using_dll) {
			$target_type = 'dll';
			$building_exe = 0;
			}
		else {
			$target_type = 'exe';
			$building_exe = 1;
			}
		}
	$target_out_dir = "$target_dir$target_type/";
	$target_obj_dir = "$target_dir$target_type/obj/";
	$target_lib_dir = "$target_dir$target_type/lib/";
	$target_include_dir = "$target_dir$target_type/include/";
	push @excl_dirs, 'exe', 'dll';
	
	pod2usage (1) if $help;
	pod2usage (-exitstatus => 0, -verbose => 2) if $man;

	$log_file_name || ($log_file_name = "${home_dir}bcm.log");

	_log ("$pi{name} - $pi{function} - version $pi{version}\n" .
		localtime () . "\n", ">$log_file_name");

# - get command
	$_ = $command;
	SWITCH: {
		"get" =~ m/^$_/ && do {
			_get ();
			last SWITCH;
			};
		"compile" =~ m/^$_/ && do {
			_compile ();
			last SWITCH;
			};
		"lib" =~ m/^$_/ && do {
			_lib ();
			last SWITCH;
			};
		"dll" =~ m/^$_/ && do {
			_dll ();
			last SWITCH;
			};
		"clean" =~ m/^$_/ && do {
			_clean ();
			last SWITCH;
			};
		print "invalid or missing command: $_\n";
		pod2usage (2);
		}

sub _get
{
	unless ($package_dir) {
		print "package directory not specified\n";
		pod2usage (2);
		}
	unless (-d $package_dir) {
		print "invalid or non-existent package directory: $package_dir\n";
		pod2usage (2);
		}

	while (1) {
		unless (-d $target_dir) {
			print "target directory $target_dir does NOT exist. Create it? (y/N): ";
			$_ = <STDIN>;
			unless (/^y/i) {
				print ("no target directory\n");
				pod2usage (2);
				}
			eval { mkpath($target_dir) };
			if ($@) {
				print "Couldn't create target directory: $target_dir\n@\n";
				pod2usage (2);
				}
			next;
			}
		last;
		}

	$target_dir =~ s{\\}{/};
	$target_dir .= '/' unless ($target_dir=~ m{/$});

	my $msg = "obtaining package source from $package_dir to $target_dir\n";
	if (@excl_dirs) {
			$msg .= "excluding directories:";
			$msg .= " $_" for (@excl_dirs);
			$msg .= "\n";
			}
	if (@excl_files) {
		$msg .= "excluding files:";
		$msg .= " $_" for (@excl_files);
		$msg .= "\n";
		}
	if (@file_exts) {
		$msg .= "selecting files with extensions:";
		$msg .= " $_" for (@file_exts);
		$msg .= "\n";
		}
	if (@excl_exts) {
		$msg .= "excluding files with extensions:";
		$msg .= " $_" for (@excl_exts);
		$msg .= "\n";
		}
	_log ($msg);

	my (@xd, @xf) = ((), ());
	push (@xd, (/^$package_dir/) ? $_ : $package_dir . $_) for (@excl_dirs);
	push (@xf, (/^$package_dir/) ? $_ : $package_dir . $_) for (@excl_files);

	_traverse ($package_dir,
		{
			exts => \@file_exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_get_item);
}

sub _get_item
{
	my ($dir, $item, $di) = @_;

	my $dst = $di;
	$dst =~ s/$package_dir/$target_dir/;
	$dst .= '.pkg';
	my ($dst_dir) = $dst =~ m{(.*/)};
	$dst_dir || die "unable to extract target subdir\n";
	unless (-d $dst_dir) {
		eval { mkpath($dst_dir) };
		if ($@) {
			print "Couldn't create target subdirectory: $dst_dir\n@\n";
			pod2usage (2);
			}
		}
	_log ("copying $dir to $dst ...\n");
	eval { copy ($di, $dst) };
	$@ && die "Couldn't copy $di to $dst:\n$@\n";
	0;
}

sub _compile
{
	unless ($target_dir) {
		print "target directory not specified\n";
		pod2usage (2);
		}
	$target_dir =~ s{\\}{/}g;
	$target_dir .= '/' unless ($target_dir=~ m{/$});
	unless (-d $target_dir) {
		print "invalid or non-existent target directory: $target_dir\n";
		pod2usage (2);
		}

	unless (-d $target_out_dir) {
		eval { mkpath($target_out_dir) };
		if ($@) {
			print "Couldn't create target out directory: $target_out_dir\n@\n";
			pod2usage (2);
			}
		}
	unless (-d $target_obj_dir) {
		eval { mkpath($target_obj_dir) };
		if ($@) {
			print "Couldn't create target obj directory: $target_obj_dir\n@\n";
			pod2usage (2);
			}
		}
	unless (-d $target_lib_dir) {
		eval { mkpath($target_lib_dir) };
		if ($@) {
			print "Couldn't create target lib directory: $target_lib_dir\n@\n";
			pod2usage (2);
			}
		}
	unless (-d $target_include_dir) {
		eval { mkpath($target_include_dir) };
		if ($@) {
			print "Couldn't create target include directory: $target_include_dir\n@\n";
			pod2usage (2);
			}
		}

	_log ("compiling sources in $target_dir\n");

	my (@xd, @xf) = ((), ());
	push (@xd, (/^$target_dir/) ? $_ : $target_dir . $_) for (@excl_dirs);
	push (@xf, (/^$target_dir/) ? $_ : $target_dir . $_) for (@excl_files);

	my $ret = 0;

	my @exts = _make_package_exts (@c_h_exts, @cpp_h_exts);
	$ret = _traverse ($target_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_build_includes);
	$ret && return $ret;

	$cflags .= ' -c' unless ($cflags =~ m/-c/);
	my $c_cmpl = "$c_compiler $cflags ";
	$c_cmpl .= "-I $_ " for (@includes);
	$c_cmpl .= "-D$_ " for (@defines);
	$c_cmpl .= "-U$_ " for (@undefines);

	$cppflags .= ' -c' unless ($cppflags =~ m/-c/);
	my $cpp_cmpl = "$cpp_compiler $cppflags ";
	$cpp_cmpl .= "-I $_ " for (@includes);
	$cpp_cmpl .= "-D$_ " for (@defines);
	$cpp_cmpl .= "-U$_ " for (@undefines);

	@exts = _make_package_exts (@c_exts, @cpp_exts);
	$ret = _traverse ($target_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_compile_orig_source, $c_cmpl, $cpp_cmpl);
	$ret && return $ret;

	@exts = _make_package_exts (@c_h_exts, @cpp_h_exts);
	$ret = _traverse ($target_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_process_includes);
	$ret && return $ret;

	@exts = _make_package_exts (@c_exts, @cpp_exts);
	$ret = _traverse ($target_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_compile_source, $c_cmpl, $cpp_cmpl);
	$ret && return $ret;

	@exts = (@c_h_exts, @cpp_h_exts);
	$ret = _traverse ($target_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_copy_includes);
	$ret;
}

sub _build_includes
{
	my ($dir, $item, $di) = @_;
	push (@includes, $dir) unless (_contains ($dir, @includes));
	_process_file ($dir, $item, $di, ['nomod']);
}

sub _compile_orig_source
{
	my ($dir, $item, $di, $c_cmpl, $cpp_cmpl) = @_;

	my $pgm = $di;
	$pgm =~ s/\.pkg$//;
	my $ext = $pgm;
	$ext =~ s/.*(\.\w*)/$1/;
	$ext or die "cant't find ext in $pgm\n";
	my $orig = "$di$ext";
	eval { copy ($di, $orig) };
	$@ && die "Couldn't copy $di to $orig:\n$@\n";
	my $obj = "$item";
	die "something wrong making object name from $item\n"
		unless ($item =~ /(.*?)\./);
	$obj = "$target_obj_dir$1.o";
	my $err = $di;
	$err =~ s/\.pkg$/\.err/;

	my $fcmpl = _make_compile_command ($pgm, $c_cmpl, $cpp_cmpl);
	$fcmpl || return 1;

	if (!(-e $err) || ( (stat($err))[9] <= (stat($di))[9] ) ) {
		my $cmd = "$fcmpl -o $obj $orig";
		print "compiling origional $pgm > ";
		_log ("---------------- $obj (origional errors)----\n" .
			"$cmd\n" . localtime () . "\n", ">$err");
		$cmd .= " 2>> $err";
		my $ret = system $cmd;
		my $result = (($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n";
		print $result;
		_log ($result, ">>$err");
		$ret && _append_file_to_log ($err);
		unlink $orig;
		unlink $obj;
		}
	0;
}

sub _process_includes
{
	my ($dir, $item, $di) = @_;

	_process_file ($dir, $item, $di);
}

sub _compile_source
{
	my ($dir, $item, $di, $c_cmpl, $cpp_cmpl) = @_;

	my $pgm = $di;
	$pgm =~ s/\.pkg$//;
	my $obj = "$item";
	die "something wrong making object name from $item\n"
		unless ($item =~ /(.*?)\./);
	$obj = "$target_obj_dir$1.o";

	my $fcmpl = _make_compile_command ($pgm, $c_cmpl, $cpp_cmpl);
	$fcmpl || return 1;

	$fcmpl .= "-o -command-target- -command-source-";
	_process_file ($dir, $item, $di,
		[$fcmpl, $pgm, $obj,]);
}

sub _make_compile_command
{
	my ($src, $c_cmpl, $cpp_cmpl) = @_;

	my $fcmpl;
	if (_has_ext_of ($src, @c_exts)) {
		$fcmpl = $c_cmpl;
		}
	elsif (_has_ext_of ($src, @cpp_exts)) {
		$fcmpl = $cpp_cmpl;
		}
	else {
		_log ("no rule to compile this kind of source\n");
		return '';
		}
	$fcmpl;
}

sub _copy_includes
{
	my ($dir, $item, $di) = @_;

	eval { copy ($di, "$target_include_dir$item") };
	$@ && die "Couldn't copy $di to $target_include_dir$item:\n$@\n";
	0;
}

sub _process_file
{
	my ($dir, $item, $di, $compile) = @_;

	my $target = $di;
	$target =~ s/\.pkg$//;
	my $mod = $item;
	$mod =~ s/\.pkg$/\.mod/;
	$mod = $mod_dir . $mod;
	my $repl = $item;
	$repl =~ s/\.pkg$//;
	$repl = $mod_dir . $repl;

	my $domod = 1;
	$domod = 0 if ($compile && $compile->[0] && $compile->[0] eq 'nomod');

	if (-e $mod && $domod) {
		print "applying $mod to $target\n";
		if (!(-e $target) || ( (stat($target))[9] <= (stat($mod))[9] ) ) {
			my $chg = $di;
			$chg =~ s/\.pkg$/\.chg/;
			my $command = "perl $mod $di $target $repl";
			my $ret = system ("$command > $chg");
			_append_file_to_log ($chg);
			$ret && return $ret;
			}
		}
	else {
		print "copying $di to $target\n";
		if (!(-e $target) || ( (stat($target))[9] < (stat($di))[9] ) ) {
			_vlog ("copying $di to $target ...\n");
			eval { copy ($di, $target) };
			$@ && die "Couldn't copy $di to $target:\n$@\n";
			}
		}

	($compile) || return 0;
	my $command = $compile->[0];
	($command) || return 0;
	my $command_source = $compile->[1];
	($command_source) || return 0;
	my $command_target = $compile->[2];
	($command_target) || return 0;

	if (!(-e $command_target) || ( (stat($command_target))[9] <= (stat($command_source))[9] ) ) {
		# - compile modified (maybe) source
		my $cmd = $command;
		$cmd =~ s/\-command\-target\-/$command_target/;
		$cmd =~ s/\-command\-source\-/$command_source/;
		print "$cmd\n";
		_log ("---------------- $command_target ------------------\n" .
			"$cmd\n" . localtime () . "\n");
		$cmd .= " 2>> $log_file_name";
		my $ret = system $cmd;
		my $result = (($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n";
		print $result;
		_log ($result);
		$ret && return $ret;
		}
	0;
}

sub _clean
{
	unless ($target_dir) {
		print "target directory not specified\n";
		pod2usage (2);
		}
	unless (-d $target_dir) {
		print "invalid or non-existent target directory: $target_dir\n";
		pod2usage (2);
		}

	$target_dir =~ s{\\}{/};
	$target_dir .= '/' unless ($target_dir=~ m{/$});

	my (@files, @xd, @xf) = ((), (), ());
	push @files, @c_exts, @c_h_exts, @cpp_exts, @cpp_h_exts,
		('.o', '.a', '.lib', '.dll', '.exe', '.err', '.script', '.def');
	for (@excl_dirs) {
		if ($building_exe) {
			next if (m{/exe/$});
			}
		else {
			next if (m{/dll/$});
			}
		push (@xd, (/^$target_dir/) ? $_ : $target_dir . $_);
		}
		
	push (@xf, (/^$target_dir/) ? $_ : $target_dir . $_) for (@excl_files);
		

	my $msg = "clean - removing files with the following extensions from $target_dir\n";
	$msg .= "$_ " for (@files);
	print ("$msg\n");
	_log ("$msg\n");

	my $ret = _traverse ($target_dir,
		{
			exts => \@files,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_remove_item);
	print (($ret) ? "FAILURE\n" : "SUCCESS\n");
}

sub _remove_item
{
	my ($dir, $item, $di) = @_;

	unlink $di;
	_log ("$di deleted\n");
	0;
}

sub _lib
{
	unless ($target_name) {
		print "no target name (-target_name xxx -or- -tn xxx) specified\n";
		pod2usage (2);
		}

	my $ret = _compile ();
	$ret && return $ret;

	my $ftn = "$target_lib_dir$target_name.a";

	_log ("creating $ftn archive\n");

	my $arsn = "$ftn.script";
	my $arsh;
	open ($arsh, ">$arsn") or die "unable to open $arsn\n$!\n";
	print $arsh "create $ftn\n";

	my (@xd, @xf) = ((), ());
	push (@xd, (/^$target_dir/) ? $_ : $target_dir . $_) for (@excl_dirs);
	push (@xf, (/^$target_dir/) ? $_ : $target_dir . $_) for (@excl_files);

	my @exts = ('.o');
	$ret = _traverse ($target_obj_dir,
		{
			exts => \@exts,
			excl_dirs => \@xd,
			excl_files => \@xf,
			excl_exts => \@excl_exts,
		},
		\&_build_ar_script, $arsh);
	$ret && return $ret;

	print $arsh "save\n";
	print $arsh "end\n";
	close $arsh;

	unlink $ftn;
	print "building library: ar -M < $arsn\n";
	$ret = system "ar -M < $arsn 2>> $log_file_name";
	system "ar -t $ftn >> $log_file_name";
	_log ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	print ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	$ret;
}

sub _build_ar_script
{
	my ($dir, $item, $di, $arsh) = @_;

	if (_obj_has_main ($di)) {
		_log ("skipping $di because function 'main' is defined\n");
		return 0;
		}

	print $arsh "addmod $di\n";
	0;
}

sub _dll
{
	unless ($target_name) {
		print "no target name (-target_name xxx -or- -tn xxx) specified\n";
		pod2usage (2);
		}

	my $ret = _lib ();
	$ret && return $ret;

	my $dlln = "$target_out_dir$target_name.dll";
	my $inpn = "$target_lib_dir$target_name.a";
	my $expn = "$target_out_dir$target_name.lib";
	my $defn = "$target_out_dir$target_name.def";

	_log ("creating dll: $dlln from $inpn including:\n" .
		" export library: $expn and defintion file: $defn\n");


	unlink $dlln;
	unlink $expn;
	unlink $defn;
	my $command = "dllwrap --output-lib $expn --dllname $dlln ";
	$command .= "--driver-name g++ --output-def $defn $inpn"; 
	print ("$command\n");
	_log ("$command\n");
	$ret = system "$command 2>> $log_file_name";
	$ret || _append_file_to_log ($defn);
	_log ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	print ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	$ret && return $ret;

	$command = "lib /def:$defn /out:$expn";
	print ("$command\n");
	_log ("$command\n");
	$ret = system "$command 2>> $log_file_name";
	_log ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	print ((($ret) ? "ERROR:" : "SUCCESS:") . " $ret\n");
	$ret;
}

sub _traverse
{
	my $dir = shift;
	my $rules = shift;
	my $processor = shift;
	my @processor_parms = @_;
	$dir .= '/' unless $dir =~ m{/$};

	for (@{ $rules->{excl_dirs} }) { return if ($dir =~ m/^$_$/); }

	opendir (DIR, $dir) ||
		die "unable to open directory $dir: $!\n";
	my @contents = readdir (DIR);
	closedir (DIR);

	for my $item (sort @contents) {
		my $di = "$dir$item";
		next unless (-d $di);
		next if $item =~ m/^\.$|^\.\.$/;			
		my $ret = _traverse ($di, $rules, $processor, @processor_parms);
		($ret && return $ret) unless ($continue_after_error);
		}

	CONTENT: for my $item (sort @contents) {
		my $di = "$dir$item";
		next if (-d $di);
		_has_ext_of ($item, @{ $rules->{exts} }) || next;
		for (@{ $rules->{excl_exts} }) { next CONTENT if (_has_ext_of ($item, $_)); }
		for (@{ $rules->{excl_files} }) { next CONTENT if ($di =~ m/^$_$/); }
		for (@{ $rules->{excl_files} }) { next CONTENT if ($item =~ m/^$_$/); }
		my $ret = &$processor ($dir, $item, $di, @processor_parms);
		($ret && return $ret) unless ($continue_after_error);
		}
	0;
}

sub _vlog
{
	$verbose && _log (@_);
}

sub _log
{
	my ($msg, $fn) = @_;
	$fn || ($fn = ">>$log_file_name");
	open (LOG, "$fn") or
		die "unable to open $fn\n$!\n";
	print LOG $msg;
	close LOG;
	$verbose && print $msg;
}

sub _append_file_to_log
{
	my ($infn, $fn) = @_;
	open (IN, "<$infn") or
		die "unable to open $infn\n$!\n";
	$fn || ($fn = ">>$log_file_name");
	open (LOG, "$fn") or
		die "unable to open $fn\n$!\n";
	while (<IN>) {
		print LOG $_;
		}
	close IN;
	close LOG;
}

sub _make_package_exts
{
	my @ret = ();
	for (@_) {
		next unless $_;
		push @ret, "$_.pkg";
		}
	@ret;
}

sub _has_ext_of
{
	my $string = shift;
	my $slen = length $string;

	for (@_) {
		next unless $_;
		my $xlen = length $_;
		next if ($xlen >= $slen);
		my $substr = substr $string, -$xlen;
		return 1 if ($_ eq $substr);
		}
	0;
}

sub _contains
{
	my $item = shift;
	for (@_) {
		next unless $_;
		return 1 if ($_ eq $item);
		}
	0;
}

sub _obj_has_main
{
	_obj_has_symbol (shift, 'T\s+_main');
}

sub _obj_has_symbol
{
	my ($obj, $symbol) = @_;

	system ("nm $obj > __bcm_temp__");
	open (OBJ, "<__bcm_temp__") || die "can't open result of 'nm $obj'\n\$!\n";
	my $has_symbol = 0;
	for (<OBJ>) {
		if (/$symbol/) {
			$has_symbol = 1;
			last;
			}
		}
	close OBJ;
	unlink "__bcm_temp__";
	$has_symbol;
}

__END__

=head1 NAME

sample - Using GetOpt::Long and Pod::Usage

=head1 SYNOPSIS

 bcm command [options] [file ...]

 Commands:
  g[et]            retrieve a package
  c[ompile]        compile/port the package

 Options:
  -help            brief help message
  -man             full documentation

=head1 OPTIONS

=over 4

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B<This program> will read the given input file(s) and do someting
useful with the contents thereof.

=cut

