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') {
75 elsif ($config{rcs} eq 'cvs') {
76 $config{cvs_wrapper}=$config{repository}."/CVSROOT/post-commit";
79 error sprintf(gettext("unsupported revision control system %s"),
84 IkiWiki::checkconfig();
86 print "\n\nSetting up $config{wikiname} ...\n";
89 mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
90 # Copy in example wiki.
91 if (exists $config{example}) {
93 # Another reason not to use -a is so that pages such as blog
94 # posts will not have old creation dates on this new wiki.
95 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
96 delete $config{example};
99 # Set up the repository.
100 delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
102 my @params=($config{rcs}, $config{srcdir});
103 push @params, $config{repository} if exists $config{repository};
104 if (system("ikiwiki-makerepo", @params) != 0) {
105 error gettext("failed to set up the repository with ikiwiki-makerepo");
109 # Make sure that all the listed plugins can load
110 # and checkconfig is ok. If a plugin fails to work,
111 # remove it from the configuration and keep on truckin'.
112 my %bakconfig=%config; # checkconfig can modify %config so back up
113 if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
114 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
116 # delete all hooks so that only this plugins's
117 # checkconfig will be run
119 IkiWiki::loadplugin($plugin);
120 IkiWiki::run_hooks(checkconfig => sub { shift->() });
123 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
126 push @{$bakconfig{disable_plugins}}, $plugin;
132 # Generate setup file.
133 require IkiWiki::Setup;
134 IkiWiki::Setup::dump($config{dumpsetup});
136 # Build the wiki, but w/o wrappers, so it's not live yet.
137 mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
138 if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
139 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
142 # Create admin user(s).
143 foreach my $admin (@{$config{adminuser}}) {
144 next if $admin=~/^http\?:\/\//; # openid
146 # Prompt for password w/o echo.
147 my ($password, $password2);
148 system('stty -echo 2>/dev/null');
150 print "\n\nCreating wiki admin $admin ...\n";
152 print "Choose a password: ";
153 chomp($password=<STDIN>);
155 print "Confirm password: ";
156 chomp($password2=<STDIN>);
158 last if $password2 eq $password;
160 print "Password mismatch.\n\n";
163 system('stty sane 2>/dev/null');
165 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
166 IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
167 IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
170 error("problem setting up $admin user");
174 # Add wrappers, make live.
175 if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
176 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
179 # Add it to the wikilist.
180 mkpath("$ENV{HOME}/.ikiwiki");
181 open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
182 print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
184 if (system("ikiwiki-update-wikilist") != 0) {
185 print STDERR "** Failed to add you to the system wikilist file.\n";
186 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
187 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
191 print "\n\nSuccessfully set up $config{wikiname}:\n";
192 foreach my $key (qw{url srcdir destdir repository}) {
193 next unless exists $config{$key};
194 print "\t$key: ".(" " x (10 - length($key)))." ".
195 prettydir($config{$key})."\n";
197 print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
198 print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";