#!/bin/sh
#
# /usr/local/bin/localikiwikirefreshsite
# Copyright 2008 Jonas Smedegaard <dr@jones.dk>
#
# $Id: localikiwikirefreshsite,v 1.2 2008-06-03 12:10:52 jonas Exp $
#
# Refresh multipart ikiwiki site, aggregating only main part
#

set -e

PRG=$(basename "$0")

TEMP=$(getopt -s sh -o h -l help -n "$PRG" -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"

showhelp() {
	cat <<EOF
Usage:   $PRG PROJECT [MAIN [ADDON ...]]

Examples:
    $PRG wiki admin . admin.dev dev

Above example will --refresh the wiki project using the below configurations,
and will also --aggregate using the first of them:
    ikiwiki.admin.setup
    ikiwiki.setup
    ikiwiki.admin.dev.setup
    ikiwiki.dev.setup
EOF
}

exit1()	{
	echo >&2 "Error: $1"
	echo >&2 "Exiting..."
	exit 1
}

ikiwikirefresh()	{
	part="$1"
	shift || exit1 "Internal error: part not supplied!"

	case "$part" in
	    ""|.)	cfgpath=$CFGDIR/ikiwiki.setup;;
	    *)		cfgpath=$CFGDIR/ikiwiki.$part.setup;;
	esac
	ikiwiki --setup $cfgpath "$@"
}

while true ; do
	case "$1" in
		-h|--help) showhelp; exit 0;;
		--) shift; break;;
		*) exit1 "Internal error!";;
	esac
done

project="$1"
[ -n "$project" ] || exit1 "Project name must be supplied!"
shift

mainpart="$1"
shift || exit1 "Main part must be supplied (use \"\" if unnamed)!"

CFGDIR=~/private_webdata/$project

ikiwikirefresh "$mainpart" --aggregate --refresh
for part in "$@"; do
	ikiwikirefresh "$part" --refresh
done