--- /dev/null
+#!/bin/sh
+#
+# apt-in-chroot - runs apt in a specified chroot
+#
+# Copyright (C) 2010 Martin Zobel-Helas <zobel@debian.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+
+VALID_CHROOTS="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /usr/bin/tr ' ' '\n')"
+VALID_CHROOTS_PARSE="$(/usr/bin/dchroot -l 2>&1 | /usr/bin/awk -F": " '{print $2}' | /bin/sed -e 's/ /|/g')"
+
+usage() {
+ bn="`basename "$0"`"
+ echo "Usage: $bn <chroot> <apt sub-command> <package>"
+ echo ""
+ echo " allowed chroots are ($VALID_CHROOTS_PARSE)"
+ echo ""
+ echo " allowed apt sub-commands are:"
+ echo " + install"
+ echo " + remove"
+ echo " + purge"
+ echo " + build-dep"
+}
+
+
+if [ "$#" -lt "3" ]; then
+ usage >&2
+ exit 1
+fi
+
+CHROOT_DIR="/chroot"
+CHROOT=$1; shift
+APTCMD=$1; shift
+PACKAGES=$*
+
+
+case "$APTCMD" in
+install|remove|purge|build-dep)
+ # those are the allowed apt sub-commands
+ ;;
+*)
+ echo "$APTCMD is not a valid apt sub-command"
+ ;;
+esac
+
+grep -qFx $CHROOT <<< "$VALID_CHROOTS"
+
+RET=$?
+
+if [ "$RET" == "0" ]; then
+ # valid chroot
+ echo "Will run '/usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES'"
+ /usr/sbin/chroot $CHROOT_DIR/$CHROOT apt-get $APTCMD $PACKAGES
+else
+ echo "$CHROOT not in $VALID_CHROOTS_PARSE"
+ exit 1
+fi
+
+