#!/bin/bash

SOURCESLIST="/etc/apt/sources.list"
VARDIR="/var/lib/apt"
LISTSDIR="$VARDIR/lists"
CACHEDIR="/var/cache/apt"
ARCHIVESDIR="$CACHEDIR/archives"
IGNORE_ARCHIVES=""
IGNORE_FRQCHG=""
APT_VERS=""

if [ -x "$UPAC_confd/31_local_apt_settings" ]; then
  # pull in configuration
  . "$UPAC_confd/31_local_apt_settings"
fi

if [ -z "${APT_VERS:-}" ]; then
  APT_VERS="$(dpkg --status apt | sed -n '/^Version:[[:space:]]/{s/[^[:space:]]\+[[:space:]]\+\([^[:space:]]\+\).*/\1/p;q};')"
fi

cat $SOURCESLIST | while read deb uri dist comp; do
  PROTOCOL="$(echo $uri | sed 's|\([^:]\+\).*|\1|')"
  if [ "$PROTOCOL" = "http" ] || [ "$PROTOCOL" = "ftp" ]; then
    HOST="$(echo $uri | sed 's|.*//\([^/[:space:]]\+\).*|\1|')"
    HOSTPATH="$(echo $uri | sed 's|.*//[^/[:space:]]\+/\?||;s|/$||;s|/|_|g;s|^\(.\+\)$|_\1|')"
    dist="${dist//\//_}"
    if [ -n "$DEBUG" ]; then
      echo "uri $uri"
      echo "HOST $HOST"
      echo "HOSTPATH $HOSTPATH"
    fi
    if [ "$deb" = "deb" ]; then
      for c in $comp; do
        echo "$LISTSDIR/${HOST}${HOSTPATH}_dists_${dist}_${c}_binary-@@{ARCH}_Packages(\.IndexDiff)?$ VarFile"
	# this is a compatibility hack for sarge
	if dpkg --compare-versions $APT_VERS gt 0.5.28.6; then
	  # > sarge
          echo "$LISTSDIR/${HOST}${HOSTPATH}_dists_${dist}_Release(\.gpg)?$ VarFile"
	else
	  # sarge
          echo "$LISTSDIR/${HOST}${HOSTPATH}_dists_${dist}_${c}_binary-@@{ARCH}_Release(\.gpg)?$ VarFile"
	  echo "$ARCHIVESDIR/lock$ VarDir"
	fi
      done
    elif [ "$deb" = "deb-src" ]; then
      for c in $comp; do
        echo "$LISTSDIR/${HOST}${HOSTPATH}_dists_${dist}_${c}_source_(Sources|Release)$ VarFile"
      done
    fi
  else
    : # other protocols are not supported. If you feel like they should
    : # please give a good reason and probably a patch.
  fi
  echo -e "\n\n"
done

echo "${LISTSDIR}(/partial)?$ VarDir"
echo "${LISTSDIR}/lock$ VarFile"

if [ "$IGNORE_ARCHIVES" = "yes" ]; then
  echo "!$ARCHIVESDIR/[-a-zA-Z0-9%\._+]+_(@@{ARCH}|all)\.deb$ VarFile"
fi

if [ "$IGNORE_FRQCHG" = "yes" ]; then
  echo "$ARCHIVESDIR(/partial|/lock)?$ VarDir"
  echo "$CACHEDIR/(src)?pkgcache\.bin$ VarFile"
  echo "$CACHEDIR$ VarDir"
fi
