#!/bin/sh
set -e

action=""
if [ -n "$1" ]; then
	action="$1"
fi

wikilist=/etc/ikiwiki/wikilist

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
	
	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 [ -e "$wikilist" ]; then
	grep -v '^#' $wikilist | grep -v '^$' | while read line; do 
		processline $line
	done
fi