#!/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}"

# skip if already updated in the last hour
timestamp="$root/$name/.timestamp"
if [ -f "$timestamp" ]; then
  ts=$(stat --format=%Y "$timestamp")
  now=$(date +%s)
  if [ $(($now - $ts)) -le 3600 ]; then
    log "I: chdist updated in the last hour, no need to update"
    exit
  fi
fi

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

  # figure out default mirror from debootstrap scripts
  DEF_MIRROR="http://http.debian.net/debian"
  SUITE=$debci_suite
  TARGET="$root/$name"
  ARCH=$debci_arch
  set +u
  . /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"  # Debian
  else
    COMPONENTS="main restricted universe multiverse"  # Ubuntu
  fi

  mirror=${debci_mirror:-$DEF_MIRROR}

  # write apt sources.list
  mkdir -p $TARGET/etc/apt/
  echo "deb [arch=${debci_arch}] $mirror $SUITE $COMPONENTS
deb-src $mirror $SUITE $COMPONENTS" > "$TARGET/etc/apt/sources.list"

  # add buildd sources
  #FIXME duplicates logic in backends/schroot/create-testbed
  if grep -q debian "$TARGET/etc/apt/sources.list"; then
    if [ "$SUITE" = unstable ]; then
      buildd_suite=buildd-$SUITE
    else
      buildd_suite=buildd-$SUITE-proposed-updates
    fi
    cat >> "$TARGET/etc/apt/sources.list" <<EOF
deb [arch=${debci_arch}] http://incoming.debian.org/debian-buildd $buildd_suite main
deb-src http://incoming.debian.org/debian-buildd $buildd_suite main
EOF
  fi

  # 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"
fi

update_chdist() {
  chdist --data-dir "$root" apt-get "$name" update

  base_system="$root/$name/base-system.txt"
  # dpgk-dev is added to all clean test beds by debci itself
  base_packages="dpkg-dev $(chdist --data-dir "$root" grep-dctrl-packages "$name" -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

# record timestamp
touch "$timestamp"
