2 # Ikiwiki setup automator.
4 package IkiWiki::Setup::Automator;
15 my ($question, $default)=@_;
17 my $r=Term::ReadLine->new("ikiwiki");
18 $r->readline(encode_utf8($question)." ", $default);
23 $dir=~s/^\Q$ENV{HOME}\E\//~\//;
27 sub sanitize_wikiname ($) {
30 # Sanitize this to avoid problimatic directory names.
31 $wikiname=~s/[^-A-Za-z0-9_]//g;
32 if (! length $wikiname) {
33 error gettext("you must enter a wikiname (that contains alphanumerics)");
40 IkiWiki::Setup::merge({@_});
42 # Avoid overwriting any existing files.
43 foreach my $key (qw{srcdir destdir repository dumpsetup}) {
44 next unless exists $config{$key};
46 my $dir=IkiWiki::dirname($config{$key})."/";
47 my $base=IkiWiki::basename($config{$key});
48 while (-e $dir.$add.$base) {
52 $config{$key}=$dir.$add.$base;
57 if ($config{rcs} eq 'git') {
58 $config{git_wrapper}=$config{repository}."/hooks/post-update";
60 elsif ($config{rcs} eq 'svn') {
61 $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
63 elsif ($config{rcs} eq 'monotone') {
64 $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
66 elsif ($config{rcs} eq 'darcs') {
67 $config{darcs_wrapper}=$config{repository}."/_darcs/ikiwiki-wrapper";
69 elsif ($config{rcs} eq 'bzr') {
72 elsif ($config{rcs} eq 'mercurial') {
76 error sprintf(gettext("unsupported revision control system %s"),
81 IkiWiki::checkconfig();
83 print "\n\nSetting up $config{wikiname} ...\n";
86 mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
87 # Copy in example wiki.
88 if (exists $config{example}) {
90 # Another reason not to use -a is so that pages such as blog
91 # posts will not have old creation dates on this new wiki.
92 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
93 delete $config{example};
96 # Set up the repository.
97 delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
99 my @params=($config{rcs}, $config{srcdir});
100 push @params, $config{repository} if exists $config{repository};
101 if (system("ikiwiki-makerepo", @params) != 0) {
102 error gettext("failed to set up the repository with ikiwiki-makerepo");
106 # Make sure that all the listed plugins can load
107 # and checkconfig is ok. If a plugin fails to work,
108 # remove it from the configuration and keep on truckin'.
109 my %bakconfig=%config; # checkconfig can modify %config so back up
110 if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
111 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
113 # delete all hooks so that only this plugins's
114 # checkconfig will be run
116 IkiWiki::loadplugin($plugin);
117 IkiWiki::run_hooks(checkconfig => sub { shift->() });
120 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
123 push @{$bakconfig{disable_plugins}}, $plugin;
129 # Generate setup file.
130 require IkiWiki::Setup;
131 IkiWiki::Setup::dump($config{dumpsetup});
133 # Build the wiki, but w/o wrappers, so it's not live yet.
134 mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
135 if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
136 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
139 # Create admin user(s).
140 foreach my $admin (@{$config{adminuser}}) {
141 next if $admin=~/^http\?:\/\//; # openid
143 # Prompt for password w/o echo.
144 my ($password, $password2);
145 system('stty -echo 2>/dev/null');
147 print "\n\nCreating wiki admin $admin ...\n";
149 print "Choose a password: ";
150 chomp($password=<STDIN>);
152 print "Confirm password: ";
153 chomp($password2=<STDIN>);
155 last if $password2 eq $password;
157 print "Password mismatch.\n\n";
160 system('stty sane 2>/dev/null');
162 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
163 IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
164 IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
167 error("problem setting up $admin user");
171 # Add wrappers, make live.
172 if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
173 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
176 # Add it to the wikilist.
177 mkpath("$ENV{HOME}/.ikiwiki");
178 open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
179 print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
181 if (system("ikiwiki-update-wikilist") != 0) {
182 print STDERR "** Failed to add you to the system wikilist file.\n";
183 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
184 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
188 print "\n\nSuccessfully set up $config{wikiname}:\n";
189 foreach my $key (qw{url srcdir destdir repository}) {
190 next unless exists $config{$key};
191 print "\t$key: ".(" " x (10 - length($key)))." ".
192 prettydir($config{$key})."\n";
194 print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
195 print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";