#!/bin/bash -e

CONFFILE=${PKGBINARYMANGLER_CONF_DIR:-/etc/pkgbinarymangler}/stripfiles.conf

. ${PKGBINARYMANGLER_COMMON_PATH:-/usr/share/pkgbinarymangler}/common

# don't do anything for PPA builds
if [ -f "$BUILDINFO" ]; then
    if grep -qs '^Purpose: PPA' "$BUILDINFO"; then
        if grep -q '/oem-archive' ${PKGBINARYMANGLER_APT_CONF_DIR:-/etc/apt}/sources.list; then
	    echo "INFO: Running pkgstripfiles for OEM PPA build"
	else
	    echo "INFO: Disabling pkgstripfiles for PPA build"
	    exit 0
	fi
    fi
fi

# find package name
while [ $# -gt 0 ]; do
    if [ -f "$1"/DEBIAN/control ]; then
        PKGCTL="$1"/DEBIAN/control
        break
    else
        shift
    fi
done

if [ -z "$PKGCTL" ]; then
    echo "pkgstripfiles: did not find a package name argument or control file, skipping"
    exit 0
fi

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

cd "$PKGDIR"

# remove buildinfo files and upstream changelogs; take care to keep native
# changelogs
dch=usr/share/doc/$PKGNAME/changelog.Debian.gz
if [ ! -e "$dch" ] && [ ! -h "$dch" ]; then
    dch=usr/share/doc/$PKGNAME/changelog.gz
fi

if [ -d usr/share/doc ]; then
    (find usr/share/doc -type f \( -name buildinfo.gz -o -iname 'changelog.*' -o -iname changes -o -iname changes.gz -o -iname '*.changes.gz' \) -a ! -name `basename $dch` ) | while read f; do
        echo ".. removing $f"
        rm "$f"
        [ ! -f DEBIAN/md5sums ] || sed -i "\& $f$&d" DEBIAN/md5sums
    done
fi

# Only keep the topmost ten entries in Debian changelogs. If we truncate, add a
# pointer to apt-get changelog.
record_sep="^[^ ]+ (.*) .*; urgency"
if [ -e $dch ]; then
    record_count=0
    changelog=""
    gzip -cd $dch | (while read; do
        if [[ "$REPLY" =~ $record_sep ]]; then
	    ((++record_count))
	    if [ "$record_count" -eq 11 ]; then
		echo "pkgstripfiles: Truncating changelog to topmost ten records"
		echo -e "${changelog}# For older changelog entries, run 'apt-get changelog $PKGNAME'" | gzip -9n > $dch
		if [ -f DEBIAN/md5sums ]; then
		    MD5=`md5sum "$dch"`
		    sed -i "s%^.*  $dch\$%$MD5%" DEBIAN/md5sums
		fi
		break
	    fi
	fi
	changelog="$changelog$REPLY\n"
    done)
fi

# skip PNG modification for games; they often rely on their particular image
# format
if [ "${SECTION%games}" != "$SECTION" ]; then
    echo "pkgstripfiles: Skipping PNG optimization for package in games section."
    exit 0
fi

# also skip when disabling explicitly
if [ -n "$NO_PNG_PKG_MANGLE" ]; then
    echo "pkgstripfiles: Disabled PNG optimization for package."
    exit 0
fi

# optipng/advancecomp
find -type f -name '*.png' | while read f; do
    if ! optipng -o4 -preserve "$f"; then
	echo "WARNING: optipng failed on $f, ignoring" >&2
	continue
    fi
    if ! advpng -z4 "$f"; then
	echo "WARNING: advpng failed on $f, ignoring" >&2
	continue
    fi

    # update md5sum
    if [ -f DEBIAN/md5sums ]; then
	f=${f#./}
	MD5=`md5sum "$f"`
	sed -i "s%^.*  ${f//%/\%}\$%${MD5//%/\%}%" DEBIAN/md5sums
    fi
done

