#!/bin/bash -eu
#
# Wrapper around dpkg-buildpackage
#

function out()
{
    local rc=${?}

    trap - EXIT INT TERM HUP
    if [ ${rc} -ne 0 ] ; then
        echo "Error: Script failed" >&2
    fi

    exit "${rc}"
}

function usage()
{
	cat <<EOF
Usage: $(basename "${0}") [-c] [-h] [--skip-branch-check] [--skip-lintian] [option...]

Build a Debian package.

Optional arguments:
  -c, --clean          Clean the git repo before the build.
  -h, --help           Show this help text and exit.
  --skip-branch-check  Skip branch name check.
  --skip-lintian       Skip lintian.
  option...            Additional options for dpkg-buildpackage.
EOF
}

clean=0
skip_branch_check=0
skip_lintian=0

while [ ${#} -gt 0 ] ; do
	case "${1}" in
		-c|--clean)
			clean=1
			;;
		-h|--help)
			usage
			exit
			;;
		--skip-branch-check)
			skip_branch_check=1
			;;
		--skip-lintian)
			skip_lintian=1
			;;
		*)
			break
			;;
	esac
	shift
done


if [ ${clean} -eq 1 ] && [ -n "$(git status --porcelain)" ] ; then
	git reset --hard HEAD
	git clean -dxf
fi

if [ -n "$(git status --porcelain)" ] ; then
    echo "Repo is unclean" >&2
    exit 1
fi

# Check that the release and branch names match
release=$(dpkg-parsechangelog -SDistribution)
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "${release}" != "${branch}" ] ; then
	echo "Release and branch name mismatch (${release} != ${branch})" >&2
	if [ ${skip_branch_check} -eq 0 ] ; then
		exit 1
	fi
fi

trap out EXIT INT TERM HUP

debian/scripts/create-quilt-series
debian/scripts/apply-quilt-series

# Default dpkg-builpackage options
opts=(
    "-i"  # Exclude revision control files and directories (diff)
    "-I"  # Exclude revision control files and directories (tarball)
)

# Check if the orig tarball should be included
version=$(dpkg-parsechangelog -SVersion)
prev_version=$(dpkg-parsechangelog -SVersion -o1 -c1)
if [ "${version%-*}" != "${prev_version%-*}" ] ; then
    opts+=("-sa")  # Include the original source tarball
fi

ts=$(date)

dpkg-buildpackage "${opts[@]}" "${@}"

if [ ${skip_lintian} -eq 0 ] ; then
	package=$(dpkg-parsechangelog -SSource)
	while IFS= read -r changes ; do
		echo "Run lintian on ${changes}"
		lintian "${changes}"
	done < <(
		find ../"${package}"_"${version}"_*.changes -type f -newermt "${ts}"
	)
fi
