#!/bin/bash

# Original written from Jeroen van Wolffelaar <jeroen@debian.org>

set -e
set -u

export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
. $SCRIPTVARS

export PAGER=cat

exec > $webdir/d-i 2>&1

testing_id=$(psql -t -c "SELECT id FROM suite WHERE suite_name='testing'")
testing_pu_id=$(psql -t -c "SELECT id FROM suite WHERE suite_name='testing-proposed-updates'")
unstable_id=$(psql -t -c "SELECT id FROM suite WHERE suite_name='unstable'")

echo "udeb's in testing that don't (anymore) correspond to any testing source:"
psql -c "
  SELECT
    b.package,
    b.version,
    (SELECT arch_string from architecture where b.architecture = architecture.id) as arch,
    s.source
  FROM bin_associations ba
  LEFT JOIN binaries b on (ba.bin = b.id)
  LEFT JOIN source s on (b.source = s.id)
  WHERE ba.suite = ${testing_id}
    AND s.id NOT IN (SELECT source from src_associations WHERE suite = ${testing_id})
    AND b.type = 'udeb'
  ORDER BY s.source, b.package, b.architecture;
  "

echo "udeb's in unstable that should be in testing too:"
psql -c "
  SELECT
    b.package,
    b.version,
    (SELECT arch_string from architecture where b.architecture = architecture.id) as arch,
    s.source
  FROM bin_associations ba
  LEFT JOIN binaries b on (ba.bin=b.id)
  LEFT JOIN source s on (b.source=s.id)
  WHERE ba.suite = ${unstable_id}
    AND NOT EXISTS (SELECT 1 FROM bin_associations ba2 WHERE ba2.suite = ${testing_id} AND ba2.bin = ba.bin)
    AND s.id IN (SELECT source from src_associations WHERE suite = ${testing_id})
    AND b.type = 'udeb'
    AND b.architecture IN (SELECT architecture FROM suite_architectures WHERE suite = ${testing_id})
  ORDER BY s.source, b.package, b.architecture;
  "

echo "udeb's in t-p-u that should be in testing too:"
psql -c "
  SELECT
    b.package,
    b.version,
    (SELECT arch_string from architecture where b.architecture=architecture.id) as arch,
    s.source
  FROM bin_associations ba
  LEFT JOIN binaries b ON ba.bin = b.id
  LEFT JOIN source s ON b.source = s.id
  WHERE ba.suite = ${testing_pu_id}
    AND NOT EXISTS (SELECT 1 FROM bin_associations ba2 WHERE ba2.suite = ${testing_id} AND ba2.bin = ba.bin)
    AND s.id IN (SELECT source from src_associations WHERE suite = ${testing_id})
    AND b.type = 'udeb'
    AND b.architecture IN (SELECT architecture FROM suite_architectures WHERE suite = ${testing_id})
  ORDER BY s.source, b.package, b.architecture;
  "
