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