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