#!/bin/bash
# vim: set shiftwidth=4 tabstop=8 noexpandtab:

# Copyright (C) 2026, Xavier Guimard <yadd@debian.org>
#
# 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 3 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.
#
# On Debian systems, the complete text of the GNU General Public License
# version 3 can be found in the /usr/share/common-licenses/GPL-3 file.

set -u

TESTTYPE="Download-compat"
. ./lib_test_uscan

COMMAND="uscan --no-conf"

# set safe defaults
DEBUGECHO=":"
# comment out for debug
#COMMAND="$COMMAND --debug"
#COMMAND="$COMMAND --verbose"
#DEBUGECHO=echo

. "${0%/*}/shunit2-helper-functions.sh"

tearDown(){
    killHttpServer
    echo
}

trap tearDown EXIT

# Run uscan in a minimal package whose 1.0-1 local version has 1.0, 1.2 and
# 3.0 available upstream. Without --download-compat-version the newest (3.0)
# is downloaded, with it only the semver-compatible version (1.2) is.
helperTestDlCompat() {
    local with_compat="$1"   # "compat" or ""
    local expect="$2"        # expected upstream version to be downloaded

    PKG=foo
    SHUNIT_TMPDIR="${SHUNIT_TMPDIR:-$(mktemp -d)}"
    TEMP_PKG_DIR=$(mktemp -d -p "$SHUNIT_TMPDIR")
    spawnHttpServer
    PORT=$(cat "$TEMP_PKG_DIR"/repo/port)

    mkdir -p "$TEMP_PKG_DIR"/$PKG/debian/source

    cat <<END > "$TEMP_PKG_DIR"/$PKG/debian/watch
version=3
http://localhost:$PORT/$PKG-(\d[\d.]*)\.tar\.gz
END

    cat <<END > "$TEMP_PKG_DIR"/$PKG/debian/changelog
$PKG (1.0-1) unstable; urgency=low

  * Initial release

 -- Joe Developer <jd@debian.org>  Mon, 02 Nov 2013 22:21:31 -0100
END

    echo -n '3.0 (quilt)' > "$TEMP_PKG_DIR"/$PKG/debian/source/format

    # upstream repository with 1.0, 1.2 and 3.0 tarballs
    for v in 1.0 1.2 3.0; do
        mkdir -p "$TEMP_PKG_DIR"/repo/$PKG-$v
        echo "content $v" > "$TEMP_PKG_DIR"/repo/$PKG-$v/content
        ( cd "$TEMP_PKG_DIR"/repo && tar -caf $PKG-$v.tar.gz $PKG-$v )
    done

    if [ "$with_compat" = "compat" ]; then
        OUTPUT=$( cd "$TEMP_PKG_DIR"/$PKG \
            && uscan --no-conf --download-compat-version )
    else
        OUTPUT=$( cd "$TEMP_PKG_DIR"/$PKG && uscan --no-conf )
    fi
    assertEquals "uscan: exit_code!=0 but exit_code=0" "$?" "0"

    TARBALL=${PKG}_${expect}.orig.tar.gz
    assertTrue "$TARBALL missing" "[ -f "$TEMP_PKG_DIR"/$TARBALL ]"
    assertTrue "uscan did not consider $expect version" \
               "$(echo "$OUTPUT" | grep -q "Newest version of $PKG on remote site is $expect" ; echo $?)"
}

# --download-compat-version: only the semver-compatible 1.2 is downloaded
testWatchDlCompat() {
    helperTestDlCompat compat 1.2
}

# control: without the option, the newest 3.0 is downloaded
testWatchDlCompatOff() {
    helperTestDlCompat "" 3.0
}

. shunit2
