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