#! /bin/bash
#
# © 2020 Ivo De Decker <ivodd@debian.org>
# License: GPL-2+
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set -e
set -u

. ${DAK_ROOT:?}/integration-tests/common
. ${DAK_ROOT:?}/integration-tests/setup
. ${DAK_ROOT:?}/integration-tests/dinstall

HTMLDIR="${DAKBASE}/html"
mkdir $HTMLDIR
MAILDIR="${DAKBASE}/mail"
mkdir $MAILDIR

cat >> ${DAKBASE}/etc/dak.conf << EOF
Show-New::HTMLPath "${HTMLDIR}";
Dir::Mail "${MAILDIR}";
Dinstall::SendmailCommand "${DAK_ROOT}/integration-tests/fake-mailer";
Dinstall::BugServer "bugs.example.org";
EOF

sed -i 's/No-Mail "true";/#No-Mail "true";/' $DAKBASE/etc/dak.conf

show_subjects() {
	grep -h ^Subject ${MAILDIR}/sendmail.* | LC_ALL=C sort
}

setup_empty_archive
setup_unstable
import-fixture-signing-key

dinstall

(
  packages=$(fixture-package-dir)

  upload_changes ${packages:?}/snowman_0.1-1_amd64.changes
  upload_changes ${packages:?}/grave_0.1-1_amd64.changes
  process_uploads

  # the queue-report contains references to time (age of the uploads), so we
  # can't check the output against a reference
  dak queue-report

  dak show-new
  for htmlfile in $HTMLDIR/snowman_0.1-1.html $HTMLDIR/grave_0.1-1.html
  do
    links -dump $htmlfile
    save_ci_artifact $htmlfile
  done

  printf "c\na\n" | dak process-new snowman_0.1-1_amd64.changes | cat
  printf "c\na\n" | dak process-new grave_0.1-1_amd64.changes | cat

  dak process-new --automatic
  dak process-policy new

  dak show-new
)

(
  dak contents scan-binary
  dak contents scan-source
)

dinstall

check_all_suites after_install

check_output maintainers.txt  "dak make-maintainers -a ftp-master -p"

echo y |dak rm -R -C test@example.com -d 123456 -m "remove ☃" snowman
echo y |dak rm -R -C test@example.com -d 123457 -m "remove è" grave

check_all_suites after_remove

check_output changelog_buildd  "dak make-changelog -b unstable -s buildd-unstable"
check_output mail_subjects  "show_subjects"

