#!/bin/sh -e

divert() {
  prog="$1"
  mv $prog $prog.real
  cat > $prog <<EOF
#!/bin/sh

echo "Fake $prog called, doing nothing"
EOF
  chmod 755 $prog
}

undivert() {
  prog="$1"
  mv $prog.real $prog
}

DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

if test -n "$debconf_preseed_file"; then
	mkdir -p $TARGET/var/cache/debconf || true
	for p in $WORKDIR $HOST ; do
		[ ! -f "$p/$debconf_preseed_file" ] || \
		( 
		  cp $p/$debconf_preseed_file $TARGET/var/cache/debconf/config.dat ;
		  echo "Preseeding Debconf from $debconf_preseed_file";
		)
	done
#	DEBCONF_DB_OVERRIDE="File{/tmp/config.dat}"
#	export DEBCONF_DB_OVERRIDE
fi

if test -n "$exclude"; then
    exclude=`echo $exclude | tr ' ' ,`
    opts="--exclude=$exclude"
fi

if test -n "$include"; then
    include=`echo $include | tr ' ' ,`
    opts="$opts --include=$include"
fi

if test -n "$basedebs"; then
    opts="$opts --unpack-tarball $basedebs"
fi

# DEBOOTSTRAP
echo "debootstrap $opts $dist $TARGET $mirror $script"
debootstrap $opts $dist $TARGET $mirror $script

# populate sources.list
if test -n "$sources"; then
	echo "$sources" > $TARGET/etc/apt/sources.list
else
	echo "deb $mirror $dist main" > $TARGET/etc/apt/sources.list
fi

# populate /etc/apt/preferences
if test -n "$preferences"; then
	echo "$preferences" | sed -e 's/^\.$//' >> $TARGET/etc/apt/preferences
fi

# populate /etc/apt/apt.conf
if test -n "$apt_conf"; then
	echo "$apt_conf" >> $TARGET/etc/apt/apt.conf
fi

rm -f $TARGET/var/lib/apt/lists/debootstrap.*

if test -z "$keepdebs"; then
    rm -f $TARGET/var/cache/apt/archives/*.deb
fi

echo $purge | tr ' ' '\n' | xargs --no-run-if-empty -t \
    chroot $TARGET dpkg --purge

echo $(hostname) > $TARGET/etc/hostname
if [ -f /etc/hosts ]; then
  install -m 644 /etc/hosts $TARGET/etc/hosts
fi

# disable hwclock
echo "# Disable hwclock at startup/shutdown" >> $TARGET/etc/default/rcS
echo "HWCLOCKACCESS=no" >> $TARGET/etc/default/rcS

if test -n "$install"; then
    apt_options="-y"
    if test "$apt_force_yes" = "true"; then
	apt_options="$apt_options --force-yes"
    fi
    divert $TARGET/sbin/start-stop-daemon
    chroot $TARGET mount -t proc proc /proc
    chroot $TARGET apt-get update
    chroot $TARGET apt-get $apt_options install $install
    chroot $TARGET apt-get clean
    echo "Killing any processes running on the target fs:"
    # kill any processes using the target fs
    fuser -k -v -m $TARGET || true
    chroot $TARGET umount /proc
    undivert $TARGET/sbin/start-stop-daemon
fi

if [ "$netconfig" != "no" ]; then
    . /tmp/rootstrap.netconf
    cat <<EOF >> $TARGET/etc/network/interfaces
auto lo
iface lo inet loopback

auto $interface
EOF
    if [ "$address" = "dhcp" ]; then
        cat <<EOF >> $TARGET/etc/network/interfaces
iface $interface inet dhcp
EOF
    else
        cat <<EOF >> $TARGET/etc/network/interfaces
iface $interface inet static
    address $address
    netmask $netmask
	up route add -net 0.0.0.0 dev $interface
EOF
    fi
	if [ -n "$gateway" ]; then
		echo "up route add -net 0.0.0.0 gw $gateway" >>	$TARGET/etc/network/interfaces
	fi
    if [ ! -e $TARGET/etc/resolv.conf ]; then
        if [ -n "$domain" ]; then
            cat <<EOF >> $TARGET/etc/resolv.conf
search $domain
EOF
        fi
        if [ -n "$nameserver" ]; then
            cat <<EOF >> $TARGET/etc/resolv.conf
nameserver $nameserver
EOF
        fi
    fi
fi
