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