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

#
# 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"
    fi
    SRCDIR=$1

    # search for sourcecode-files
    SRCFILES=`find $SRCDIR -type d -name testsuite -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 "*.rb"  `

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

    for F in $SRCFILES; do
        # strip comments from the files and match [_]_( "..." )
        # 1. perl: strip one-line-comments
        # 2. perl: match for _( "...") and __("..." ) (multiline too)
	# problems left:
	# - false matches of comments inside strings
	# - uncaught _( at line beginning
        MATCH=`perl -n -e 'print "$ARGV: $_" if not /(\/\/|#)/' $F | \
               perl -n -e 'print "$_" if /[^[: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=`egrep '^[[:space:]]*<?[Tt]extdomain>?' $F | head -n 1 | \
            sed 's/^[[:space:]]*<\?[Tt]extdomain[[:space:]]*[=: \
                 '\''"(>]*[[:space:]]*\([-_[:alnum:]]*\).*/\1/'`;
        if [ -z $D ]; then
            ERR="$PWD/$F $ERR" ;
        else
            DOMAINS="$D:$F\n$DOMAINS" ;
        fi ;
    done
}

