setup automator
[ikiwiki.git] / IkiWiki / Setup / Automator.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup automator.
3
4 package IkiWiki::Setup::Automator;
5
6 use warnings;
7 use strict;
8 use IkiWiki;
9 use Term::ReadLine;
10 use File::Path;
11
12 sub ask ($$) { #{{{
13         my ($question, $default)=@_;
14
15         my $r=Term::ReadLine->new("ikiwiki");
16         $r->readline($question." ", $default);
17 } #}}}
18
19 sub import (@) { #{{{
20         my %setup=@_;
21
22         # Sanitize this to avoid problimatic directory names.
23         $setup{wikiname}=~s/[^-A-Za-z0-9_] //g;
24         if (! length $setup{wikiname}) {
25                 die "you must enter a wikiname\n";
26         }
27
28         # Avoid overwriting any existing files.
29         foreach my $key (qw{srcdir destdir repository setupfile}) {
30                 next unless exists $setup{$key};
31                 my $add="";
32                 while (-e $setup{$key}.$add) {
33                         $add=1 if ! $add;
34                         $add++;
35                 }
36                 $setup{$key}.=$add;
37         }
38
39         print "\n\nSetting up $setup{wikiname} ...\n";
40
41         # Set up the repository.
42         mkpath($setup{srcdir}) || die "mkdir $setup{srcdir}: $!";
43         delete $setup{repository} if ! $setup{rcs} || $setup{rcs}=~/bzr|mercurial/;
44         if ($setup{rcs}) {
45                 my @params=($setup{rcs}, $setup{srcdir});
46                 push @params, $setup{repository} if exists $setup{repository};
47                 if (system("ikiwiki-makerepo", @params) != 0) {
48                         die "failed: ikiwiki-makerepo @params";
49                 }
50         }
51
52         # Generate setup file.
53         my @params=(
54                 "--dumpsetup", $setup{setupfile},
55                 "--wikiname", $setup{wikiname},
56                 "--url", $setup{url},
57                 "--cgiurl", $setup{cgiurl}
58         );
59         push @params, "--rcs", $setup{rcs} if $setup{rcs};
60         if (exists $setup{add_plugins}) {
61                 foreach my $plugin (@{$setup{add_plugins}}) {
62                         push @params, "--plugin", $plugin;
63                 }
64         }
65         if (exists $setup{disable_plugins}) {
66                 foreach my $plugin (@{$setup{disable_plugins}}) {
67                         push @params, "--disable-plugin", $plugin;
68                 }
69         }
70         foreach my $key (keys %setup) {
71                 next if $key =~ /^(disable_plugins|add_plugins|setupfile|wikiname|url|cgiurl||srcdir|destdir|repository)$/;
72                 push @params, "--set", "$key=$setup{$key}";
73         }
74         if (system("ikiwiki", @params, $setup{srcdir}, $setup{destdir}) != 0) {
75                 die "failed: ikiwiki @params";
76         }
77
78         # Build the wiki.
79         mkpath($setup{destdir}) || die "mkdir $setup{destdir}: $!";
80         if (system("ikiwiki", "--setup", $setup{setupfile}) != 0) {
81                 die "ikiwiki --setup $setup{setupfile} failed";
82         }
83
84         # Done!
85         print "\n\nSuccessfully set up $setup{wikiname}:\n";
86         foreach my $key (qw{url srcdir destdir repository setupfile}) {
87                 next unless exists $setup{$key};
88                 my $value=$setup{$key};
89                 $value=~s/^\Q$ENV{HOME}\E\//~\//;
90                 print "\t$key: ".(" " x (10 - length($key)))." $value\n";
91         }
92         exit 0;
93 } #}}}
94
95 1