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