#!/bin/sh
set -e

# only run this script if the system is going into suspend
if [ "$1" != "suspend" ]; then
	exit 0
fi

# Script to unmount any automounted filesystems when suspending
# Taken from /etc/init.d/autofs
# Ian Redfern (redferni@logica.com)

# The name and location of the daemon
DAEMON=/usr/sbin/automount

# Check if automount exists
test -f $DAEMON || exit 0

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Each automount daemon leaves its pid in one of these files
set -- /var/run/autofs/*.pid
if [ "$1" = "/var/run/autofs/*.pid" ]; then
	exit 0
fi
while [ $# -gt 0 ]; do
	pid=$(head -n 1 $1 2>/dev/null)
	if [ "$pid" ]; then
		# SIGUSR1 makes the daemon unmount any unused filesystems without quitting
		kill -USR1 $pid
	fi
	shift
done
