#!/bin/sh

if [ "$#" -lt 2 ]; then
	echo "Usage: $0 dist apt-command"
	exit 1
fi

if [ $1 == 'stable' ]; then
	aptdir='state'
else
	aptdir='lib'
fi

absroot=/usr/local/chroot/$1
shift
if [ ! -d $absroot ]; then
	echo "$absroot: no such directory"
	exit 1
fi

exec sudo apt-get \
	-o Dir::State=$absroot/var/$aptdir/apt \
	-o Dir::State::status=$absroot/var/lib/dpkg/status \
	-o Dir::Cache=$absroot/var/cache/apt \
	-o Dir::Etc::sourcelist=$absroot/etc/apt/sources.list \
	-o DPkg::Options::=--root=$absroot \
	-o DPkg::Run-Directory=$absroot \
	"$@"
