#! /bin/bash

# Let's make sure that we have correct http proxy settings.
if [ -x $target/usr/share/debian-edu-config/tools/update-proxy-from-wpad ]; then 
	chroot $target /usr/share/debian-edu-config/tools/update-proxy-from-wpad
fi

# The proxy settings should not have been written to /etc/environment.
# Let's export those settings to the current shell, so that cf-agent
# has them...
for line in $(cat $target/etc/environment | grep -v -E "^#.*" | grep -E ".+=.*"); do
	export $line;
done

# Now let's assume, we have http_proxy, https_proxy and ftp_proxy set in our
# environment. We need to pass them on into the chroot...

echo http_proxy=$http_proxy
echo https_proxy=$https_proxy
echo ftp_proxy=$ftp_proxy

# d-e-c/cf.finalize expects this directory to exist for prep'ing desktop-profiles
# (FIXME: file d-e-c-bug)
mkdir -p $target/etc/xdg/menus

# d-e-c/cf.workarounds expects /etc/xdg/xfce4/panel/ to exist (FIXME: file d-e-c bug)
mkdir -p $target/etc/xdg/xfce4/panel/

# Do the conversion of the vanilla Debian system to a Debian Edu system
chroot $target /usr/bin/cf-agent -I -D installation

# fetch DebianEdu root CA certs
chroot $target /usr/share/debian-edu-config/tools/fetch-rootca-cert

sed -i $target/etc/wgetrc \
    -e "s@^http_proxy\s*=.*@http_proxy = $http_proxy@" \
    -e "s@^https_proxy\s*=.*@https_proxy = $https_proxy@" \
    -e "s@^ftp_proxy\s*=.*@ftp_proxy = $ftp_proxy@" \
    ${NULL}
