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