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