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