#!/usr/bin/perl
$ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
delete @ENV{qw{IFS CDPATH ENV BASH_ENV}};

package IkiWiki;

use warnings;
use strict;
use lib '.'; # For use in nonstandard directory, munged by Makefile.
use IkiWiki;

sub usage () {
	die gettext("usage: ikiwiki [options] source dest"), "\n",
	    gettext("       ikiwiki --setup configfile"), "\n";
}

sub getconfig () {
	if (! exists $ENV{WRAPPED_OPTIONS}) {
		%config=defaultconfig();
		eval q{use Getopt::Long};
		Getopt::Long::Configure('pass_through');
		GetOptions(
			"setup|s=s" => sub {
				require IkiWiki::Setup;
				my $verbose=$config{verbose};
				my $syslog=$config{syslog};
				IkiWiki::Setup::load($_[1]);
				$config{setupverbose}=$config{verbose};
				$config{setupsyslog}=$config{syslog};
				$config{verbose}=$verbose || $config{setupverbose};
				$config{syslog}=$syslog;
				$config{setup}=1;
			},
			"dumpsetup|s=s" => \$config{dumpsetup},
			"wikiname=s" => \$config{wikiname},
			"verbose|v!" => \$config{verbose},
			"syslog!" => \$config{syslog},
			"rebuild!" => \$config{rebuild},
			"refresh!" => \$config{refresh},
			"post-commit" => \$config{post_commit},
			"render=s" => \$config{render},
			"wrappers!" => \$config{genwrappers},
			"wrappergroup=s" => \$config{wrappergroup},
			"usedirs!" => \$config{usedirs},
			"prefix-directives!" => \$config{prefix_directives},
			"getctime" => \$config{getctime},
			"numbacklinks=i" => \$config{numbacklinks},
			"rcs=s" => \$config{rcs},
			"no-rcs" => sub { $config{rcs}="" },
			"cgi!" => \$config{cgi},
			"discussion!" => \$config{discussion},
			"w3mmode!" => \$config{w3mmode},
			"url=s" => \$config{url},
			"cgiurl=s" => \$config{cgiurl},
			"historyurl=s" => \$config{historyurl},
			"diffurl=s" => \$config{diffurl},
			"svnpath" => \$config{svnpath},
			"adminemail=s" => \$config{adminemail},
			"timeformat=s" => \$config{timeformat},
			"sslcookie!" => \$config{sslcookie},
			"userdir=s" => \$config{userdir},
			"htmlext=s" => \$config{htmlext},
			"libdir=s" => \$config{libdir},
			"exclude=s@" => sub {
				push @{$config{wiki_file_prune_regexps}}, $_[1];
			},
			"adminuser=s@" => sub {
				push @{$config{adminuser}}, $_[1]
			},
			"templatedir=s" => sub {
				$config{templatedir}=possibly_foolish_untaint($_[1])
			},
			"underlaydir=s" => sub {
				$config{underlaydir}=possibly_foolish_untaint($_[1])
			},
			"wrapper:s" => sub {
				$config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
			},
			"wrappermode=i" => sub {
				$config{wrappermode}=possibly_foolish_untaint($_[1])
			},
			"plugin=s@" => sub {
				push @{$config{add_plugins}}, $_[1];
			},
			"disable-plugin=s@" => sub {
				push @{$config{disable_plugins}}, $_[1];
			},
			"set=s" => sub {
				my ($var, $val)=split('=', $_[1], 2);
				if (! defined $var || ! defined $val) {
					die gettext("usage: --set var=value"), "\n";
				}
				$config{$var}=$val;
			},
			"version" => sub {
				print "ikiwiki version $IkiWiki::version\n";
				exit;
			},
			"help|h" => sub { $SIG{__WARN__}=sub {}; die },
		) || usage();

		if (! $config{setup}) {
			loadplugins();
			if (@ARGV == 2) {
				$config{srcdir} = possibly_foolish_untaint(shift @ARGV);
				$config{destdir} = possibly_foolish_untaint(shift @ARGV);
				checkconfig();
			}
			else {
				usage() unless $config{dumpsetup};
			}
		}
	}
	else {
		# wrapper passes a full config structure in the environment
		# variable
		eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
		if ($@) {
			error("WRAPPED_OPTIONS: $@");
		}
		delete $ENV{WRAPPED_OPTIONS};

		loadplugins();
		checkconfig();
	}
}

sub main () {
	getconfig();
	
	if ($config{setup}) {
		delete $config{setup};
		loadplugins();
		checkconfig();

		if (@{$config{wrappers}} && 
		    ! $config{render} && ! $config{dumpsetup} &&
		    ((! $config{refresh} && ! $config{post_commit})
		     || $config{genwrappers})) {
			debug(gettext("generating wrappers.."));
			require IkiWiki::Wrapper;
			my %origconfig=(%config);
			foreach my $wrapper (@{$config{wrappers}}) {
				%config=(%origconfig, %{$wrapper});
				$config{verbose}=$config{setupverbose}
					if exists $config{setupverbose};
				$config{syslog}=$config{setupsyslog}
					if exists $config{setupsyslog};
				delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
				checkconfig();
				if (! $config{cgi} && ! $config{post_commit} &&
				    ! $config{test_receive}) {
					$config{post_commit}=1;
				}
				gen_wrapper();
			}
			%config=(%origconfig);
		}
		
		# setup implies a wiki rebuild by default
		if (! $config{refresh} && ! $config{render} &&
		    ! $config{post_commit}) {
			$config{rebuild}=1;
		}
	}

	if ($config{dumpsetup}) {
		$config{srcdir}="" if ! defined $config{srcdir};
		$config{destdir}="" if ! defined $config{destdir};
		$config{syslog}=1 if $config{setupsyslog};
		require IkiWiki::Setup;
		IkiWiki::Setup::dump($config{dumpsetup});
	}
	elsif ($config{wrapper}) {
		lockwiki();
		require IkiWiki::Wrapper;
		gen_wrapper();
	}
	elsif ($config{cgi}) {
		require IkiWiki::CGI;
		eval {cgi()};
		if ($@) {
			cgierror($@);
		}
	}
	elsif ($config{render}) {
		require IkiWiki::Render;
		commandline_render();
	}
	elsif ($config{post_commit} && ! commit_hook_enabled()) {
		# do nothing
	}
	else {
		if ($config{rebuild}) {
			debug(gettext("rebuilding wiki.."));
		}
		else {
			debug(gettext("refreshing wiki.."));
		}
		lockwiki();
		loadindex();
		require IkiWiki::Render;
		rcs_update();
		refresh();
		saveindex();
		debug(gettext("done"));
	}
}

main;