blogspam: Fix use of blogspam_options and blogspam_server config settings.
[ikiwiki] / 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->readline(encode_utf8($question)." ", $default);
19 }
20
21 sub prettydir ($) {
22         my $dir=shift;
23         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
24         return $dir;
25 }
26
27 sub import (@) {
28         my $this=shift;
29         IkiWiki::Setup::merge({@_});
30
31         # Sanitize this to avoid problimatic directory names.
32         $config{wikiname}=~s/[^-A-Za-z0-9_]//g;
33         if (! length $config{wikiname}) {
34                 error gettext("you must enter a wikiname (that contains alphanumerics)");
35         }
36
37         # Avoid overwriting any existing files.
38         foreach my $key (qw{srcdir destdir repository dumpsetup}) {
39                 next unless exists $config{$key};
40                 my $add="";
41                 my $dir=IkiWiki::dirname($config{$key})."/";
42                 my $base=IkiWiki::basename($config{$key});
43                 while (-e $dir.$add.$base) {
44                         $add=1 if ! $add;
45                         $add++;
46                 }
47                 $config{$key}=$dir.$add.$base;
48         }
49         
50         # Set up wrapper
51         if ($config{rcs}) {
52                 if ($config{rcs} eq 'git') {
53                         $config{git_wrapper}=$config{repository}."/hooks/post-update";
54                 }
55                 elsif ($config{rcs} eq 'svn') {
56                         $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
57                 }
58                 elsif ($config{rcs} eq 'monotone') {
59                         $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
60                 }
61                 elsif ($config{rcs} eq 'bzr') {
62                         # TODO
63                 }
64                 elsif ($config{rcs} eq 'mercurial') {
65                         # TODO
66                 }
67                 else {
68                         error sprintf(gettext("unsupported revision control system %s"),
69                                 $config{rcs});
70                 }
71         }
72
73         IkiWiki::checkconfig();
74
75         print "\n\nSetting up $config{wikiname} ...\n";
76
77         # Set up the srcdir.
78         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
79         # Copy in example wiki.
80         if (exists $config{example}) {
81                 # cp -R is POSIX
82                 # Another reason not to use -a is so that pages such as blog
83                 # posts will not have old creation dates on this new wiki.
84                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
85                 delete $config{example};
86         }
87
88         # Set up the repository.
89         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
90         if ($config{rcs}) {
91                 my @params=($config{rcs}, $config{srcdir});
92                 push @params, $config{repository} if exists $config{repository};
93                 if (system("ikiwiki-makerepo", @params) != 0) {
94                         error gettext("failed to set up the repository with ikiwiki-makerepo");
95                 }
96         }
97
98         # Generate setup file.
99         require IkiWiki::Setup;
100         IkiWiki::Setup::dump($config{dumpsetup});
101
102         # Build the wiki, but w/o wrappers, so it's not live yet.
103         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
104         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
105                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
106         }
107
108         # Create admin user(s).
109         foreach my $admin (@{$config{adminuser}}) {
110                 next if $admin=~/^http\?:\/\//; # openid
111                 
112                 # Prompt for password w/o echo.
113                 system('stty -echo 2>/dev/null');
114                 local $|=1;
115                 print "\n\nCreating wiki admin $admin ...\n";
116                 print "Choose a password: ";
117                 chomp(my $password=<STDIN>);
118                 print "\n\n\n";
119                 system('stty sane 2>/dev/null');
120
121                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
122                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
123                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
124                 }
125                 else {
126                         error("problem setting up $admin user");
127                 }
128         }
129         
130         # Add wrappers, make live.
131         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
132                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
133         }
134
135         # Add it to the wikilist.
136         mkpath("$ENV{HOME}/.ikiwiki");
137         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
138         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
139         close WIKILIST;
140         if (system("ikiwiki-update-wikilist") != 0) {
141                 print STDERR "** Failed to add you to the system wikilist file.\n";
142                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
143                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
144         }
145         
146         # Done!
147         print "\n\nSuccessfully set up $config{wikiname}:\n";
148         foreach my $key (qw{url srcdir destdir repository}) {
149                 next unless exists $config{$key};
150                 print "\t$key: ".(" " x (10 - length($key)))." ".
151                         prettydir($config{$key})."\n";
152         }
153         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
154         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
155         exit 0;
156 }
157
158 1