]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-mass-rebuild
wishlist++
[git.ikiwiki.info.git] / ikiwiki-mass-rebuild
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
5 my $etcfile="/etc/ikiwiki/wikilist";
7 sub root {
8         $> == 0;
9 }
11 sub username {
12         (getpwuid($>))[0];
13 }
15 sub processline {
16         my $setup=shift;
17         
18         if (! -f "$setup") {
19                 print STDERR "warning: $setup does not exist, skipping\n";
20                 return;
21         }
22         print "Processing $setup as user ".username()." ...\n";
23         my $ret=system("ikiwiki", "-setup", $setup, @ARGV);
24         if ($ret != 0) {
25                 print STDERR "warning: processing $setup failed with code $ret\n";
26         }
27 }
29 my %users;
30 sub processuser {
31         my $user=shift;
32         return if $user=~/^-/ || $users{$user};
33         $users{$user}=1;
34         my $ret=system("su", $user, "-s", "/bin/sh", "-c", "--", "$0 --nonglobal @ARGV");
35         if ($ret != 0) {
36                 print STDERR "warning: processing for $user failed with code $ret\n";
37         }
38 }
40 sub processlist {
41         my $file=shift;
43         return unless -e $file;
45         my $list;
46         open ($list, "<$file") || die "$file: $!";
47         while (<$list>) {
48                 chomp;
49                 s/^\s+//;
50                 s/\s+$//;
51                 next if /^#/ || ! length;
52                 if (/^([-\w]+)\s+([^\s]+)$/) {
53                         my $user=$1;
54                         my $setup=$2;
55                         if (root()) {
56                                 processuser($user);
57                         }
58                         else {
59                                 if (username() eq $user) {
60                                         processline($setup);
61                                 }
62                         }
63                 }
64                 elsif (/^([-\w]+)$/) {
65                         my $user=$1;
66                         if (root()) {
67                                 processuser($user);
68                         }
69                         else {
70                                 my $home=(getpwnam($user))[7];
71                                 if (defined $home && -d $home) {
72                                         my $dotfile="$home/.ikiwiki/wikilist";
73                                         processlist($dotfile);
74                                 }
75                         }
76                 }
77         }
78         close $list;
79 }
81 if (@ARGV && $ARGV[0] eq "--nonglobal") {
82         shift;
83         # avoid recursively processing if the wikilist file has a root
84         # user in it
85         if (root()) {
86                 exit 1;
87         }
88 }
90 processlist($etcfile);