aboutsummaryrefslogtreecommitdiff
path: root/ikiwiki-mass-rebuild
blob: 06a9b512b42ca8afc176ad4dfbe7aba8db3506c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/perl
use warnings;
use strict;

my $etcfile="/etc/ikiwiki/wikilist";

sub root {
	$> == 0;
}

sub username {
	(getpwuid($>))[0];
}

sub processline {
	my $setup=shift;
	
	if (! -f "$setup") {
		print STDERR "warning: $setup does not exist, skipping\n";
		return;
	}
	print "Processing $setup as user ".username()." ...\n";
	my $ret=system("ikiwiki", "--setup", $setup, @ARGV);
	if ($ret != 0) {
		print STDERR "warning: processing $setup failed with code $ret\n";
	}
}

my %users;
sub processuser {
	my $user=shift;
	return if $user=~/^-/ || $users{$user};
	$users{$user}=1;
	my $ret=system("su", $user, "-s", "/bin/sh", "-c", "--", "$0 --nonglobal @ARGV");
	if ($ret != 0) {
		print STDERR "warning: processing for $user failed with code $ret\n";
	}
}

sub processlist {
	my $file=shift;

	return unless -e $file;

	my $list;
	open ($list, "<$file") || die "$file: $!";
	while (<$list>) {
		chomp;
		s/^\s+//;
		s/\s+$//;
		next if /^#/ || ! length;
		if (/^([-\w]+)\s+([^\s]+)$/) {
			my $user=$1;
			my $setup=$2;
			if (root()) {
				processuser($user);
			}
			else {
		       		if (username() eq $user) {
					processline($setup);
				}
			}
		}
		elsif (/^([-\w]+)$/) {
			my $user=$1;
			if (root()) {
				processuser($user);
			}
			else {
				my $home=(getpwnam($user))[7];
				if (defined $home && -d $home) {
					my $dotfile="$home/.ikiwiki/wikilist";
					processlist($dotfile);
				}
			}
		}
	}
	close $list;
}

if (@ARGV && $ARGV[0] eq "--nonglobal") {
	shift;
	# avoid recursively processing if the wikilist file has a root
	# user in it
	if (root()) {
		exit 1;
	}
}

processlist($etcfile);