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