#!/bin/bash
#
# Hook to run lintian inside the cowbuilder environment.
#

BUILDDIR="${BUILDDIR:-/tmp/buildd}"

if [ "${LINTIAN:-}" != "true" ] ; then
  echo "Skipping lintian (LINTIAN is not 'true')"
  exit 0
fi

if [ -n "${LINTIAN_OPTIONS:-}" ] ; then
  echo "*** Using provided LINTIAN_OPTIONS $LINTIAN_OPTIONS ***"
fi

set -ex -o pipefail
apt-get -y "${APTGETOPT[@]}" install lintian
lintian --version

package_dir=$(cd "$BUILDDIR"/*/debian/.. 2>/dev/null && pwd -P)
PACKAGE_NAME=$(basename "$package_dir")

echo "Found package name: $PACKAGE_NAME"

# Requires a .pbuilderrc with:
#  export ADDITIONAL_BUILDRESULTS=(../*.lintian.txt)
lintian_out=${BUILDDIR}/${PACKAGE_NAME}.lintian.txt

DSC_EXIT=0
if ls "${BUILDDIR}"/*.dsc &>/dev/null ; then
  su -s /bin/bash -c "set -o pipefail; lintian ${LINTIAN_OPTIONS:-} \"${BUILDDIR}\"/*.dsc 2>&1 | tee \"${lintian_out}\"" - pbuilder \
    || DSC_EXIT=$?
else
  echo "WARNING: no dsc file, not executing lintian on source package files"
fi

CHANGES_EXIT=0
su -s /bin/bash -c "set -o pipefail; lintian ${LINTIAN_OPTIONS:-} \"${BUILDDIR}\"/*.changes 2>&1 | tee -a \"${lintian_out}\"" - pbuilder \
  || CHANGES_EXIT=$?

# We strip ANSI sequences in case lintian got --color forced
sed -i 's%\x1B\[[0-9;]*\?[mGKH]%%g' "${lintian_out}"

if [[ "${DSC_EXIT:-0}" =~ [01] ]] && [[ "${CHANGES_EXIT:-0}" =~ [01] ]] ; then
  echo "Finished lintian execution [DSC_EXIT=${DSC_EXIT} CHANGES_EXIT=${CHANGES_EXIT}]"
  exit 0
else
  echo "Error: lintian execution failed [DSC_EXIT=${DSC_EXIT} CHANGES_EXIT=${CHANGES_EXIT}]" >&2
  exit 2
fi
