#!/bin/sh

set -e

if [ -f "$CHROOT_SCRIPT_CONFIG" ]; then
    . "$CHROOT_SCRIPT_CONFIG"
fi

if [ -n "${CHROOT_UNION_TYPE}" ] && [ "${CHROOT_UNION_TYPE}" != 'none' ]; then

    if [ $1 = "setup-start" ]; then
	mount -t tmpfs debathena ${CHROOT_UNION_OVERLAY_DIRECTORY} -o size=${TMPFS_SIZE:-10737418240}
    elif [ $1 = "setup-stop" ]; then
        if [ "$CHROOT_SESSION_PURGE" = "true" ]; then
	    if [ "$AUTH_VERBOSITY" = "verbose" ]; then
		echo "Unmounting $CHROOT_UNION_OVERLAY_DIRECTORY"
	    fi
	    if [ -d "${CHROOT_UNION_OVERLAY_DIRECTORY}" ]; then
		umount "${CHROOT_UNION_OVERLAY_DIRECTORY}"
	    fi
	fi
    fi
fi
