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