#!/bin/sh
# Copyright © 2008-2009  Jan-Marek Glogowski <glogow@fbihome.de>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
. "$SETUP_DATA_DIR/common-config"

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

    if [ $STAGE = "setup-start" ]; then
        mkdir "${CHROOT_UNION_OVERLAY_DIRECTORY}"
        if [ ! -d "$CHROOT_UNION_OVERLAY_DIRECTORY" ]; then
            fatal "$CHROOT_UNION_OVERLAY_DIRECTORY does not exist, and could not be created"
        fi

        case "${CHROOT_UNION_TYPE_MODE}" in
            overlayfs-workdir)
                mkdir "${CHROOT_UNION_OVERLAY_DIRECTORY}_work"
                if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}_work" ]; then
                    fatal "${CHROOT_UNION_OVERLAY_DIRECTORY}_work does not exist, and could not be created"
                fi
                ;;
            overlay)
                mkdir "${CHROOT_UNION_OVERLAY_DIRECTORY}/upper"
                if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}/upper" ]; then
                    fatal "${CHROOT_UNION_OVERLAY_DIRECTORY}/upper does not exist, and could not be created"
                fi
                mkdir "${CHROOT_UNION_OVERLAY_DIRECTORY}/work"
                if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}/work" ]; then
                    fatal "${CHROOT_UNION_OVERLAY_DIRECTORY}/work does not exist, and could not be created"
                fi
        esac

        mkdir "${CHROOT_UNION_UNDERLAY_DIRECTORY}"
        if [ ! -d "$CHROOT_UNION_UNDERLAY_DIRECTORY" ]; then
            fatal "$CHROOT_UNION_UNDERLAY_DIRECTORY does not exist, and could not be created"
        fi

    elif [ $STAGE = "setup-recover" ]; then
        if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}" ]; then
            fatal "Missing overlay directory for session: can't recover"
        fi

        case "${CHROOT_UNION_TYPE_MODE}" in
            overlayfs-workdir)
                # Note that for an existing chroot created on older version
                # this may not exist, no data is carried in this directory so
                # we can simply create it here.
                if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}_work" ]; then
                    mkdir "${CHROOT_UNION_OVERLAY_DIRECTORY}_work"
                    if [ ! -d "${CHROOT_UNION_OVERLAY_DIRECTORY}_work" ]; then
                        fatal "${CHROOT_UNION_OVERLAY_DIRECTORY}_work does not exist, and could not be created"
                    fi
                fi
                ;;
        esac

        if [ ! -d "${CHROOT_UNION_UNDERLAY_DIRECTORY}" ]; then
            fatal "Missing underlay directory for session: can't recover"
        fi

    elif [ $STAGE = "setup-stop" ]; then
        if [ "$CHROOT_SESSION_PURGE" = "true" ]; then
            info "Purging $CHROOT_UNION_OVERLAY_DIRECTORY"
            if [ -d "${CHROOT_UNION_OVERLAY_DIRECTORY}" ]; then
                rm -rf "${CHROOT_UNION_OVERLAY_DIRECTORY}"
            fi
            case "${CHROOT_UNION_TYPE_MODE}" in
                overlayfs-workdir)
                    info "Purging ${CHROOT_UNION_OVERLAY_DIRECTORY}_work"
                    rm -rf "${CHROOT_UNION_OVERLAY_DIRECTORY}_work"
                    ;;
            esac

            # For safety, use rmdir rather than rm -rf in case
            # umount failed.
            info "Removing $CHROOT_UNION_UNDERLAY_DIRECTORY"
            if [ -d "${CHROOT_UNION_UNDERLAY_DIRECTORY}" ]; then
                if ! rmdir "${CHROOT_UNION_UNDERLAY_DIRECTORY}"; then
                    warn "Please unmount any filesystems and remove any files under $CHROOT_UNION_UNDERLAY_DIRECTORY"
                    fatal "Failed to remove $CHROOT_UNION_UNDERLAY_DIRECTORY (directory not empty)."
                fi
            fi
        fi
    fi

fi

