#!/bin/bash -e

CONFFILE=/etc/pkgbinarymangler/striptranslations.conf

. /usr/share/pkgbinarymangler/common

# ignore language packs
if echo "$srcname" | grep -q ^language-pack; then
    echo "pkgstriptranslations: building language pack, doing nothing"
    exit 0
fi

# ignore backports
if dpkg-parsechangelog | grep -q 'Distribution:.*backport'; then
    echo "pkgstriptranslations: building backport, doing nothing"
    exit 0
fi

# check whether /CurrentlyBuilding is present; so we can check the component
if [ -f /CurrentlyBuilding ]; then
    readctrl "$CONFFILE" "components"
    stripcomponents="$RET"
    unset dostrip
    readctrl "/CurrentlyBuilding" "Component"
    for c in $stripcomponents; do
        if [ $c = "$RET" ]; then dostrip=1; fi
    done
else
    dostrip=1
fi

# determine tarball name
readctrl "$CONFFILE" "posuffix"
posuffix=$RET
arch=$(dpkg --print-architecture)
[ "$posuffix" ] || posuffix=translations
tarball=${srcname}_${version}_${arch}_$posuffix.tar.gz
tarball_static=${srcname}_${version}_static_${posuffix}.tar.gz

# make sure that only one pkgstriptranslations runs at a time, so that we don't
# corrupt the tarball
lockfile-create --retry 20 "../$tarball"

tmp=$(mktemp -t -d pkgstriptranslations.XXXXXX)
trap "lockfile-remove '../$tarball'; rm -rf $tmp" 0 1 2 3 7 10 13 15

# process every directory that looks like a package, remove all
# usr/share/locale/*.mo files, and copy them into the tarball
for PKGCTL in $(find -regex '\./debian/.*/DEBIAN/control$'); do
    # skip installer udebs
    readctrl $PKGCTL Section
    if [ "$RET" = debian-installer ]; then
        continue
    fi

    readctrl $PKGCTL Package
    PKGNAME=$RET
    PKGDIR=$(dirname $(dirname $PKGCTL))
    echo "pkgstriptranslations: processing control file: $PKGCTL, package $PKGNAME, directory $PKGDIR"

    # read blacklist file and test all regexps
    unset blacklisted
    if [ -f /etc/pkgbinarymangler/striptranslations.blacklist ]; then
        while read regex; do
            if echo "$PKGNAME" | egrep -xq "$regex"; then
                blacklisted=1
                echo "pkgstriptranslations: $PKGNAME is blacklisted, not stripping"
                break
            fi
        done < /etc/pkgbinarymangler/striptranslations.blacklist
    fi

    # strip translations from .desktop files if they have a gettext domain (since
    # inline translations are preferred)
    for desktop in `find "$PKGDIR" -type f -name "*.desktop"`; do
	if grep -q '^X-.*-Gettext-Domain=' "$desktop"; then
	    echo "$desktop: stripping translations"
	    sed -ri '/^(Name|GenericName|Comment)\[/d' "$desktop";
	    # adapt md5sums file
	    if [ -f $PKGDIR/DEBIAN/md5sums ]; then
		P=${desktop#$PKGDIR}
		P=${P#/}
		(cd "$PKGDIR"; sed -i "s#^[[:alnum:]]\+  $P\$#`md5sum $P`#" DEBIAN/md5sums)
	    fi
	else
	    echo "$desktop: does not have gettext domain, not stripping"
	fi
    done

    # save/strip GNOME help files if we don't have a static tarball yet
    if [ "$dostrip" -a -z "$blacklisted" -a -d "$PKGDIR/usr/share/gnome/help" -a ! -f "../$tarball_static" ]; then
        pushd "$PKGDIR" >/dev/null
	(find usr/share/gnome/help -type f ! -wholename '*/C/*'; [ ! -d usr/share/omf ] || find usr/share/omf -type f -name '*.omf' ! -name '*-C.omf') | while read f; do
	    install -D -m 644 "$f" "$tmp/_static/$PKGNAME/$f"
	    rm "$f"
	    dest=${f/gnome\/help/gnome\/help-langpack}
	    dest=${dest/share\/omf/share\/omf-langpack}
	    ln -s "/$dest" "$f"
	    [ ! -f DEBIAN/md5sums ] || sed -i "\& $f$&d" DEBIAN/md5sums
	done
        popd >/dev/null
    fi

    [ -d "$PKGDIR/usr/share/locale" ] || {
        echo "pkgstriptranslations: $PKGNAME does not contain translations, skipping"
        continue
    }

    # save *.mo files if we don't have a tarball yet
    if [ ! -f "../$tarball" ]; then
        pushd "$PKGDIR" >/dev/null
        find -type f -name "*.mo" -exec install -D -m 644 '{}' "$tmp/$PKGNAME/{}" \;
        popd >/dev/null
    fi

    if [ "$dostrip" -a -z "$blacklisted" ]; then
        find "$PKGDIR/usr/share/locale" -type f -name "*.mo" -exec rm '{}' \;
        find "$PKGDIR/usr/share/locale" -depth -type d -empty -exec rmdir '{}' \; || true

        # adapt md5sums file
        if [ -f $PKGDIR/DEBIAN/md5sums ]; then
            sed -i '/usr\/share\/locale\/.*\.mo$/d' $PKGDIR/DEBIAN/md5sums
        fi
    fi
done

# check for empty files, as they indicate a package bug
EMPTYFILES="`find -mindepth 1 -name '.*' -prune -o \( -name "*.po" -o -name "*.pot" \) -empty -print`"
if [ -n "$EMPTYFILES" ]; then
    cat <<EOF
pkgstriptranslations: The following PO/POT files are empty. This is known to
cause trouble in the translation importer and generally indicates a package
bug:

$EMPTYFILES
EOF
    exit 1
fi

# create tarball if there are files
if [ ! -f "../$tarball" ]; then
    # copy po/pot files
    find \( -name "*.po" -o -name "*.pot" -o -name "*.ts" \) -exec install -D -m 644 '{}' "$tmp/source/{}" \;

    # grab sdf files from OO.o
    if [ "$srcname" = "openoffice.org" ]; then
        find debian/ -name "*.sdf" -exec install -D -m 644 '{}' "$tmp/source/{}" \;
    fi

    # grab XPI files for Mozilla-ish packages
    if [ -d debian/lp-export-xpis ]; then
        install -d $tmp/source/
        cp -a debian/lp-export-xpis $tmp/source/
    fi

    if [ "$(ls -I _static $tmp | wc -l)" -gt 0 ]; then
        echo -n "pkgstriptranslations: preparing translation tarball $tarball..."
        pwd=$(pwd)
        pushd $tmp > /dev/null
        tar c --exclude=_static . | gzip -9 > "$pwd/../$tarball"
        numfiles=$(find -type f | wc -l)
        popd > /dev/null
        echo "done ($numfiles files)"
        dpkg-distaddfile "$tarball" raw-translations -
    else
        echo "pkgstriptranslations: no translation files, not creating tarball"
    fi
else
    echo "pkgstriptranslations: tarball already exists"
fi

# create static tarball if there are files
if [ ! -f "../$tarball_static" -a -d $tmp/_static ]; then
    echo -n "pkgstriptranslations: preparing static translation tarball $tarball_static..."
    pwd=$(pwd)
    pushd $tmp/_static > /dev/null
    tar c . | gzip -9 > "$pwd/../$tarball_static"
    numfiles=$(find -type f | wc -l)
    popd > /dev/null
    echo "done ($numfiles files)"
    dpkg-distaddfile "$tarball_static" raw-translations-static -
else
    echo "pkgstriptranslations: static tarball already exists"
fi
