#!/usr/bin/perl

#
#   artemus - HTML (and other things) Preprocessor
#
#   Copyright (C) 2000/2009 Angel Ortega <angel@triptico.com>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#   http://www.triptico.com
#

use locale;
use Getopt::Long;

use Artemus;

# substitution variables
%vars = ();

# substitution functions
%funcs = ();

# source and destination files
$src = '-';
$dst = '>-';

# config file
$config_file = 'artemus.conf';

# local configuration file
$local_config_file = 'local-artemus.conf';

# paragraph separator
$para_sep = '';

# use cr/lf instead of lf
$use_cr_lf = 0;

# append instead of overwrite output file
$append = 0;

# send files using ftp
$use_ftp = 0;

# build site map
$site_map = 0;

# quiet flag
$quiet = 0;

# inverse substitutions
$inverse_config_file = 'artemus-inv.conf';
%inv_vars = ();

# include path
$include_path = '';

# include debugging info
$debug = 0;

# unresolved templates
@unresolved = ();

#####################################################################

# special utility functions
$funcs{'filesize'}	= sub { -s $_[0] };
$funcs{'shell'}		= sub { $_=`$_[0]`; chomp; return $_ };

# id
$VERSION = $Artemus::VERSION;
$artemus_id = "Artemus $VERSION";

usage() if (!GetOptions('i|input=s'		=> \$src,
			'o|output=s'		=> \$dst,
			'c|conf=s'		=> \$config_file,
			'p|paragraph=s'		=> \$para_sep,
			'm|msdos'		=> \$use_cr_lf,
			'a|append'		=> \$append,
			'v|version-only'	=> \$version_only,
			'q|quiet'		=> \$quiet,
			'l|include-path=s'	=> \$include_path,
			'd|debug'		=> \$debug,
			'h|help'		=> \$help) or $help);

$dst = '>-' if $dst eq '-';

if ($version_only) {
	print("$VERSION\n");
	exit 0;
}

# read the configuration files
if (-f $config_file) {
	read_config($config_file);
}

if (-f $local_config_file) {
	read_config($local_config_file);
}

if (-f $inverse_config_file) {
	read_config($inverse_config_file, 1);
}

open F, $src or die "can't open '$src'";

# get all the file
$data = join('', <F>);
close F;

# create the Artemus handle
$ah = Artemus->new('paragraph-separator'	=> $para_sep,
		'vars'				=> \%vars,
		'inv-vars'			=> \%inv_vars,
		'funcs'				=> \%funcs,
		'include-path'			=> $include_path,
		'use-cr-lf'			=> $use_cr_lf,
		'unresolved'			=> \@unresolved,
		'debug'				=> $debug
	      );

# do it
$data = $ah->process($data);

# save file

open F, ($append ? '>' : '') . ">$dst" or die "can't write '$dst'";
print F $data;
close F;

foreach my $t (@unresolved) {
	print STDERR "Artemus: unresolved '$t'\n";
}

if ($debug) {
	foreach my $c (@{$ah->{call_stack}}) {
		my ($t, $level, $full_line, $ret) = @{$c};

		$ret =~ s/\n/\\n/g;
		$full_line =~ s/\n/\\n/g;

		print STDERR ('  ' x $level),
			'{-', $full_line, '} -> ',
			$ret,
			"\n"
		;
	}
}

exit(0);

######################################################################

sub read_config
{
	my ($conf, $inverse) = @_;
	local (*F);

	# read config file
	unless (open F, $conf) {
		if($quiet) {
			return;
		}
		else {
			die "'$conf' bad config file";
		}
	}

	while (<F>) {
		my ($key, $val);

		chomp;

		unless (/^#/ or /^$/) {
			($key, $val) = split("=", $_, 2);

			if ($val =~ s/^\|//) {
				$val = `$val`;
				chop($val);
			}
			elsif ($val eq ('<<' . 'EOF')) {
				# 'document here' construction
				$val = '';

				while (<F>) {
					last if /^EOF/;

					$val .= $_;
				}
			}
			elsif ($key eq "\\INCLUDE") {
				read_config($val);
				next;
			}

			$vars{$key} = $val;

			if ($inverse) {
				$inv_vars{$key} = $val;
			}
		}
	}

	close F;
}


sub usage
{
	print("$artemus_id - HTML (and other things) Preprocessor\n");
	print("Copyright (C) 2000/2009 Angel Ortega <angel\@triptico.com>\n\n");

	print("Usage:\n");
	print("  artemus -i|--input={input file} -o|--output={output file}\n");
	print("        [-c|--conf={config file}]\n");
	print("        [-l|--include-path={path to includes}]\n");
	print("        [-p|--paragraph={paragraph_separator}]\n");
	print("        [-q|--quiet]\n");
	print("        [-m|--msdos] [-a|--append] [-d|--debug]\n\n");

	exit(1);
}
