-sub error { #{{{
- if ($config{cgi}) {
- print "Content-type: text/html\n\n";
- print misctemplate("Error", "<p>Error: @_</p>");
- }
- die @_;
-} #}}}
-
-sub debug ($) { #{{{
- return unless $config{verbose};
- if (! $config{cgi}) {
- print "@_\n";
- }
- else {
- print STDERR "@_\n";
- }
-} #}}}
-
-sub possibly_foolish_untaint { #{{{
- my $tainted=shift;
- my ($untainted)=$tainted=~/(.*)/;
- return $untainted;
-} #}}}
-
-sub basename ($) { #{{{
- my $file=shift;
-
- $file=~s!.*/!!;
- return $file;
-} #}}}
-
-sub dirname ($) { #{{{
- my $file=shift;
-
- $file=~s!/?[^/]+$!!;
- return $file;
-} #}}}
-
-sub pagetype ($) { #{{{
- my $page=shift;
-
- if ($page =~ /\.mdwn$/) {
- return ".mdwn";
- }
- else {
- return "unknown";
- }
-} #}}}
-
-sub pagename ($) { #{{{
- my $file=shift;
-
- my $type=pagetype($file);
- my $page=$file;
- $page=~s/\Q$type\E*$// unless $type eq 'unknown';
- return $page;
-} #}}}
-
-sub htmlpage ($) { #{{{
- my $page=shift;
-
- return $page.".html";
-} #}}}
-
-sub readfile ($) { #{{{
- my $file=shift;
-
- if (-l $file) {
- error("cannot read a symlink ($file)");
- }
-
- local $/=undef;
- open (IN, "$file") || error("failed to read $file: $!");
- my $ret=<IN>;
- close IN;
- return $ret;
-} #}}}
-
-sub writefile ($$) { #{{{
- my $file=shift;
- my $content=shift;
-
- if (-l $file) {
- error("cannot write to a symlink ($file)");
- }
-
- my $dir=dirname($file);
- if (! -d $dir) {
- my $d="";
- foreach my $s (split(m!/+!, $dir)) {
- $d.="$s/";
- if (! -d $d) {
- mkdir($d) || error("failed to create directory $d: $!");
- }
- }
- }
-
- open (OUT, ">$file") || error("failed to write $file: $!");
- print OUT $content;
- close OUT;
-} #}}}
-
-sub bestlink ($$) { #{{{
- # Given a page and the text of a link on the page, determine which
- # existing page that link best points to. Prefers pages under a
- # subdirectory with the same name as the source page, failing that
- # goes down the directory tree to the base looking for matching
- # pages.
- my $page=shift;
- my $link=lc(shift);
-
- my $cwd=$page;
- do {
- my $l=$cwd;
- $l.="/" if length $l;
- $l.=$link;
-
- if (exists $links{$l}) {
- #debug("for $page, \"$link\", use $l");
- return $l;
+sub getconfig () { #{{{
+ if (! exists $ENV{WRAPPED_OPTIONS}) {
+ %config=defaultconfig();
+ eval q{use Getopt::Long};
+ GetOptions(
+ "setup|s=s" => \$config{setup},
+ "wikiname=s" => \$config{wikiname},
+ "verbose|v!" => \$config{verbose},
+ "rebuild!" => \$config{rebuild},
+ "refresh!" => \$config{refresh},
+ "getctime" => \$config{getctime},
+ "wrappermode=i" => \$config{wrappermode},
+ "rcs=s" => \$config{rcs},
+ "no-rcs" => sub { $config{rcs}="" },
+ "anonok!" => \$config{anonok},
+ "rss!" => \$config{rss},
+ "cgi!" => \$config{cgi},
+ "discussion!" => \$config{discussion},
+ "notify!" => \$config{notify},
+ "url=s" => \$config{url},
+ "cgiurl=s" => \$config{cgiurl},
+ "historyurl=s" => \$config{historyurl},
+ "diffurl=s" => \$config{diffurl},
+ "svnrepo" => \$config{svnrepo},
+ "svnpath" => \$config{svnpath},
+ "adminemail=s" => \$config{adminemail},
+ "exclude=s@" => sub {
+ $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[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] ? $_[1] : "ikiwiki-wrap"
+ },
+ "plugin=s@" => sub {
+ push @{$config{plugin}}, $_[1];
+ },
+ "disable-plugin=s@" => sub {
+ $config{plugin}=[grep { $_ ne $_[1] } @{$config{plugin}} ];
+ },
+ ) || usage();
+
+ if (! $config{setup}) {
+ usage() unless @ARGV == 2;
+ $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
+ $config{destdir} = possibly_foolish_untaint(shift @ARGV);
+ checkconfig();