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