#! /bin/bash
#
# © 2019 Niels Thykier <niels@thykier.net>
# 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

setup_debian_like_archive
import-fixture-signing-key

# Verify that our dinstall works with an empty archive
dinstall


(
  packages=$(fixture-package-dir)

  cp ${packages:?}/nonfree-package_* ${packages:?}/package_0.1* ${DAKBASE}/tmp

  dak process-upload -d ${DAKBASE}/tmp --automatic
)

echo "Verifying that packages in NEW are *not* published"
if ls -l ${DAKBASE}/ftp-master/pool/main/p/package/package_*.dsc 2>/dev/null ; then
    # We never acepted it, so it should not be published!
    echo "package has been publish even though it is still in NEW!?" >&2
    dak ls package
    exit 1
fi

echo "OK; accepting the package and running dinstall to see it become published"

# Accept the package from NEW
(

  dak control-overrides -s unstable -t deb -a << EOF
package required admin
EOF
  dak control-overrides -s unstable -t dsc -a << EOF
package admin
EOF
  
  dak process-new --automatic
  dak process-policy new
)

# After running dinstall, the package should now be present in the pool
dinstall
ls -l ${DAKBASE}/ftp-master/pool/main/p/package/package_*.dsc
echo "Published successfully"
