#!/bin/sh
#
# Update resolv.conf in LTSP chroots when the host resolv.conf is
# changed, to make sure apt keep working in the chroot.

# Make sure the logger binary is used if available
PATH="/bin:/usr/bin:/sbin:/usr/sbin"
export PATH

sourcefile=/etc/resolv.conf

if [ ! -s $sourcefile ] ; then
    # No use copying an empty file
    exit 0
fi

for f in /opt/ltsp/*/etc/resolv.conf ; do
    if [ -f "$f" ] && ! cmp -s $f $sourcefile ; then
	arch=$(echo $f | cut -d/ -f4)
	msg="Updating LTSP DNS config for $arch (cp $sourcefile $f)"
	# Ignore errors in case /usr/bin with logger is not yet
	# mounted
	logger -t debian-edu-config "$msg" 2>/dev/null || true
	echo "$msg"
	cp $sourcefile $f
	chmod 644 $f

        # Fix proxy settings too, as they depend on DNS resolution.
	msg="Updating LTSP proxy setup for $arch"
	logger -t debian-edu-config "$msg" 2>/dev/null || true
	echo "$msg"
	for f in /etc/environment /etc/apt/apt.conf ; do
	    newf=/opt/ltsp/$arch$f
	    cp $f $newf
	    chmod 644 $newf
	done
    fi
done
