#!/bin/bash

#########################################

CHRDIR=/chroot

#########################################

PARTIAL=/var/cache/apt/archives/partial

function update_head
{
    echo "-------------------------------------------------------------------------------"
    echo "*** Update $1..."
    echo "-------------------------------------------------------------------------------"
}

# gather from all the chroots
if [ -d $CHRDIR ]; then
    CHROOTS=`ls $CHRDIR`
fi

# do it in /
update_head "root system"
[ -d $PARTIAL ] || mkdir $PARTIAL
apt-get update
apt-get autoclean

for CHR in $CHROOTS; do

	if [ -d $CHRDIR/$CHR/etc/apt ]; then
		update_head "$CHR chroot"
		[ -d $CHRDIR/$CHR$PARTIAL ] || mkdir $CHRDIR/$CHR$PARTIAL

		if ! [ -f $CHRDIR/$CHR/etc/apt/noautoupgrade ]; then
			chroot $CHRDIR/$CHR apt-get update
		else
			echo "WARNING: Automatic update disabled in $CHR chroot !!!"
			echo "This can lead to a security risk."
		fi

		chroot $CHRDIR/$CHR apt-get autoclean
	fi

done

echo "Finished."
