summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: 38e0d4422bc0506ec2249273299313e672777a70 (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. my $err=$@;
  113. print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
  114. $plugin)."\n";
  115. print STDERR "$err\n";
  116. push @{$bakconfig{disable_plugins}}, $plugin;
  117. }
  118. }
  119. }
  120. %config=%bakconfig;
  121. # Generate setup file.
  122. require IkiWiki::Setup;
  123. IkiWiki::Setup::dump($config{dumpsetup});
  124. # Build the wiki, but w/o wrappers, so it's not live yet.
  125. mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
  126. if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
  127. die "ikiwiki --refresh --setup $config{dumpsetup} failed";
  128. }
  129. # Create admin user(s).
  130. foreach my $admin (@{$config{adminuser}}) {
  131. next if defined IkiWiki::openiduser($admin);
  132. # Prompt for password w/o echo.
  133. my ($password, $password2);
  134. system('stty -echo 2>/dev/null');
  135. local $|=1;
  136. print "\n\nCreating wiki admin $admin ...\n";
  137. for (;;) {
  138. print "Choose a password: ";
  139. chomp($password=<STDIN>);
  140. print "\n";
  141. print "Confirm password: ";
  142. chomp($password2=<STDIN>);
  143. last if $password2 eq $password;
  144. print "Password mismatch.\n\n";
  145. }
  146. print "\n\n\n";
  147. system('stty sane 2>/dev/null');
  148. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  149. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  150. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  151. }
  152. else {
  153. error("problem setting up $admin user");
  154. }
  155. }
  156. # Add wrappers, make live.
  157. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  158. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  159. }
  160. # Add it to the wikilist.
  161. mkpath("$ENV{HOME}/.ikiwiki");
  162. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  163. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  164. close WIKILIST;
  165. if (system("ikiwiki-update-wikilist") != 0) {
  166. print STDERR "** Failed to add you to the system wikilist file.\n";
  167. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  168. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  169. }
  170. # Done!
  171. print "\n\nSuccessfully set up $config{wikiname}:\n";
  172. foreach my $key (qw{url srcdir destdir repository}) {
  173. next unless exists $config{$key};
  174. print "\t$key: ".(" " x (10 - length($key)))." ".
  175. prettydir($config{$key})."\n";
  176. }
  177. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  178. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  179. exit 0;
  180. }
  181. 1