### sourced by 'check-textdomain' and 'y2makepot' ### -*- sh -*-

# Try reading the default text domain from the TEXTDOMAIN file
#    Argument:  $1 -> directory to search
#               $F -> the source file
#    Returns:   $D -> the found text domain
function default_textdomain()
{
    local TD="$1/TEXTDOMAIN"
    if [ -f "$TD" ]; then
        echo "File $F: Using default text domain from $TD"
        D=`cat "$TD"`
    fi
}

#
# get_domains_and_err():
#     Argument:  $1        -> directory to search
#     Returns:   $DOMAINS  -> all text-domains found in $SRCDIR
#                $ERR      -> filenames without text-domain, but using [_]_tr()
#

function get_domains_and_err()
{
    if [ -z "$1" ]; then
        echo `basename $0`": argument missing" >&2
        exit 1
    fi
    SRCDIR=$1

    # search for sourcecode-files
    SRCFILES=`find $SRCDIR -type d -name testsuite -prune , \
                           -type d -name test -prune , \
		  	   -type d -name .svn -prune , \
                           -type f -name "*.ycp" \
                                -o -name "*.pm"  \
                                -o -name "*.c"   \
                                -o -name "*.cc"  \
                                -o -name "*.cpp" \
                                -o -name "*.erb" \
                                -o -name "*.glade" \
                                -o -name "*.ui" \
                                -o -name "*.ycontrol.xml" \
                                -o -name "*.ycontrol.xsl" \
                                -o -name "*.rb"  `

    if test "$?" != "0"; then
        echo "Error: check-pot terminated unexpected." >&2
        exit 1
    fi

    for F in $SRCFILES; do
        # strip comments from the files and match [_]_( "..." )
        # 1. perl: strip one-line-comments, except ruby "string #{interpolation}"
        # 2. perl: match for _( "...") and __("..." ) (multiline too),
        #          or "<label>" used in XML (*.glade) files
        # NOTE: be less strict here, if e.g. a gettext call is in a trailing comment:
        # "foo" # _("not translated")
        # then it will be ignored by the extractor later, we can only get a false
        # warning about a missing text domain in the file. That's more acceptable
        # then a completely missing translation because of too strict comment filtering.
	# problems left:
	# - false matches of comments inside strings
	# - uncaught _( at line beginning
        MATCH=`perl -n -e 'print "$ARGV: $_" if not /(^\s*\/\/|^\s*#[^\{])/' $F | \
               perl -n -e 'print "$_" if /<label>|<property\s+name\s*=\s*"text"|[^[:alnum:]_"<](_|__|gettext)\(/../\)/'`
        if [ -n "$MATCH" ]; then
            TR_FILES="$F $TR_FILES" ;
        fi
    done

    # we can specify additional files to .pot creation
    POTFILES=`test -e $SRCDIR/POTFILES && grep -v '^$' $SRCDIR/POTFILES | sed "s,^,$SRCDIR/," `

    DOMAINS="" ;
    for F in $TR_FILES $POTFILES; do
        D=`grep -E '^[[:space:]]*<?[Tt]extdomain>?' $F | head -n 1 | \
            sed 's/^[[:space:]]*<\?[Tt]extdomain[[:space:]]*[=: \
                 '\''"(>]*[[:space:]]*\([-_[:alnum:]]*\).*/\1/'`;
        # if the text domain is not specified directly in the file then
        # search for the TEXTDOMAIN file in the source directory or in its parents
        if [ -z "$D" ]; then
            DIR=`dirname "$F"`
            # search in the parent dirs
            while [ -z "$D" -a "$DIR" != "." ]; do
                default_textdomain "$DIR"
                DIR=`dirname "$DIR"`
            done
            # search in the top level dir
            if [ -z "$D" ]; then
                default_textdomain "$DIR"
            fi
        fi

        if [ -z "$D" ]; then
            ERR="$PWD/$F $ERR" ;
        else
            DOMAINS="$D:$F\n$DOMAINS" ;
        fi ;
    done
}

