summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: 7d9eca3afef7f929edfb8a1c4ddc1da2fc3be875 (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 'bzr') {
  53. # TODO
  54. }
  55. elsif ($config{rcs} eq 'mercurial') {
  56. # TODO
  57. }
  58. else {
  59. error sprintf(gettext("unsupported revision control system %s"),
  60. $config{rcs});
  61. }
  62. }
  63. IkiWiki::checkconfig();
  64. print "\n\nSetting up $config{wikiname} ...\n";
  65. # Set up the srcdir.
  66. mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
  67. # Copy in example wiki.
  68. if (exists $config{example}) {
  69. # cp -R is POSIX
  70. # Another reason not to use -a is so that pages such as blog
  71. # posts will not have old creation dates on this new wiki.
  72. system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
  73. delete $config{example};
  74. }
  75. # Set up the repository.
  76. delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
  77. if ($config{rcs}) {
  78. my @params=($config{rcs}, $config{srcdir});
  79. push @params, $config{repository} if exists $config{repository};
  80. if (system("ikiwiki-makerepo", @params) != 0) {
  81. error gettext("failed to set up the repository with ikiwiki-makerepo");
  82. }
  83. }
  84. # Generate setup file.
  85. require IkiWiki::Setup;
  86. IkiWiki::Setup::dump($config{dumpsetup});
  87. # Build the wiki, but w/o wrappers, so it's not live yet.
  88. mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
  89. if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
  90. die "ikiwiki --refresh --setup $config{dumpsetup} failed";
  91. }
  92. # Create admin user(s).
  93. foreach my $admin (@{$config{adminuser}}) {
  94. next if $admin=~/^http\?:\/\//; # openid
  95. # Prompt for password w/o echo.
  96. my ($password, $password2);
  97. system('stty -echo 2>/dev/null');
  98. local $|=1;
  99. print "\n\nCreating wiki admin $admin ...\n";
  100. for (;;) {
  101. print "Choose a password: ";
  102. chomp($password=<STDIN>);
  103. print "Confirm password: ";
  104. chomp($password2=<STDIN>);
  105. last if $password2 eq $password;
  106. print "Password mismatch.\n\n";
  107. }
  108. print "\n\n\n";
  109. system('stty sane 2>/dev/null');
  110. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  111. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  112. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  113. }
  114. else {
  115. error("problem setting up $admin user");
  116. }
  117. }
  118. # Add wrappers, make live.
  119. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  120. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  121. }
  122. # Add it to the wikilist.
  123. mkpath("$ENV{HOME}/.ikiwiki");
  124. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  125. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  126. close WIKILIST;
  127. if (system("ikiwiki-update-wikilist") != 0) {
  128. print STDERR "** Failed to add you to the system wikilist file.\n";
  129. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  130. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  131. }
  132. # Done!
  133. print "\n\nSuccessfully set up $config{wikiname}:\n";
  134. foreach my $key (qw{url srcdir destdir repository}) {
  135. next unless exists $config{$key};
  136. print "\t$key: ".(" " x (10 - length($key)))." ".
  137. prettydir($config{$key})."\n";
  138. }
  139. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  140. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  141. exit 0;
  142. }
  143. 1