#!/bin/sh

set -e

# Regression test: pkgos-debpypi used to grep for "Apache Software License
# 2.0", which isn't part of the canonical Apache-2.0 license text, so a
# regular LICENSE file was never detected as Apache-2.0. Verify that the
# detection now works, auto-selects the license, and that the ITP text is
# written with it.

ME=$(basename $0)
WORK=$(mktemp -d)
trap 'rm -rf "${WORK}"' EXIT
TESTDIR=$(cd "$(dirname "$0")" && pwd)
. "${TESTDIR}/fixtures.sh"

cd "${WORK}"

fixture_repo ${WORK}/repo yes
fixture_json ${WORK}/pkgos-test-pkg.json
fixture_sdist ${WORK}
mock_git ${WORK} ${WORK}/repo
debpypi_env ${WORK}

"${SCRIPT}" pkgos-test-pkg >${WORK}/out.log 2>&1 <<EOF
1
EOF

check () {
	if ! grep -q "$2" "$1" ; then
		echo "${ME}: $1 doesn't contain: $2"
		exit 1
	fi
}

check ${WORK}/out.log 'Detected license: Apache-2.0 (from'
check ${WORK}/itp.txt 'License         : Apache-2.0'
check ${WORK}/itp.txt 'Package name    : python-pkgos-test-pkg'
check ${WORK}/python-pkgos-test-pkg-1.2.3/debian/copyright 'License: Apache-2.0'
if [ -e /usr/share/openstack-pkg-tools/pkgos.make ] ; then
	if ! [ -e ${WORK}/python-pkgos-test-pkg_1.2.3.orig.tar.xz ] ; then
		echo "${ME}: orig tarball was not generated"
		exit 1
	fi
fi

exit 0
