#!/bin/sh
#
# fetch <package> [...]
#
# Fetch a list of binary packages and their source

set -e

DEBS="$@"

export APTDIR="$HOME/.ia32-libs-tools/apt"
mkdir -p $APTDIR

LIST=/usr/lib/ia32-libs-tools/sources.list
if [ -e /etc/ia32-libs-tools/sources.list ]; then
  LIST=/etc/ia32-libs-tools/sources.list
fi

APT_FLAGS="-o Dir::Etc::sourcelist=$LIST \
	-o Dir::State=$APTDIR/state \
	-o Debug::NoLocking=true \
	-o Dir::Cache=$APTDIR/cache \
	-o Acquire::Retries=3 \
	-o Apt::Architecture=i386 \
        -o Dir::State::Status=$APTDIR/state/status"

# Prepare APTDIR
mkdir -p $APTDIR/state/lists/partial
mkdir -p $APTDIR/cache/archives/partial
echo -n > $APTDIR/state/status

# FIXME: do authentication
# Probe apt version for --allow-unauthenticated
APT_VER=$(apt-get --version | head --lines 1 | cut -d" " -f2)
if dpkg --compare-versions "$APT_VER" ">=" 0.6; then
  # Sid apt needs authentication
  APT_AUTH="--allow-unauthenticated"
fi

APT_GET="apt-get $APT_AUTH $APT_FLAGS --assume-yes"
APTITUDE="aptitude $APT_FLAGS"

$APT_GET update

APT_CACHE="apt-cache $APT_FLAGS"

######################################################################
# Sources

# Fetch sources for all debs
echo "$DEBS" \
| xargs $APT_CACHE show \
| grep-dctrl "" -s Package,Version,Source \
| while read KEY VAL; do # ^ Get package information, needed fields and parse
    # Source: is optional and may not contain a version
    # Fill in missing information from Package and Version
    case "$KEY" in
      Package:) PKG="$VAL"; SRC="$VAL";;
      Version:) VER=$(echo "$VAL" | sed 's/+b[0-9]*$//');;
      Source:) case "$VAL" in
		 *\(*\)) SRC=$(echo "$VAL" | cut -d" " -f1)
			 VER=$(echo "$VAL" | sed 's/.*(\(.*\))/\1/' | sed 's/+b[0-9]*$//');;
		 "") ;;
		 *) SRC="$VAL";;
	       esac;;
      "") echo >&2 "Fetching source $SRC $VER for $PKG"
	  echo "$SRC=$VER";;
    esac
  done \
| sort -u | xargs $APT_GET -d --only-source source \
|| ( echo Fetching source failed; exit 1 ) # Fetch source

######################################################################
# Debs

# fetch prebuild debs
echo "$DEBS" \
| xargs $APTITUDE download || ( echo Fetching debs failed; exit 1 )

# clean up
# rm -rf $APTDIR

# Sanity check that we have all debs

for DEB in $DEBS; do
  if ! [ -f ${DEB}_*_i386.deb ]; then
    echo "${DEB}_*_i386.deb" missing
    exit 1
  fi
done

# Sanity check that we have _matching_ source for every deb.

dpkg-scanpackages pkgs /dev/null 2>/dev/null \
| grep-dctrl "" -s Package,Version,Source \
| while read KEY VAL; do
    case "$KEY" in
      Package:) PKG="$VAL"; SRC="$VAL";;
      Version:) VER="$(echo "$VAL" | sed 's/+b[0-9]*$//')";;
      Source:) case "$VAL" in
		 *\(*\)) SRC=$(echo "$VAL" | cut -d" " -f1)
			 VER=$(echo "$VAL" | sed 's/.*(\(.*\))/\1/' | sed 's/+b[0-9]*$//');;
		 "") ;;
		 *) SRC="$VAL";;
	       esac;;
      "") echo "Testing source $SRC $VER for $PKG"
	  FVER=$(echo "$VER" | cut -d: -f2)
	  if [ ! -e "local/srcs/${SRC}_${FVER}.dsc" ]; then
	    if [ ! -e "srcs/${SRC}_${FVER}.dsc" ]; then
	      echo "Missing source $SRC $VER for $PKG!"
	      exit 1
	    fi
	  fi;;
    esac
  done || exit 1

echo All sources and packages fetched, all versions match.
echo Enjoy.
exit 0
