#!/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

# list of generated files which should be removed at the end
CLEAN_FILES=""

function gettext_call()
{
    MODULE=$1
    SOURCE_FILES=$2
    SOURCE_RUBY_FILES=$3

    # Is POT file merge needed? (both Ruby and non-Ruby files found)
    if [ -n "$SOURCE_FILES" -a -n "$SOURCE_RUBY_FILES" ]; then
        # only one header is sufficient when merging POT files,
        # more over it has undefined CHARSET which makes trouble
        # when merging with rxgettext POT file (declares UTF-8 charset)
        xgettext_call "$MODULE" "$SOURCE_FILES" "--omit-header"
        mv "$MODULE.pot" "$MODULE.xgettext.pot"

        rxgettext_call "$MODULE" "$SOURCE_RUBY_FILES"
        mv "$MODULE.pot" "$MODULE.rxgettext.pot"

        # merge the rxgettext POT with the xgettext POT file
        echo "Merging the .pot files..."
        msgcat -o "$MODULE.pot" "$MODULE.rxgettext.pot" "$MODULE.xgettext.pot"
        rm "$MODULE.xgettext.pot" "$MODULE.rxgettext.pot"
    else
        xgettext_call "$MODULE" "$SOURCE_FILES"
        rxgettext_call "$MODULE" "$SOURCE_RUBY_FILES"
    fi

    if [ -n "$CLEAN_FILES" ]; then
        echo "Removing generated files: $CLEAN_FILES"
        rm -f $CLEAN_FILES
        CLEAN_FILES=""
    fi
}

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

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

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

    if [ -n "$FILES" ]; then
        echo "Creating ./$MODULE.pot from $FILES ...";

        RXGETTEXT_PATH=`which $RXGETTEXT`

        # rubygem-gettext is a weak dependency, check if it is present
        if [ -x "$RXGETTEXT_PATH" ]; then
            $RXGETTEXT --copyright-holder="SuSE Linux Products GmbH, Nuernberg" \
                --add-comments --output=$MODULE.pot $FILES;

            # remove plural forms template, the dummy values are invalid
            # and do not pass the following check
            sed -i '/^"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;/d' $MODULE.pot

            # verify if the output is valid, rxgettext had some bugs resulting
            # in invalid file, be sure that GNU gettext is able to read it, huh :-(
            echo "Verifying $MODULE.pot validity..."
            msgfmt --check -o /dev/null $MODULE.pot

            if [ "$?" != "0" ]; then
                echo "ERROR: $MODULE.pot is not valid, it looks like a rxgettext bug..."
                exit 1
            fi

            echo "...OK"
        else
            echo "ERROR: $RXGETTEXT is missing; please install rubygem-gettext package" 1>&2
            exit 1
        fi
    fi
}

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="";
    RUBY_FILES=""

    for I in $DOMAINS; do
	D=${I%%:*} ;
	F=${I#*:} ;

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

            gettext_call "$MODULE" "$FILES" "$RUBY_FILES"

	    echo
	    MODULE=$D;
	    FILES=""
            RUBY_FILES=""
	fi

        # is it a Ruby file?
        if [[ "$F" =~ \.(erb|rb)$ ]]; then
            RUBY_FILES="$RUBY_FILES $F";
        else
            if [[ "$F" =~ \.glade$ ]]; then
                echo "Processing $F file..."
                GLADE_FILE="$F.translations.glade"
                xsltproc /usr/share/YaST2/control/control_to_glade.xsl "$F" > "$GLADE_FILE"
                FILES="$FILES $GLADE_FILE" ;
                CLEAN_FILES="$CLEAN_FILES $GLADE_FILE" ;
            else
                FILES="$FILES $F" ;
            fi
        fi
    done

    POT_DST="$POT_DST $MODULE.pot"
    gettext_call "$MODULE" "$FILES" "$RUBY_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"
RXGETTEXT="rxgettext"

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 ---------------------------------------------------------------

