Merge branch 'master' into autoconfig
[ikiwiki] / ikiwiki.in
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3 delete @ENV{qw{IFS CDPATH ENV BASH_ENV}};
4
5 package IkiWiki;
6
7 use warnings;
8 use strict;
9 use lib '.'; # For use in nonstandard directory, munged by Makefile.
10 use IkiWiki;
11
12 sub usage () { #{{{
13         die gettext("usage: ikiwiki [options] source dest"), "\n";
14 } #}}}
15
16 sub getconfig () { #{{{
17         if (! exists $ENV{WRAPPED_OPTIONS}) {
18                 %config=defaultconfig();
19                 eval q{use Getopt::Long};
20                 Getopt::Long::Configure('pass_through');
21                 GetOptions(
22                         "setup|s=s" => \$config{setup},
23                         "dumpsetup|s=s" => \$config{dumpsetup},
24                         "wikiname=s" => \$config{wikiname},
25                         "verbose|v!" => \$config{verbose},
26                         "syslog!" => \$config{syslog},
27                         "rebuild!" => \$config{rebuild},
28                         "refresh!" => \$config{refresh},
29                         "post-commit" => \$config{post_commit},
30                         "render=s" => \$config{render},
31                         "wrappers!" => \$config{genwrappers},
32                         "usedirs!" => \$config{usedirs},
33                         "prefix-directives!" => \$config{prefix_directives},
34                         "getctime" => \$config{getctime},
35                         "numbacklinks=i" => \$config{numbacklinks},
36                         "rcs=s" => \$config{rcs},
37                         "no-rcs" => sub { $config{rcs}="" },
38                         "cgi!" => \$config{cgi},
39                         "discussion!" => \$config{discussion},
40                         "w3mmode!" => \$config{w3mmode},
41                         "url=s" => \$config{url},
42                         "cgiurl=s" => \$config{cgiurl},
43                         "historyurl=s" => \$config{historyurl},
44                         "diffurl=s" => \$config{diffurl},
45                         "svnpath" => \$config{svnpath},
46                         "adminemail=s" => \$config{adminemail},
47                         "timeformat=s" => \$config{timeformat},
48                         "sslcookie!" => \$config{sslcookie},
49                         "userdir=s" => \$config{userdir},
50                         "htmlext=s" => \$config{htmlext},
51                         "libdir=s" => \$config{libdir},
52                         "exclude=s@" => sub {
53                                 push @{$config{wiki_file_prune_regexps}}, $_[1];
54                         },
55                         "adminuser=s@" => sub {
56                                 push @{$config{adminuser}}, $_[1]
57                         },
58                         "templatedir=s" => sub {
59                                 $config{templatedir}=possibly_foolish_untaint($_[1])
60                         },
61                         "underlaydir=s" => sub {
62                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
63                         },
64                         "wrapper:s" => sub {
65                                 $config{wrapper}=$_[1] ? possibly_foolish_untaint($_[1]) : "ikiwiki-wrap"
66                         },
67                         "wrappermode=i" => sub {
68                                 $config{wrappermode}=possibly_foolish_untaint($_[1])
69                         },
70                         "plugin=s@" => sub {
71                                 push @{$config{add_plugins}}, $_[1];
72                         },
73                         "disable-plugin=s@" => sub {
74                                 push @{$config{disable_plugins}}, $_[1];
75                         },
76                         "set=s" => sub {
77                                 my ($var, $val)=split('=', $_[1], 2);
78                                 if (! defined $var || ! defined $val) {
79                                         die gettext("usage: --set var=value"), "\n";
80                                 }
81                                 $config{$var}=$val;
82                         },
83                         "version" => sub {
84                                 print "ikiwiki version $IkiWiki::version\n";
85                                 exit;
86                         },
87                 ) || usage();
88
89                 if (! $config{setup} && ! $config{render}) {
90                         loadplugins();
91                         if (@ARGV == 2) {
92                                 $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
93                                 $config{destdir} = possibly_foolish_untaint(shift @ARGV);
94                                 checkconfig();
95                         }
96                         else {
97                                 usage() unless $config{dumpsetup};
98                         }
99                 }
100         }
101         else {
102                 # wrapper passes a full config structure in the environment
103                 # variable
104                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
105                 if ($@) {
106                         error("WRAPPED_OPTIONS: $@");
107                 }
108                 loadplugins();
109                 checkconfig();
110         }
111 } #}}}
112
113 sub main () { #{{{
114         getconfig();
115         
116         if ($config{setup}) {
117                 require IkiWiki::Setup;
118                 IkiWiki::Setup::load($config{setup});
119                 
120                 loadplugins();
121                 checkconfig();
122
123                 if (@{$config{wrappers}} && 
124                     ! $config{render} && ! $config{dumpsetup} &&
125                     (! $config{refresh} || $config{genwrappers})) {
126                         debug(gettext("generating wrappers.."));
127                         require IkiWiki::Wrapper;
128                         my %origconfig=(%config);
129                         my @wrappers=@{$config{wrappers}};
130                         delete $config{wrappers};
131                         delete $config{genwrappers};
132                         foreach my $wrapper (@wrappers) {
133                                 %config=(%origconfig,
134                                         rebuild => 0,
135                                         verbose => 0,
136                                         %{$wrapper},
137                                 );
138                                 checkconfig();
139                                 if (! $config{cgi} && ! $config{post_commit}) {
140                                         $config{post_commit}=1;
141                                 }
142                                 gen_wrapper();
143                         }
144                         %config=(%origconfig);
145                 }
146                 
147                 # setup implies a wiki rebuild by default
148                 if (! $config{refresh}) {
149                         $config{rebuild}=1;
150                 }
151                 
152                 # ignore syslog setting from setup file
153                 # while doing initial setup
154                 $config{syslog}=0 unless $config{dumpsetup};
155         }
156
157         if ($config{dumpsetup}) {
158                 require IkiWiki::Setup;
159                 IkiWiki::Setup::dump($config{dumpsetup});
160         }
161         elsif ($config{wrapper}) {
162                 lockwiki();
163                 require IkiWiki::Wrapper;
164                 gen_wrapper();
165         }
166         elsif ($config{cgi}) {
167                 require IkiWiki::CGI;
168                 eval {cgi()};
169                 if ($@) {
170                         cgierror($@);
171                 }
172         }
173         elsif ($config{render}) {
174                 require IkiWiki::Render;
175                 commandline_render();
176         }
177         elsif ($config{post_commit} && ! commit_hook_enabled()) {
178                 # do nothing
179         }
180         else {
181                 if (! $config{refresh}) {
182                         debug(gettext("rebuilding wiki.."));
183                 }
184                 else {
185                         debug(gettext("refreshing wiki.."));
186                 }
187                 lockwiki();
188                 loadindex();
189                 require IkiWiki::Render;
190                 rcs_update();
191                 refresh();
192                 saveindex();
193                 debug(gettext("done"));
194         }
195 } #}}}
196
197 main;