#!/bin/sh
set -eu

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

$@
EOF
}

# skip when running tests
if [ -n "${DEBCI_RUNNING_TESTS:-}" ]; then exit; fi

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

root="$debci_data_basedir/chdist"
name="${debci_suite}-${debci_arch}"

# create new chdist if it doesn't exist already
if [ ! -e "$root/$name" ]; then
  log "I: Creating new chdist $root/$name"
  call_chdist create >/dev/null
fi

# figure out default mirror from debootstrap scripts
DEF_MIRROR="${debci_mirror}"
SUITE=$debci_suite
TARGET="$root/$name"
ARCH=$debci_arch
set +u
export DEBOOTSTRAP_DIR=/usr/share/debootstrap
. /usr/share/debootstrap/functions
exec 4>&1
# this updates $DEF_MIRROR (Ubuntu, ports, ..)
. /usr/share/debootstrap/scripts/$debci_suite
set -u

# enable all components
if [ "${DEF_MIRROR%ubuntu*}" = "$DEF_MIRROR" ]; then
  COMPONENTS="main contrib non-free"  # Debian
else
  COMPONENTS="main restricted universe multiverse"  # Ubuntu
fi

mirror=${debci_mirror:-$DEF_MIRROR}

# write apt sources.list
mkdir -p $TARGET/etc/apt/

case "$mirror" in
  (*debian*)
    debci-generate-apt-sources \
      --mirror="$mirror" \
      --components="$COMPONENTS" \
      --source \
      --buildd \
      --dbgsym \
      --single-arch \
      "$SUITE" \
      > "$TARGET/etc/apt/sources.list"
    ;;
  (*)
    debci-generate-apt-sources \
      --mirror="$mirror" \
      --components="$COMPONENTS" \
      --source \
      --single-arch \
      "$SUITE" \
      > "$TARGET/etc/apt/sources.list"
    ;;
esac

# use a local proxy if available
http_proxy="${http_proxy:-}"
if [ -z "$http_proxy" ]; then
  # detect a local apt-cacher-ng cache running.  10.0.2.2 = default IP
  # assigned to host system as seen from a kvm/virtualbox virtual machine
  for ip in 127.0.0.1 10.0.2.2; do
    if nc -z -w 1 $ip 3142; then
      export http_proxy=http://$ip:3142
    fi
  done
fi
if [ -n "$http_proxy" ]; then
  echo "Acquire::http::Proxy \"$http_proxy\";" > "$TARGET/etc/apt/apt.conf.d/01proxy"
fi

# disable multi-arch
echo "Apt::Architectures {\"$ARCH\";};" > "$TARGET/etc/apt/apt.conf.d/97_no_multiarch"

# disable unnecessary srcpkgcache
echo 'Dir::Cache::srcpkgcache "";' > "$TARGET/etc/apt/apt.conf.d/98disable_cache"

# do not download translations
echo 'Acquire::Languages "none";' > "$TARGET/etc/apt/apt.conf.d/99translations"

update_chdist() {
  call_chdist apt-get update

  base_system="$root/$name/base-system.txt"
  # dpgk-dev is added to all clean test beds by debci itself
  base_packages="dpkg-dev $(call_chdist grep-dctrl-packages -n -s Package -F Priority required --or important)"
  chdist \
    --data-dir "$root" \
    apt-get "$name" --simulate --quiet --no-install-recommends install $base_packages \
    | awk '{ if ($1 == "Inst") {print($2)}}' | sort > "$base_system"

  awk '{print("/^"$1"\\s/d")}' $base_system > "${root}/${name}/exclude-base-system.sed"
}

if [ "$debci_quiet" = 'true' ]; then
  update_chdist >/dev/null 2>&1
else
  update_chdist
fi
