X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/c20c4066311341e332dc425023f856b6414de9ae..0b733575a44a527b54afc75b4c2b87b8bc5872a3:/ikiwiki-mass-rebuild

diff --git a/ikiwiki-mass-rebuild b/ikiwiki-mass-rebuild
index dac54cb8e..9b57532bc 100755
--- a/ikiwiki-mass-rebuild
+++ b/ikiwiki-mass-rebuild
@@ -1,32 +1,90 @@
-#!/bin/sh
-set -e
+#!/usr/bin/perl
+use warnings;
+use strict;
 
-action=""
-if [ -n "$1" ]; then
-	action="$1"
-fi
+my $etcfile="/etc/ikiwiki/wikilist";
 
-wikilist=/etc/ikiwiki/wikilist
+sub root {
+	$> == 0;
+}
 
-processline () {
-	user="$1"
-	setup="$2"
-	
-	if [ -z "$user" ] || [ -z "$setup" ]; then
-		echo "parse failure in /etc/ikiwiki/wikilist, line: '$user $setup'" >&2
-		exit 1
-	fi
+sub username {
+	(getpwuid($>))[0];
+}
+
+sub processline {
+	my $setup=shift;
 	
-	if [ ! -f "$setup" ]; then
-		echo "warning: $setup specified in /etc/ikiwiki/wikilist does not exist, skipping" >&2
-	else
-		echo "Processing $setup as user $user ..."
-		su "$user" -c "ikiwiki -setup $setup $action"
-	fi
+	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;
+	}
 }
 
-if [ -e "$wikilist" ]; then
-	grep -v '^#' $wikilist | grep -v '^$' | while read line; do 
-		processline $line
-	done
-fi
+processlist($etcfile);