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