#!/bin/bash

set -e
set -u

# make sure we have the sbin directories in our PATH to find piuparts ootb
PATH="${PATH}:/sbin:/usr/local/sbin:/usr/sbin"

if [ "$#" -lt 1 ] ; then
  echo "Usage: $0 <debian_package(s).deb>" >&2
  exit 1
fi

if [ -z "${distribution:-}" ] ; then
  # default to the currently running distribution to avoid hardcoding
  # a distribution which might not be supported by the running system
  distribution=$(lsb_release --short --codename 2>/dev/null)
  [ -n "${distribution}" ] || distribution="sid"  # fallback to "sid" iff lsb_release fails
  echo "*** distribution variable is unset, using distribution $distribution ***"
fi

if [ -z "${architecture:-}" ] ; then
  architecture=$(dpkg --print-architecture)
  echo "*** architecture variable is unset, using system architecture [$architecture] ***"
fi

if [ -n "${SCRIPTSDIR:-}" ] ; then
  echo "*** SCRIPTSDIR variable is set to $SCRIPTSDIR - using for piuparts run ***"
  scriptsdir="$SCRIPTSDIR"
else
  scriptsdir_path="/etc/piuparts/scripts/"
  if [ -d "${scriptsdir_path}" ]; then
    echo "*** SCRIPTSDIR variable is NOT set - using default [/etc/piuparts/scripts/] for piuparts run ***"
    scriptsdir="${scriptsdir_path}"
  else
    # Some old piuparts do not ship a scripts dir
    scriptsdir=""
  fi
fi

if [ -n "${PIUPARTS_TMPDIR:-}" ] ; then
  echo "*** PIUPARTS_TMPDIR is set to $PIUPARTS_TMPDIR - using for piuparts run ***"
  piuparts_tmpdir="$PIUPARTS_TMPDIR"
fi

create_base_tgz() {
  [ -r /var/cache/pbuilder/base-${distribution}-${architecture}.tgz ] && return 0

  if [ -n "${MIRROR:-}" ] ; then
    echo "*** MIRROR variable is set [$MIRROR], using it ***"
  else
    if lsb_release --id 2>/dev/null | grep -q Ubuntu ; then
      MIRROR='http://archive.ubuntu.com/ubuntu'
    else
      MIRROR='http://http.debian.net/debian'
    fi
    echo "*** Using mirror $MIRROR ***"
  fi

  if [ -n "${COMPONENTS:-}" ] ; then
    echo "*** COMPONENTS is set [$COMPONENTS], using for debootstrapping ***"
    components="$COMPONENTS"
  elif lsb_release --id 2>/dev/null | grep -q Ubuntu ; then
    components='main,universe,restricted,multiverse'
    echo "*** Ubuntu detected, using components $components ***"
  else
    components='main,contrib,non-free'
    echo "*** COMPONENTS is unset and looks like Debian - therefore using components $components ***"
  fi
  unset "COMPONENTS" # ensure it's unset so debootstrap doesn't fail, see GH issue 145

  tmpdir=$(mktemp -d)

  if [ -n "${DEBOOTSTRAP_OPTIONS:-}" ] ; then
    echo "*** DEBOOTSTRAP_OPTIONS is set [$DEBOOTSTRAP_OPTIONS], using for debootstrapping ***"
  fi

  debootstrap --variant=minbase \
              --components="$components" \
              --arch="$architecture" \
              ${DEBOOTSTRAP_OPTIONS:-} \
              "$distribution" \
              "$tmpdir" \
              "$MIRROR"

  (
    cd "$tmpdir"
    tar acf /var/cache/pbuilder/base-${distribution}-${architecture}.tgz *
    rm -rf ./*
  )
}

if [ -n "${SKIP_BASE_TGZ:-}" ] ; then
  echo "*** SKIP_BASE_TGZ is set, not using building/using /var/cache/pbuilder/base-${distribution}-${architecture}.tgz ***"
else
  create_base_tgz
  basetgz="--basetgz=/var/cache/pbuilder/base-${distribution}-${architecture}.tgz"
fi

# option not available with piuparts 0.38
if piuparts --help 2>/dev/null | grep -q -- '--warn-on-leftovers-after-purge' ; then
  warn_on_leftovers_after_purge_option="--warn-on-leftovers-after-purge"
fi

# logrotate test is kind of useless until http://bugs.debian.org/582630 is solved
if piuparts --help 2>/dev/null | grep -q -- '--skip-logrotatefiles-test' ; then
  skip_logrotatefiles_test_option="--skip-logrotatefiles-test"
fi

piuparts_version=$(piuparts --version 2>/dev/null | awk '{print $2}')
if dpkg --compare-versions $piuparts_version lt 0.43 ; then
  echo "*************************************************************************"
  echo "*** WARNING - piuparts version $piuparts_version is not recent enough ***"
  echo "*** You might run into problems, please consider upgrading piuparts   ***"
  echo "*************************************************************************"
fi

piuparts \
  -d "${distribution}" \
  ${warn_on_leftovers_after_purge_option:-} \
  ${skip_logrotatefiles_test_option:-} \
  --log-file='piuparts.txt' \
  ${scriptsdir:+--scriptsdir "$scriptsdir"} \
  ${piuparts_tmpdir:+--tmpdir="$piuparts_tmpdir"} \
  ${basetgz:-} \
  "$@"
