summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: 9f2380d52e0eb1a48bfd218b23c0cd0e94e2e9f2 (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 repository.
  65. mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
  66. delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
  67. if ($config{rcs}) {
  68. my @params=($config{rcs}, $config{srcdir});
  69. push @params, $config{repository} if exists $config{repository};
  70. if (system("ikiwiki-makerepo", @params) != 0) {
  71. error gettext("failed to set up the repository with ikiwiki-makerepo");
  72. }
  73. }
  74. # Generate setup file.
  75. require IkiWiki::Setup;
  76. IkiWiki::Setup::dump($config{dumpsetup});
  77. # Build the wiki, but w/o wrappers, so it's not live yet.
  78. mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
  79. if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
  80. die "ikiwiki --refresh --setup $config{dumpsetup} failed";
  81. }
  82. # Create admin user(s).
  83. foreach my $admin (@{$config{adminuser}}) {
  84. next if $admin=~/^http\?:\/\//; # openid
  85. # Prompt for password w/o echo.
  86. system('stty -echo 2>/dev/null');
  87. local $|=1;
  88. print "\n\nCreating wiki admin $admin ...\n";
  89. print "Choose a password: ";
  90. chomp(my $password=<STDIN>);
  91. print "\n\n\n";
  92. system('stty sane 2>/dev/null');
  93. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  94. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  95. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  96. }
  97. else {
  98. error("problem setting up $admin user");
  99. }
  100. }
  101. # Add wrappers, make live.
  102. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  103. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  104. }
  105. # Add it to the wikilist.
  106. mkpath("$ENV{HOME}/.ikiwiki");
  107. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  108. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  109. close WIKILIST;
  110. if (system("ikiwiki-update-wikilist") != 0) {
  111. print STDERR "** Failed to add you to the system wikilist file.\n";
  112. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  113. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  114. }
  115. # Done!
  116. print "\n\nSuccessfully set up $config{wikiname}:\n";
  117. foreach my $key (qw{url srcdir destdir repository}) {
  118. next unless exists $config{$key};
  119. print "\t$key: ".(" " x (10 - length($key)))." ".
  120. prettydir($config{$key})."\n";
  121. }
  122. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  123. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  124. exit 0;
  125. }
  126. 1