#!/bin/sh
# Copyright © 2012  Reinhard Tartler <siretart@debian.org>
#
# 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/>.
#
#####################################################################

# this script starts an additional autofs instance outside the chroot
# to manage autofs managed mount points inside the chroot

set -e

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

DAEMON=/usr/sbin/automount
PIDFILE="/var/run/autofs-${SESSION_ID}.pid"
NEW_MASTER="/var/run/autofs-${SESSION_ID}-master.conf"

start_stop_autofs() {
	if [ ! -r $NEW_MASTER ]; then
            return 1;
        fi
        start-stop-daemon "$@" --pidfile $PIDFILE --exec $DAEMON -- \
                --dont-check-daemon $OPTIONS --pid-file $PIDFILE $NEW_MASTER
}


# when recovering, make sure that autofs is stopped
if [ $STAGE = "setup-recover" ]; then
    if [ -x $DAEMON ] && [ -r /etc/default/autofs ]; then 
        info "Stopping automount"

        if ! start_stop_autofs --stop --retry 5 --oknodo --quiet ; then
            info "stopping automount failed"
        fi
        rm -f $NEW_MASTER $PIDFILE
    else           
        info "automount not installed"
    fi
fi
