#!/bin/sh
# This script clean-up the underlay page. (contained in zip files).
# Copyright 2009, Frank lin Piat ; License GPL V2 or later.


# Strip these notes in underlay about editing only on moinmaster:
## Pour plus d'informations, consultez la page MoinMoin:MoinDev/Translation.
## Please edit system and help pages ONLY in the moinmaster wiki!
## Please edit system and help pages ONLY in the master wiki!
## For more information, please see MoinMoin:MoinDev/Translation.

set -e
set -x 
UNDERLAY_DIR=$1
[ ! -d "$UNDERLAY_DIR" ] && (echo "Error: argument isn't a directory" ; false)

find  $UNDERLAY_DIR -iname '*.zip' \
	| grep -E '[^[:alnum:]/_\.-]' \
	&& (echo "^^ Are the above filenames safe?"; exit 1)

BASE=$PWD
COUNT=0
for f in $(find  $BASE/$UNDERLAY_DIR -iname '*.zip') ; do
	echo "Cleaning comments in $f"
	tmp=`mktemp -d --tmpdir=$BASE` || exit 2
	cd $tmp
	unzip -q $f
	chmod a+rw ./*
	egrep -r -l -Z '^## ' * \
		| xargs -r -0 -n 1 \
		  sed -i -e '1,8{/^## \(Please edit system\|For more information\|.*MoinMoin:MoinDev\/Translation\)/d}'
		# ^^ search and drop notes in the 8 first lines.
	chmod a-w ./*
	chmod a+w MOIN_PACKAGE
	rm $f
	zip -q $f *
	cd $BASE
	rm -Rf $tmp
	COUNT=$(($COUNT + 1))
done

[ $COUNT -eq 0 ] && (echo "Error: No Zip (language pages) found" ; false) || true
