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