summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: 42caf3039a9d79fc9686adc206b5b874eb2b3bdf (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. system('stty -echo 2>/dev/null');
  97. local $|=1;
  98. print "\n\nCreating wiki admin $admin ...\n";
  99. print "Choose a password: ";
  100. chomp(my $password=<STDIN>);
  101. print "\n\n\n";
  102. system('stty sane 2>/dev/null');
  103. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  104. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  105. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  106. }
  107. else {
  108. error("problem setting up $admin user");
  109. }
  110. }
  111. # Add wrappers, make live.
  112. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  113. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  114. }
  115. # Add it to the wikilist.
  116. mkpath("$ENV{HOME}/.ikiwiki");
  117. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  118. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  119. close WIKILIST;
  120. if (system("ikiwiki-update-wikilist") != 0) {
  121. print STDERR "** Failed to add you to the system wikilist file.\n";
  122. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  123. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  124. }
  125. # Done!
  126. print "\n\nSuccessfully set up $config{wikiname}:\n";
  127. foreach my $key (qw{url srcdir destdir repository}) {
  128. next unless exists $config{$key};
  129. print "\t$key: ".(" " x (10 - length($key)))." ".
  130. prettydir($config{$key})."\n";
  131. }
  132. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  133. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  134. exit 0;
  135. }
  136. 1