summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Automator.pm
blob: ee83a2fcfd1a871a6189d313e1f78c77eae9246e (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. die "you must enter a wikiname\n";
  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. while (-e $add.$config{$key}) {
  33. $add=1 if ! $add;
  34. $add++;
  35. }
  36. $config{$key}=$add.$config{$key};
  37. }
  38. IkiWiki::checkconfig();
  39. print "\n\nSetting up $config{wikiname} ...\n";
  40. # Set up the repository.
  41. mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
  42. delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
  43. if ($config{rcs}) {
  44. my @params=($config{rcs}, $config{srcdir});
  45. push @params, $config{repository} if exists $config{repository};
  46. if (system("ikiwiki-makerepo", @params) != 0) {
  47. die "failed: ikiwiki-makerepo @params";
  48. }
  49. }
  50. # Generate setup file.
  51. require IkiWiki::Setup;
  52. if ($config{rcs}) {
  53. if ($config{rcs} eq 'git') {
  54. $config{git_wrapper}=$config{repository}."/hooks/post-update";
  55. }
  56. elsif ($config{rcs} eq 'svn') {
  57. $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
  58. }
  59. elsif ($config{rcs} eq 'bzr') {
  60. # TODO
  61. }
  62. elsif ($config{rcs} eq 'mercurial') {
  63. # TODO
  64. }
  65. }
  66. IkiWiki::Setup::dump($config{dumpsetup});
  67. # Build the wiki, but w/o wrappers, so it's not live yet.
  68. mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
  69. if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
  70. die "ikiwiki --refresh --setup $config{dumpsetup} failed";
  71. }
  72. # Create admin user(s).
  73. foreach my $admin (@{$config{adminuser}}) {
  74. next if $admin=~/^http\?:\/\//; # openid
  75. # Prompt for password w/o echo.
  76. system('stty -echo 2>/dev/null');
  77. local $|=1;
  78. print "\n\nCreating wiki admin $admin ...\n";
  79. print "Choose a password: ";
  80. chomp(my $password=<STDIN>);
  81. print "\n\n\n";
  82. system('stty sane 2>/dev/null');
  83. if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
  84. IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
  85. IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
  86. }
  87. else {
  88. error("problem setting up $admin user");
  89. }
  90. }
  91. # Add wrappers, make live.
  92. if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
  93. die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
  94. }
  95. # Add it to the wikilist.
  96. mkpath("$ENV{HOME}/.ikiwiki");
  97. open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
  98. print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
  99. close WIKILIST;
  100. if (system("ikiwiki-update-wikilist") != 0) {
  101. print STDERR "** Failed to add you to the system wikilist file.\n";
  102. print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
  103. print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
  104. }
  105. # Done!
  106. print "\n\nSuccessfully set up $config{wikiname}:\n";
  107. foreach my $key (qw{url srcdir destdir repository}) {
  108. next unless exists $config{$key};
  109. print "\t$key: ".(" " x (10 - length($key)))." ".
  110. prettydir($config{$key})."\n";
  111. }
  112. print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
  113. print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
  114. exit 0;
  115. } #}}}
  116. 1