add toc, and improve layout
[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                         "wikiname=s" => \$config{wikiname},
24                         "verbose|v!" => \$config{verbose},
25                         "syslog!" => \$config{syslog},
26                         "rebuild!" => \$config{rebuild},
27                         "refresh!" => \$config{refresh},
28                         "post-commit" => \$config{post_commit},
29                         "render=s" => \$config{render},
30                         "wrappers!" => \$config{wrappers},
31                         "usedirs!" => \$config{usedirs},
32                         "getctime" => \$config{getctime},
33                         "wrappermode=i" => \$config{wrappermode},
34                         "numbacklinks=i" => \$config{numbacklinks},
35                         "rcs=s" => \$config{rcs},
36                         "no-rcs" => sub { $config{rcs}="" },
37                         "cgi!" => \$config{cgi},
38                         "discussion!" => \$config{discussion},
39                         "w3mmode!" => \$config{w3mmode},
40                         "notify!" => \$config{notify},
41                         "url=s" => \$config{url},
42                         "cgiurl=s" => \$config{cgiurl},
43                         "historyurl=s" => \$config{historyurl},
44                         "diffurl=s" => \$config{diffurl},
45                         "svnrepo" => \$config{svnrepo},
46                         "svnpath" => \$config{svnpath},
47                         "adminemail=s" => \$config{adminemail},
48                         "timeformat=s" => \$config{timeformat},
49                         "sslcookie!" => \$config{sslcookie},
50                         "httpauth!" => \$config{httpauth},
51                         "userdir=s" => \$config{userdir},
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] ? $_[1] : "ikiwiki-wrap"
66                         },
67                         "plugin=s@" => sub {
68                                 push @{$config{plugin}}, $_[1];
69                         },
70                         "disable-plugin=s@" => sub {
71                                 push @{$config{disable_plugins}}, $_[1];
72                         },
73                         "pingurl=s" => sub {
74                                 push @{$config{pingurl}}, $_[1];
75                         },
76                         "version" => sub {
77                                 print "ikiwiki version $IkiWiki::version\n";
78                                 exit;
79                         },
80                 ) || usage();
81
82                 if (! $config{setup} && ! $config{render}) {
83                         loadplugins();
84                         usage() unless @ARGV == 2;
85                         $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
86                         $config{destdir} = possibly_foolish_untaint(shift @ARGV);
87                         checkconfig();
88                 }
89         }
90         else {
91                 # wrapper passes a full config structure in the environment
92                 # variable
93                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
94                 if ($@) {
95                         error("WRAPPED_OPTIONS: $@");
96                 }
97                 loadplugins();
98                 checkconfig();
99         }
100 } #}}}
101
102 sub main () { #{{{
103         getconfig();
104         
105         if ($config{cgi}) {
106                 loadindex();
107                 require IkiWiki::CGI;
108                 cgi();
109         }
110         elsif ($config{setup}) {
111                 require IkiWiki::Setup;
112                 setup();
113         }
114         elsif ($config{wrapper}) {
115                 lockwiki();
116                 require IkiWiki::Wrapper;
117                 gen_wrapper();
118         }
119         elsif ($config{render}) {
120                 require IkiWiki::Render;
121                 commandline_render();
122         }
123         elsif ($config{post_commit} && ! commit_hook_enabled()) {
124                 if ($config{notify}) {
125                         loadindex();
126                         rcs_notify();
127                 }
128         }
129         else {
130                 lockwiki();
131                 loadindex();
132                 require IkiWiki::Render;
133                 rcs_update();
134                 refresh();
135                 rcs_notify() if $config{notify};
136                 saveindex();
137         }
138 } #}}}
139
140 main;