#!/bin/sh

set -eu

usage() {
  cat <<EOF
usage: $0 [OPTIONS] PACKAGE

EOF
  if [ -n "$*" ]; then
    echo "$@"
  fi
}

base=$(readlink -f $(dirname $(readlink -f $0))/..)
. $base/lib/environment.sh
. $base/lib/functions.sh

prepare_args

pkg="$1"
if [ -z "$pkg" ]; then
  usage
  exit 1
fi

binaries=$(list_binaries "$pkg")
APT_CHDIST="chdist --data-dir $debci_data_basedir/chdist apt-get $debci_suite-$debci_arch --simulate --quiet install"

{
  if ! $APT_CHDIST $binaries 2>&1; then
    for binary in $binaries; do
      # this can fail for binaries which don't exist on this architecture
      $APT_CHDIST $binary 2>&1 || true
    done
  fi
} | \
  awk '{ if ($1 == "Inst") { sub("^.", "", $3); print($2, $3) } }' | \
  sort -u
