#!/bin/sh
#
# change-textdomain -- hack the text domain of a specified campaign 
#
# Code by Eric S. Raymond, May 2007.
#
# Ivanovic's original specification from FR #9039.
# 
#  would be nice to have a tool maybe named utils/textdomain2textdomain
#  that is able to change the textdomain of a given campaign. The params
#  that the script should support are these: campaignname,
#  oldtextdomain, newtextdomain
# 
#     * asumptions: campaign lies in data/campaigns, current textdomain
#       is wesnoth-oldtextdomain, the translation files are existing
#       (and maybe already uploaded) in the folder
#       /po/wesnoth-oldtextdomain/.
# 
# Here is what is to be done for the campaign named 'foo' to move from
# the textdomain wesnoth-oldtextdomain to wesnoth-newtextdomain:
# 
# 1) search in all files in data/campaigns/foo* (yes, the .cfg file, too)
# for this string:
# 
# #textdomain wesnoth-oldtextdomain
# 
# 2) replace (be aware, there might be " " around the part after name=)
# 
# [textdomain]
# name=wesnoth-oldtextdomain
# [/textdomain]
# 
# with
# 
# [textdomain]
# name=wesnoth-newtextdomain
# [/textdomain]
# 
# 3) move the folder po/wesnoth-oldtextdomain to
#    po/wesnoth-newtextdomain (svn mv if the file is already under
#    version conrol, normal mv if it is not)
# 
# 4) move the file (folders are already changed)
#    po/wesnoth-newtextdomain/wesnoth-oldtextdomain.pot to
#    po/wesnoth-newtextdomain/wensoth-newtextdomain.pot (via svn mv if
#    files are under version control already, --force will be needed,
#    since it is a 2nd move)
# 
# 5) replace wesnoth-oldtextdomain by wesnoth-newtextdomain in
#    po/wesnoth-newtextdomain/Makevars
# 
# 6) replace wesnoth-oldtextdomain by wesnoth-newtextdomain in po/Makefile.am
# 
# 7) replace wesnoth-oldtextdomain by wesnoth-newtextdomain in configure.ac 
#
# Later, I added the -t option to insert a default textdomain in files that
# don't have it.
#
# Note: This script is rather sensitive to the current directory layout --
# the campaigns directory is required to be under data, po directly under
# toplevel, etc. It will silently break if these assumptions stop being true.

usage()
{
    cat <<EOF
Usage: change_textdomain {-h | -t | campaign-name oldtextdomain newtextdomain}
Options:
    -h, --help                   Emit this help message and quit.
    -d, --dryrun                 Echo shell actions without performing them.
    -t                           Insert "#textdomain wesnoth" in all cfg files
                                 without textdomain, no other params required.
Requires as first argument a campaign name.
Requires as second and third arguments old and new text domain names. 

Call from the top-level directory of mainline.
EOF
}

die()
{
    echo "change_textdomain: $1"
    exit 1
}

replace()
# Replace a specified string with another in any number of files
{
    left="$1"; right="$2"; shift; shift;

    for file in $*
    do
	if grep "$left" $file >/dev/null 2>&1
	then
	    overwrite $file sed "s@$left@$right@g" $file
	fi
    done
}

overwrite()
# Replace a file with itself filtered by a command
{
    opath=$PATH
    PATH=/bin:/usr/bin

    file=$1; shift
    new=/tmp/over$$; old=/tmp/under$$
    trap 'rm -f $new $old ; exit 1' 1 2 15

    if PATH=$opath "$@" >$new
    then
            cp $file $old		# save the old file
            trap '' 1 2 15		# We are committed; ignore signals
            cp $new $file
    else
            echo "overwrite: $1 failed, $file unchanged" 1 >&2
            exit 1
    fi
    rm -f $new $old
}

svnmove()
# Move a file, whether under version control or not
{
    if svn mv --force $1 $2
    then
        :
    else
        mv $1 $2
    fi
}

add_textdomain()
# Add '#textdomain wesnoth' to files that don't have it. 
{
    for file in `find data -name "*.cfg" -print | grep -v 'data/languages'`
    do
	if grep '#textdomain' $file >/dev/null
	then
	    :
	else
	    ${do} echo "#textdomain wesnoth" >/tmp/ctd$$.tmp
	    ${do} cat $file >>/tmp/ctd$$.tmp
	    ${do} cp /tmp/ctd$$.tmp $file
	fi
    done
}

if [ "$1" = "-h" -o "$1" = "--help" ]
then
    usage
    exit 1
elif [ "$1" = "-d" -o "$1" = "--dryrun" ]
then
    do=echo
    shift
elif [ "$1" = "-t" ]
then
    add_textdomain
    exit 0
fi

campaign=$1
oldtextdomain=$2
newtextdomain=$3
echo "campaign=$campaign, ols=$oldtextdomain, new=$newtextdomain"
if [ "$campaign" = "" ]
then
    usage
    die "a campaign name is required."
elif [ "${oldtextdomain}" = "" ]
then
    usage
    die "an old textdomain name is required."
elif [ "${newtextdomain}" = "" ]
then
    usage
    die "a new textdomain name is required."
else
    # First, hack scenario and autoconf files
    ${do} replace ${oldtextdomain} ${newtextdomain} \
	configure.ac \
	po/Makefile.am \
	po/${oldtextdomain}/Makevars \
	data/campaigns/${campaign}.cfg \
	`find data/campaigns/${campaign} -name "*.cfg" -print`

    # Then do the .pot and folder moves
    ${do} svnmove po/${oldtextdomain}/${oldtextdomain}.pot po/${oldtextdomain}/${newtextdomain}.pot
    ${do} svnmove po/${oldtextdomain} po/${newtextdomain}
fi
