#! /bin/sh -e

# remove the debianfreespace logical volume.  Doing it before installing packages, to 
# make it possible to resize volumes on the fly during installation.
if [ -e /dev/vg_system/debianedufreespace ] ; then
    if mount | grep -q /debianedufreespace ; then
        umount /target/debianedufreespace
    fi
    lvremove -f /dev/vg_system/debianedufreespace
    grep -v /debianedufreespace /target/etc/fstab > /target/etc/fstab.new && \
      mv /target/etc/fstab.new /target/etc/fstab

    rmdir /target/debianedufreespace
fi

# Expand overfull file systems during installation, until /target is umounted.
(
    # Workaround for in-target not accepting arguments as part of the command
    # to run.  Need to create a temporary script that can be executed without
    # arguments.  Can not create it in /target/tmp/, as it does not exist yet.
    script=/debian-edu-extend-file-systems
    if [ ! -f $script ] ; then  # Avoid several loops running at the same time
        cat > /target$script <<EOF
#!/bin/sh
/usr/sbin/debian-edu-fsautoresize -n
EOF
        chmod a+rx /target$script
        while [ -x /target$script ]; do
            if [ -x /target/usr/sbin/debian-edu-fsautoresize ] && [ -x /target/usr/bin/perl ]; then
                # Avoid in-target, as it can not run in parallel with apt-install or in-target
                chroot /target $script || true
            fi
            sleep 10
        done
        # $script is removed in finish-install.d script
    fi
) < /dev/null > /dev/null 2>&1 &
