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