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