#!/bin/sh -e

. /usr/share/debconf/confmodule

log() {
    logger -t add_local_apt "info: $*"
}

# We need to check for a local repository if we are using http as source
db_get mirror/protocol || true
PROTOCOL="$RET"

if [ "$PROTOCOL" != "http" ] ; then 
  log "Not using http as protocol, bailing out"
  exit 0
fi

# Fetch the mirror 
db_get mirror/http/hostname || true
MIRROR=$RET

# mirror/codename is also set by cdrom methods
db_get mirror/codename || true
CODENAME="$RET"

# Fetch Directory used as base for the source
db_get mirror/http/directory || true
DIRECTORY="$RET"

# We need to check for a local repository if we are using http as source
if wget -qO - http://$MIRROR$RET/dists/$CODENAME/Release | grep -q " local/binary" ; then 
    log "OK, Found local/binary, adding local to sources.list"
    echo "deb http://$MIRROR$RET $CODENAME main local" > /target/etc/apt/sources.list
    log "running apt-get update"
    chroot /target apt-get update
else
    log "Hmm, did not find any local/binary"
fi
