28eba6f64e98e1298e9a32e61dbe6cb1f2897847
[ikiwiki.git] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 package IkiWiki;
5 use warnings;
6 use strict;
7 use lib '.'; # For use without installation, removed by Makefile.
8 use IkiWiki;
9
10 sub usage () { #{{{
11         die "usage: ikiwiki [options] source dest\n";
12 } #}}}
13
14 sub getconfig () { #{{{
15         if (! exists $ENV{WRAPPED_OPTIONS}) {
16                 %config=defaultconfig();
17                 eval q{use Getopt::Long};
18                 Getopt::Long::Configure('pass_through');
19                 GetOptions(
20                         "setup|s=s" => \$config{setup},
21                         "wikiname=s" => \$config{wikiname},
22                         "verbose|v!" => \$config{verbose},
23                         "rebuild!" => \$config{rebuild},
24                         "refresh!" => \$config{refresh},
25                         "getctime" => \$config{getctime},
26                         "wrappermode=i" => \$config{wrappermode},
27                         "rcs=s" => \$config{rcs},
28                         "no-rcs" => sub { $config{rcs}="" },
29                         "anonok!" => \$config{anonok},
30                         "rss!" => \$config{rss},
31                         "cgi!" => \$config{cgi},
32                         "discussion!" => \$config{discussion},
33                         "w3mmode!" => \$config{w3mmode},
34                         "notify!" => \$config{notify},
35                         "url=s" => \$config{url},
36                         "cgiurl=s" => \$config{cgiurl},
37                         "historyurl=s" => \$config{historyurl},
38                         "diffurl=s" => \$config{diffurl},
39                         "svnrepo" => \$config{svnrepo},
40                         "svnpath" => \$config{svnpath},
41                         "adminemail=s" => \$config{adminemail},
42                         "timeformat=s" => \$config{timeformat},
43                         "exclude=s@" => sub {
44                                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
45                         },
46                         "adminuser=s@" => sub {
47                                 push @{$config{adminuser}}, $_[1]
48                         },
49                         "templatedir=s" => sub {
50                                 $config{templatedir}=possibly_foolish_untaint($_[1])
51                         },
52                         "underlaydir=s" => sub {
53                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
54                         },
55                         "wrapper:s" => sub {
56                                 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
57                         },
58                         "plugin=s@" => sub {
59                                 push @{$config{plugin}}, $_[1];
60                         },
61                         "disable-plugin=s@" => sub {
62                                 $config{plugin}=[grep { $_ ne $_[1] } @{$config{plugin}}];
63                         },
64                         "pingurl" => sub {
65                                 push @{$config{pingurl}}, $_[1];
66                         }
67                 ) || usage();
68
69                 if (! $config{setup}) {
70                         loadplugins();
71                         run_hooks(getopt => sub { shift->() });
72                         if (grep /^-/, @ARGV) {
73                                 print STDERR "Unknown option: $_\n"
74                                         foreach grep /^-/, @ARGV;
75                                 usage();
76                         }
77                         usage() unless @ARGV == 2;
78                         $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
79                         $config{destdir} = possibly_foolish_untaint(shift @ARGV);
80                         checkconfig();
81                 }
82         }
83         else {
84                 # wrapper passes a full config structure in the environment
85                 # variable
86                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
87                 if ($@) {
88                         error("WRAPPED_OPTIONS: $@");
89                 }
90                 loadplugins();
91                 checkconfig();
92         }
93 } #}}}
94
95 sub main () { #{{{
96         getconfig();
97         
98         if ($config{cgi}) {
99                 lockwiki();
100                 loadindex();
101                 require IkiWiki::CGI;
102                 cgi();
103         }
104         elsif ($config{setup}) {
105                 require IkiWiki::Setup;
106                 setup();
107         }
108         elsif ($config{wrapper}) {
109                 lockwiki();
110                 require IkiWiki::Wrapper;
111                 gen_wrapper();
112         }
113         else {
114                 lockwiki();
115                 loadindex();
116                 require IkiWiki::Render;
117                 rcs_update();
118                 refresh();
119                 rcs_notify() if $config{notify};
120                 saveindex();
121         }
122 } #}}}
123
124 main;