#!/bin/sh

# Remove running processes from the runtime environment
# and unmount the mounted directory structure

# This is $Revision: 1.4 $
#
# $Log: lsb_stop_dev_env,v $
# Revision 1.4  2001/11/14 06:34:02  cyeoh
# Remove uneeded echo
#
# Revision 1.3  2001/11/12 06:40:58  cyeoh
# Keep trying to unmount as long as at least one mount point is removed
# each time
#
#

# Get configuration
. /etc/lsbdev/lsbdev.conf

# Stop LSB build environment
if [ `id -u` -ne 0 ]; then
  echo You must run this script as root
  echo Aborting.
  exit 1
fi

# Attempt to stop processes
# Check to see if sshd was running
if [ -f $ROOT/var/run/sshd.pid ]; then
  kill `cat $ROOT/var/run/sshd.pid`
fi

# Remove existing mounts
last_num_mounts=0
num_mounts=1
mount_dirs=`cut -d' ' -f2 /proc/mounts  | grep $ROOT | sort -r`

while [ $num_mounts -ne $last_num_mounts ]; do
    last_num_mounts=`cut -d' ' -f2 /proc/mounts  | grep $ROOT | sort -r | wc -l`
    if [ "$mount_dirs" ]; then
        umount $mount_dirs > /dev/null 2>&1
    fi
    num_mounts=`cut -d' ' -f2 /proc/mounts  | grep $ROOT | wc -l`
done

if [ $num_mounts -ne 0 ]; then
    echo "Failed to remove some mounts"
    echo "Remaining mounts are:"
    mount_dirs=`cut -d' ' -f2 /proc/mounts  | grep $ROOT | sort -r`
    echo "$mount_dirs"
    echo "It is recommended to cleanup the remaining mount points before"
    echo "attempting to restart the build environment"
    echo 
    echo "A common cause of unmount failures are remaining processes"
    echo "or logins in the chroot environment."
    echo
    exit 1
fi


# Remove lock file
rm -f $RUNFILE
