move generic comment into IkiWiki::Setup
[ikiwiki.git] / IkiWiki / Setup / Automator.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup automator.
3
4 package IkiWiki::Setup::Automator;
5
6 use warnings;
7 use strict;
8 use IkiWiki;
9 use IkiWiki::UserInfo;
10 use Term::ReadLine;
11 use File::Path;
12 use Encode;
13
14 sub ask ($$) {
15         my ($question, $default)=@_;
16
17         my $r=Term::ReadLine->new("ikiwiki");
18         $r->ornaments("md,me");
19         $r->readline(encode_utf8($question)." ", $default);
20 }
21
22 sub prettydir ($) {
23         my $dir=shift;
24         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
25         return $dir;
26 }
27
28 sub sanitize_wikiname ($) {
29         my $wikiname=shift;
30
31         # Sanitize this to avoid problimatic directory names.
32         $wikiname=~s/[^-A-Za-z0-9_]//g;
33         if (! length $wikiname) {
34                 error gettext("you must enter a wikiname (that contains alphanumerics)");
35         }
36         return $wikiname;
37 }
38
39 sub import (@) {
40         my $this=shift;
41         IkiWiki::Setup::merge({@_});
42
43         if (! $config{force_overwrite}) {
44                 # Avoid overwriting any existing files.
45                 foreach my $key (qw{srcdir destdir repository dumpsetup}) {
46                         next unless exists $config{$key};
47                         my $add="";
48                         my $dir=IkiWiki::dirname($config{$key})."/";
49                         my $base=IkiWiki::basename($config{$key});
50                         while (-e $dir.$add.$base) {
51                                 $add=1 if ! $add;
52                                 $add++;
53                         }
54                         $config{$key}=$dir.$add.$base;
55                 }
56         }
57         
58         # Set up wrapper
59         if ($config{rcs}) {
60                 if ($config{rcs} eq 'git') {
61                         $config{git_wrapper}=$config{repository}."/hooks/post-update";
62                 }
63                 elsif ($config{rcs} eq 'svn') {
64                         $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
65                 }
66                 elsif ($config{rcs} eq 'monotone') {
67                         $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
68                 }
69                 elsif ($config{rcs} eq 'darcs') {
70                         $config{darcs_wrapper}=$config{repository}."/_darcs/ikiwiki-wrapper";
71                 }
72                 elsif ($config{rcs} eq 'bzr') {
73                         # TODO
74                 }
75                 elsif ($config{rcs} eq 'mercurial') {
76                         # TODO
77                 }
78                 elsif ($config{rcs} eq 'cvs') {
79                         $config{cvs_wrapper}=$config{repository}."/CVSROOT/post-commit";
80                 }
81                 else {
82                         error sprintf(gettext("unsupported revision control system %s"),
83                                 $config{rcs});
84                 }
85         }
86
87         IkiWiki::checkconfig();
88
89         print "\n\nSetting up $config{wikiname} ...\n";
90
91         # Set up the srcdir.
92         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
93         # Copy in example wiki.
94         if (exists $config{example}) {
95                 # cp -R is POSIX
96                 # Another reason not to use -a is so that pages such as blog
97                 # posts will not have old creation dates on this new wiki.
98                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
99                 delete $config{example};
100         }
101
102         # Set up the repository.
103         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
104         if ($config{rcs}) {
105                 my @params=($config{rcs}, $config{srcdir});
106                 push @params, $config{repository} if exists $config{repository};
107                 if (system("ikiwiki-makerepo", @params) != 0) {
108                         error gettext("failed to set up the repository with ikiwiki-makerepo");
109                 }
110         }
111
112         # Make sure that all the listed plugins can load
113         # and checkconfig is ok. If a plugin fails to work,
114         # remove it from the configuration and keep on truckin'.
115         my %bakconfig=%config; # checkconfig can modify %config so back up
116         if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
117                 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
118                         eval {
119                                 # delete all hooks so that only this plugins's
120                                 # checkconfig will be run
121                                 %IkiWiki::hooks=();
122                                 IkiWiki::loadplugin($plugin);
123                                 IkiWiki::run_hooks(checkconfig => sub { shift->() });
124                         };
125                         if ($@) {
126                                 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
127                                         $plugin)."\n";
128                                 print STDERR "$@\n";
129                                 push @{$bakconfig{disable_plugins}}, $plugin;
130                         }
131                 }
132         }
133         %config=%bakconfig;
134
135         # Generate setup file.
136         require IkiWiki::Setup;
137         IkiWiki::Setup::dump($config{dumpsetup});
138
139         # Build the wiki, but w/o wrappers, so it's not live yet.
140         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
141         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
142                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
143         }
144
145         # Create admin user(s).
146         foreach my $admin (@{$config{adminuser}}) {
147                 next if defined IkiWiki::openiduser($admin);
148                 
149                 # Prompt for password w/o echo.
150                 my ($password, $password2);
151                 system('stty -echo 2>/dev/null');
152                 local $|=1;
153                 print "\n\nCreating wiki admin $admin ...\n";
154                 for (;;) {
155                         print "Choose a password: ";
156                         chomp($password=<STDIN>);
157                         print "\n";
158                         print "Confirm password: ";
159                         chomp($password2=<STDIN>);
160
161                         last if $password2 eq $password;
162
163                         print "Password mismatch.\n\n";
164                 }
165                 print "\n\n\n";
166                 system('stty sane 2>/dev/null');
167
168                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
169                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
170                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
171                 }
172                 else {
173                         error("problem setting up $admin user");
174                 }
175         }
176         
177         # Add wrappers, make live.
178         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
179                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
180         }
181
182         # Add it to the wikilist.
183         mkpath("$ENV{HOME}/.ikiwiki");
184         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
185         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
186         close WIKILIST;
187         if (system("ikiwiki-update-wikilist") != 0) {
188                 print STDERR "** Failed to add you to the system wikilist file.\n";
189                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
190                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
191         }
192         
193         # Done!
194         print "\n\nSuccessfully set up $config{wikiname}:\n";
195         foreach my $key (qw{url srcdir destdir repository}) {
196                 next unless exists $config{$key};
197                 print "\t$key: ".(" " x (10 - length($key)))." ".
198                         prettydir($config{$key})."\n";
199         }
200         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
201         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
202         exit 0;
203 }
204
205 1