#!/bin/bash
#
# File:		y2makepot
# Package:	devtools
# Summary:	Build all translation files (*.pot) from a module
# Authors:	Holger Macht <hmacht@suse.de>
#
# Call this from a module toplevel directory to generate all
# translation files (*.pot)

# ##############################################################################
# -- function defininitions -- start -------------------------------------------
# ##############################################################################

# same function for this file and check-textdomain: get_domain_and_err()
CWD=`dirname $0`
. $CWD/gettextdomains


function xgettext_call()
{
    MODULE=$1
    FILES=$2

    echo "Creating ./$MODULE.pot from $FILES ...";
    $XGETTEXT --no-wrap --add-comments --add-location \
	--keyword=_ --keyword=_:1,2 --keyword=__ \
	--foreign-user \
	--copyright-holder="SuSE Linux Products GmbH, Nuernberg" \
	--default-domain=$MODULE --output=$MODULE.pot $FILES
}

function usage()
{
    echo
    echo -e "create *.pot files from a source tree.";
    echo -e "\nOptions:\n\t-c\t Check in the newly created files to SVN"
    echo -e "\t-s\t Specify the source dir where to look for translatable files (recursive)"
    echo -e "\nYou can specify additional files to .pot creation in $SRCDIR/POTFILES"
    echo

    exit 0
}


function generate_potfiles()
{
    # call function in gettextdomains
    get_domains_and_err $SRCDIR

    # $ERR contains the files without textdomain (see gettextdomains)
    echo
    for F in $ERR; do
        echo "Warning: empty textdomain in $F" ;
        echo $F > _MISSING_TEXTDOMAIN_`basename $F` ;
    done
    echo

    # all textdomains found by gettextdomains
    DOMAINS=`echo -en $DOMAINS | LC_COLLATE=C sort` ;
    MODULE=${DOMAINS%%:*};

    # gather files that share a domain to FILES and call xgettext
    FILES="";
    for I in $DOMAINS; do
	D=${I%%:*} ;
	F=${I#*:} ;

	if [ "$D" != "$MODULE" ]; then
	    POT_DST="$POT_DST $MODULE.pot"

	    xgettext_call "$MODULE" "$FILES";
	    echo
	    MODULE=$D;
	    FILES=$F;
	else
	    FILES="$FILES $F" ;
	fi

    done

    POT_DST="$POT_DST $MODULE.pot"
    xgettext_call "$MODULE" "$FILES"
    echo
}



function checkin_potfiles()
{
    ADDED=""
    CHANGED=""

    for f in $POT_DST; do
	MODIFIER="`svn status $f | cut -d' ' -f1`"

        # file has changed in repository
	if test "$MODIFIER" == "M"; then
	    echo "$f is already under version control and has changed."
	    CHANGED="$CHANGED $f"
        # file has been added already
	elif test "$MODIFIER" == "A"; then
	    echo "$f has already been added."
	    ADDED="$ADDED $f"
	# file is not under version control
	else
	    if test "`svn add $f | cut -d' ' -f1`" == "A"; then
		echo "Added $f to svn repository."
		ADDED="$ADDED $f"
	    else
		echo "warning: Could not add $f to svn repository"
	    fi
	fi
    done

    if test -n "$ADDED" -o -n "$CHANGED"; then
        # commit the added or changed files now
        svn commit --message "Committed potfile(s) $f to repository" $ADDED $CHANGED

        if test "$?" == "0"; then
            echo "Committed files $ADDED $CHANGED to repository"
        else
	        echo "Error while committing files to repository";
        fi    
    fi
}

# -- function defininitions -- end ---------------------------------------------


# ##############################################################################
# -- main -- start -------------------------------------------------------------
# ##############################################################################

# define global variables
CHECKIN=1
XGETTEXT="xgettext"
SRCDIR="."
POT_DST=""

# parse command line options
while getopts "chfs::" opt; do
    case $opt in
	c)
	    CHECKIN=0
            ;;
	f)
	    echo "y2makepot: ignoring obsolete option -f"
            ;;
	s)
            SRCDIR="$OPTARG"
            ;;
	*)
	    usage
            ;;
    esac
done

generate_potfiles

if test $CHECKIN = 0; then
    checkin_potfiles
fi

echo
exit 0

# -- main -- end ---------------------------------------------------------------

