#!/bin/bash

## Copyright (C) 2019 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -x

set -e

true "$0: start"

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

## travis.debian.net does not pass the CI environment variable to docker.
## build script reads CI variable.
CI=true
export CI

apt-get update

## Sanity tests.
apt-get --yes dist-upgrade
dpkg --configure -a

apt-get --yes install sudo virt-what lintian

## Debugging.
#systemd-detect-virt || true
virt-what || true
pwd_output="$(pwd)" || true
ls "$pwd_output" || true
ls /home || true

## The environment variables are not set here.
#true "$0: TRAVIS_DEBIAN_BUILD_DIR: $TRAVIS_DEBIAN_BUILD_DIR"
#true "$0: TRAVIS_DEBIAN_TARGET_DIR: $TRAVIS_DEBIAN_TARGET_DIR"

"${MYDIR}/build" --vm --debug --non-interactive

true "$0 INFO: ${MYDIR}/build success."

## TODO: do not hardcode hardened-vm-kernel

## Debugging.
find /var/lib/hardened-kernel/hardened-vm-kernel -type f -maxdepth 1

true "$0 INFO: OK."

## Move to /home/travis/build/ so travis.debian.net script can copy it out of
## the docker container.
## XXX: using find in for might not be safe but good enough for CI.
for file_name in $(find /var/lib/hardened-kernel/hardened-vm-kernel -type f -maxdepth 1) ; do
   mv "$file_name" /home/travis/build/
done

## Debugging.
ls "/home/travis/build/" || true

lintian_suppress_tags=""
lintian_suppress_tags+="missing-tests-control,"
lintian_suppress_tags+="changed-by-address-is-root-user,"
lintian_suppress_tags+="changed-by-address-malformed,"
lintian_suppress_tags+="debian-control-has-obsolete-dbg-package,"
lintian_suppress_tags+="license-problem-gfdl-invariants-empty,"
lintian_suppress_tags+="source-contains-prebuilt-binary,"
lintian_suppress_tags+="package-does-not-install-examples,"
lintian_suppress_tags+="license-problem-gfdl-invariants,"
lintian_suppress_tags+="package-does-not-use-debhelper-or-cdbs,"
lintian_suppress_tags+="missing-debian-source-format,"
lintian_suppress_tags+="dbg-package-missing-depends,"
lintian_suppress_tags+="direct-changes-in-diff-but-no-patch-system,"
lintian_suppress_tags+="debian-rules-missing-required-target,"
lintian_suppress_tags+="debian-rules-missing-recommended-target,"
lintian_suppress_tags+="debian-rules-missing-recommended-target,"
lintian_suppress_tags+="no-dep5-copyright,"
lintian_suppress_tags+="no-standards-version-field,"
lintian_suppress_tags+="debian-watch-file-is-missing,"
lintian_suppress_tags+="spelling-error-in-binary,"
lintian_suppress_tags+="new-package-should-close-itp-bug,"
lintian_suppress_tags+="extended-description-is-probably-too-short,"
lintian_suppress_tags+="maintainer-address-is-root-user,"
lintian_suppress_tags+="maintainer-address-malformed,"
lintian_suppress_tags+="hardening-no-fortify-functions,"
lintian_suppress_tags+="hardening-no-bindnow,"
lintian_suppress_tags+="unstripped-binary-or-object,"
lintian_suppress_tags+="debian-changelog-file-contains-invalid-email-address,"
lintian_suppress_tags+="package-contains-vcs-control-file,"
lintian_suppress_tags+="package-contains-documentation-outside-usr-share-doc,"

## Try lintian newly built kernel.
lintian \
   --suppress-tags "$lintian_suppress_tags" \
   --quiet \
   --pedantic \
   --info \
   --display-info \
   "/home/travis/build/"*".changes" || true

## Try install newly built kernel.
dpkg -i "/home/travis/build/"*".deb"

true "$0 INFO: OK. End."
