#!/bin/sh
#
# /usr/local/bin/localezmangle
# Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
#
# $Id: localezmangle,v 1.8 2002-12-05 21:41:56 jonas Exp $
#
# Extract, rename and auto-mangle eZ Publish 2.2.x templates and languages
#

#set -e

prg=`basename $0`

if [ -z "$1" -o -z "$2" -o -z "$3" ]; then
	echo "$prg: Extract, rename and auto-mangle eZ Publish 2.2.x templates and"
	echo "    languages"
	echo
	echo "Usage: $prg origname targetname targetdir [origdir [sedfile]]"
	echo
	echo "Options:"
	echo "    origname:     Search string"
	echo "    targetname:   Replacement string"
	echo "    targetdir:    Where to store mangled file hierarchy"
	echo "    origdir:      Where to look for original files"
	echo
	echo "1) <targetdir> is created if it doesn't exist. If <origname> is"
	echo "provided it is considered a \"first run\", and files matching"
	echo "<origname> are copied from <origdir> to <targetdir>."
	echo
	echo "2) Path + filename is matched against <origname> and replaced with"
	echo "<targetname>."
	echo
	echo "3) All files in <targetdir> is run through the sed rulesfile <sedfile>."
	echo "If <sedfile> not provided, the file mangle.<targetname>.sed is looked"
	echo "for in current dir."
	echo
	echo "Examples:"
	echo "    $prg /default/ /mytemplate/ /tmp/my_ez_tpl /var/www/ezsite"
	echo "    $prg en_GB en_US_slang ezlang/2.2.7-en_US_slang ez/2.2.7"
	exit 1
fi

origname=$1
targetname=$2
targetdir=$3
origdir=$4
sedrules=mangle.`echo $targetname | sed 's/[^_[:alnum:]-]//g'`.sed

if [ ! -f $sedrules ]; then
	echo "WARNING: sed rulesfile \"$sedrules\" not found. Rename only..."
fi

if [ -n "$origdir" -a -d "$origdir" ]; then
	if [ -d "$targetdir" ]; then
		echo "ERROR: targetdir exists and origdir provided! (either remove targetdir"
		echo "or only update existing targetdir (by not providing origdir option)."
		echo "Exiting...!"
		exit 1
	fi
	# Isolate all template files
	for x in `cd $origdir && find * -type f | grep -v cache | grep "$origname"`; do mkdir -p $targetdir/`dirname $x`; cp -a $origdir/$x $targetdir/`dirname $x`; done
elif [ -d "$targetdir" ]; then
	echo "NOTICE: origdir not provided. Working with existing targetdir..."
else
	echo "ERROR: origdir not provided, and targetdir doesn't exist. Exiting...!"
	exit 1
fi

# rename origname to targetname (ignored on multiple runs)
for x in `cd $targetdir && find * -type d | sed 's!$!/!' | grep "$origname"`; do
	mv $targetdir/$x $targetdir/`echo $x | sed -e "s	$origname	$targetname	g" -e 's!/$!!'`
done
for x in `cd $targetdir && find * -not -type d | grep "$origname"`; do
	mv $targetdir/$x $targetdir/`echo $x | sed "s	$origname	$targetname	g"`
done

if [ ! -f $sedrules ]; then
	exit 1
fi

# Use super-sed if available (wins a few seconds)
ssed=`which ssed`

# TODO: only touch file if mangled
for file in `find $targetdir -type f`; do
	if [ -n "$ssed" ]; then
		cat $file | $ssed -f $sedrules -i $file
	else
		cat $file | sed -f $sedrules > $file.tmp
		mv $file.tmp $file
	fi
done

echo "Done. Now patch manually using mangle.$targetname.diff (if available)..."