summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: f1ed57b4b9f683bbf9f9e6e50756b1163d67b2bd (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 import (@) {
  22. my $this=shift;
  23. IkiWiki::Setup::merge({@_});
  24. # Sanitize this to avoid problimatic directory names.
  25. $config{wikiname}=~s/[^-A-Za-z0-9_]//g;
  26. if (! length $config{wikiname}) {
  27. error gettext("you must enter a wikiname (that contains alphanumerics)");
  28. }
  29. # Avoid overwriting any existing files.
  30. foreach my $key (qw{srcdir destdir repository dumpsetup}) {
  31. next unless exists $config{$key};
  32. my $add="";
  33. my $dir=IkiWiki::dirname($config{$key})."/";
  34. my $base=IkiWiki::basename($config{$key});
  35. while (-e $dir.$add.$base) {
  36. $add=1 if ! $add;
  37. $add++;
  38. }
  39. $config{$key}=$dir.$add.$base;
  40. }
  41. # Set up wrapper
  42. if ($config{rcs}) {
  43. if ($config{rcs} eq 'git') {
  44. $config{git_wrapper}=$config{repository}."/hooks/post-update";
  45. }
  46. elsif ($config{rcs} eq 'svn') {
  47. $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
  48. }
  49. elsif ($config{rcs} eq 'monotone') {
  50. $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
  51. }
  52. elsif ($config{rcs} eq 'darcs') {
  53. $config{darcs_wrapper}=$config{repository}."_darcs/ikiwiki-wrapper";
  54. }
  55. elsif ($config{rcs} eq 'bzr') {
  56. # TODO
  57. }
  58. elsif ($config{rcs} eq 'mercurial') {
  59. # TODO
  60. }
  61. else {
  62. error sprintf(gettext("unsupported revision control system %s"),
  63. $config{rcs});
  64. }
  65. }
  66. IkiWiki::checkconfig();
  67. print "\n\nSetting up $config{wikiname} ...\n";
  68. # Set up the srcdir.
  69. mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
  70. # Copy in example wiki.
  71. if (exists $config{example}) {
  72. # cp -R is POSIX
  73. # Another reason not to use -a is so that pages such as blog
  74. # posts will not have old creation dates on this new wiki.
  75. system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
  76. delete $config{example};
  77. }
  78. # Set up the repository.
  79. delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
  80. if ($config{rcs}) {
  81. my @params=($config{rcs}, $config{srcdir});
  82. push @params, $config{repository} if exists $config{repository};
  83. if (system("ikiwiki-makerepo", @params) != 0) {
  84. error gettext("failed to set up the repository with ikiwiki-makerepo");
  85. }
  86. }
  87. # Generate setup file.
  88. require IkiWiki::Setup;
  89. IkiWiki::Setup::dump($config{dumpsetup});
  90. # Build the wiki, but w/o wrappers, so it's not live yet.
  91. mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
  92. if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
  93. die "ikiwiki --refresh --setup $config{dumpsetup} failed";
  94. }
  95. # Create admin user(s).
  96. foreach my $admin (@{$config{adminuser}}) {
  97. next if $admin=~/^http\?:\/\//; # openid
  98. # Prompt for password w/o echo.
  99. my ($password, $password2);
  100. system('stty -echo 2>/dev/null');
  101. local $|=1;
  102. print "\n\nCreating wiki admin $admin ...\n";
  103. for (;;) {
  104. print "Choose a password: ";
  105. chomp($password=<STDIN>);
  106. print "Confirm password: ";
  107. chomp($password2=<STDIN>);
  108. last if $password2 eq $password;
  109. print "Password mismatch.\n\n";
  110. }
  111. print "\n\n\n";
  112. system('stty sane 2>/dev/null');
  113. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  114. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  115. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  116. }
  117. else {
  118. error("problem setting up $admin user");
  119. }
  120. }
  121. # Add wrappers, make live.
  122. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  123. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  124. }
  125. # Add it to the wikilist.
  126. mkpath("$ENV{HOME}/.ikiwiki");
  127. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  128. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  129. close WIKILIST;
  130. if (system("ikiwiki-update-wikilist") != 0) {
  131. print STDERR "** Failed to add you to the system wikilist file.\n";
  132. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  133. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  134. }
  135. # Done!
  136. print "\n\nSuccessfully set up $config{wikiname}:\n";
  137. foreach my $key (qw{url srcdir destdir repository}) {
  138. next unless exists $config{$key};
  139. print "\t$key: ".(" " x (10 - length($key)))." ".
  140. prettydir($config{$key})."\n";
  141. }
  142. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  143. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  144. exit 0;
  145. }
  146. 1