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