#!/bin/bash -e

CONFFILE=/etc/pkgstriptranslations.conf

srcname=$(dpkg-parsechangelog | grep ^Source: | cut -f 2 -d\ )
version=$(dpkg-parsechangelog | grep ^Version: | cut -f 2 -d\ )
version=${version#*:}

# read value for attribute $2 in dpkg control style file $1
# return value in $RET
readctrl() {
    _line=$(grep "^$2: " $1) || true
    RET=${_line#"$2: "}
}

#
# Execution starts here
#

# Check configuration: enabled?
[ -f "$CONFFILE" ] || exit 0
readctrl "$CONFFILE" "enable"
[ "$RET" = "true" ] || exit 0

if [ ! -f debian/control ]; then
    echo "pkgstriptranslations: Error: not in source package directory" 2>&1
    exit 1
fi

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

# check whether /CurrentlyBuilding is present; we have to check the suite in
# this case
if [ -f /CurrentlyBuilding ]; then
    readctrl "$CONFFILE" "components"
    stripcomponents="$RET"
    unset ignore_invalid_cb
    readctrl "$CONFFILE" "invalid_currentlybuilding"
    [ "$RET" != "ignore" ] || ignore_invalid_cb=1

    readctrl "/CurrentlyBuilding" "Package"

    if [ "$RET" != $srcname ]; then
        echo "pkgstriptranslations: inconsistent /CurrentlyBuilding file, Package: value is $RET (should be $srcname)" >&2
        [ "$ignore_invalid_cb" ] || exit 1
    fi

    readctrl "/CurrentlyBuilding" "Component"
    if [ -z "$RET" ]; then
        echo "pkgstriptranslations: inconsistent /CurrentlyBuilding file, Component: value is empty" >&2
        [ "$ignore_invalid_cb" ] || exit 1
    fi

    unset dostrip
    for c in $stripcomponents; do
        if [ $c = "$RET" ]; then dostrip=1; fi
    done
else
    dostrip=1
fi

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

readctrl "$CONFFILE" "nostrip"
NOSTRIP="$RET"

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

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

    # read blacklist file and test all regexps
    unset blacklisted
    if [ -f /etc/pkgstriptranslations.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/pkgstriptranslations.blacklist
    fi

    pushd "$PKGDIR" >/dev/null
    find -type f -name "*.mo" -exec install -D -m 644 '{}' "$tmp/$PKGNAME/{}" \;
    popd >/dev/null

    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 \( -name "*.po" -o -name "*.pot" \) -empty`"
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

# copy po/pot files
find \( -name "*.po" -o -name "*.pot" \) -exec install -D -m 644 '{}' "$tmp/source/{}" \;

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

# create tarball if there are files and a posuffix is given
readctrl "$CONFFILE" "posuffix"
posuffix=$RET
arch=$(dpkg --print-installation-architecture)
if [ "$posuffix" ]; then
    tarball=${srcname}_${version}_${arch}_$posuffix.tar.gz

    if [ "$(ls $tmp | wc -l)" -gt 0 ]; then
        echo -n "pkgstriptranslations: preparing translation tarball $tarball..."
        pwd=$(pwd)
        pushd $tmp > /dev/null
        if [ -f "$pwd/../$tarball" ]; then
            echo -n "(copying contents of old tarball) "
            tar xz < "$pwd/../$tarball"
        fi
        tar c . | gzip -9 > "$pwd/../$tarball"
        popd > /dev/null
        numfiles=$(tar tz < "$pwd/../$tarball" | wc -l)
        echo "done ($numfiles files)"
        dpkg-distaddfile "$tarball" raw-translations -
    else
        echo "pkgstriptranslations: no translation files, not creating tarball"
    fi
else
    echo "pkgstriptranslations: no posuffix given in $CONFFILE, not creating tarball"
fi

