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