#! /usr/bin/perl
# (C)2006 Veit Wahlich <cru@zodia.de>

use strict;
use warnings;

# Where to find the source
my $src_dir=$ENV{SRC_DIR} || './src';

# Where to put the compilation - if not an absolute path, this is relative to $src_dir
my $bin_dir=$ENV{BIN_DIR} || '../bin';

# Save compiled .pm files to .pmc?
my $pm2pmc=1;

# Save compiled .pl files to .plc?
my $pl2plc=1;

# Create HTML documentation?
my $make_html=1;

# Where to put the HTML documentation extracted from the modules - if not an absolute path, this is relative to $src_dir
my $html_dir=$ENV{HTML_DIR} || '../html';

# The root directory used for links inside the HTML documentation
my $html_root=$ENV{HTML_ROOT} || '/cru/psp/docs';

# Full path to the Perl interpreter
my $perl_bin=$ENV{PERL_BIN} || '/usr/bin/perl';

# Full path to pod2html
my $pod2html_bin=$ENV{POD2HTML_BIN} || '/usr/bin/pod2html';

# Temporary directory
my $tmp_dir=$ENV{TMP_DIR} || '../tmp';



sub main{
	my @data;
	
	chdir($src_dir) || die("Unable to change directory to '$src_dir': $!\n");
	
	@data=get_files();
	compile(@data);
	$make_html && htmldocs(@data);
	
}

sub compile{
	my($files,$dirs)=@_;
	
	(-d $bin_dir) || mkdir($bin_dir) || die("Unable to create directory to '$bin_dir': $!\n");
	
	foreach(@{$dirs}){
		(-d $bin_dir.'/'.$_) || mkdir($bin_dir.'/'.$_) || die("Unable to create directory to '$bin_dir/$_': $!\n");
		chmod((stat($_))[2],$bin_dir.'/'.$_);
	}
	
	foreach(@{$files}){
		if(/\.(pm|pl|cgi)$/){
			print('Compiling '.$_.'...');
			if(($pm2pmc && /\.pm$/) || ($pl2plc && /\.pl$/)){
				system($perl_bin,'-MO=-qq,Bytecode,-H,-o'.$bin_dir.'/'.$_.'c',$_) && die("Error compiling '$_'\n");
				chmod((stat($_))[2],$bin_dir.'/'.$_.'c');
			}
			else{
				system($perl_bin,'-MO=-qq,Bytecode,-H,-o'.$bin_dir.'/'.$_,$_) && die("Error compiling '$_'\n");
				chmod((stat($_))[2],$bin_dir.'/'.$_);
			}
			print(" OK\n");
		}
	}
}

sub htmldocs{
	my($files,$dirs)=@_;
	my $ext;
	
	(-d $tmp_dir) || mkdir($tmp_dir) || die("Unable to create directory to '$tmp_dir': $!\n");
	(-d $html_dir) || mkdir($html_dir) || die("Unable to create directory to '$html_dir': $!\n");
	
	foreach(@{$dirs}){
		(-d $html_dir.'/'.$_) || mkdir($html_dir.'/'.$_) || die("Unable to create directory to '$html_dir/$_': $!\n");
	}
	
	foreach(@{$files}){
		if(/\.(pm|pod)$/){
			$ext=$1;
			s/\.(pm|pod)$//;
			print('Creating '.$_.'.html...');
			system('cat '.$_.'.'.$ext.' | '.$perl_bin.' -pe "s/\t/  /g;" | '.$pod2html_bin.' --htmlroot='.$html_root.' --cachedir='.$tmp_dir.' --outfile='.$html_dir.'/'.$_.'.html') && die("Error documenting '$_': $!\n")
				&& die("Error creating '$_'\n");
			print(" OK\n");
		}
	}

}

sub get_files{
	my $dir=shift || '.';
	my $cur_depth=shift || 0;
	my $max_depth=shift || 30;
	my $files=shift || [];
	my $dirs=shift || [];
	if(++$cur_depth > $max_depth){
		print(STDERR "Warning! Maximum depth $max_depth reached.\n");
		return($files,$dirs);
	}
	else{
		foreach(glob($dir.'/*')){
			if(-d $_){
				push(@{$dirs},$_);
				get_files($_,$cur_depth,$max_depth,$files,$dirs);
			}
			else{
				push(@{$files},$_);
			}
		}
		return($files,$dirs);
	}
}

main(@ARGV);
1;