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