#!/bin/sh
#
# Pre-Post script for the pumount programm.
# It ensure that all ntfs device mounted with ntfs-3g
# are cleanly unmounted before exiting
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.


#find fisrt the name of the mount_point or the device
for var in $*; do
		[ -n `echo $var | egrep '/media/'`"" ] && volume=$var
		[ -n `echo $var | egrep '/dev/'`"" ] && volume=$var
done

#Check if the volume is mount with ntfs-3g.
#If yes, stock the PID of the ntfs-3g process
ntfs3g=no
if [ -n `ps -ef | egrep "mount\.ntfs-3g.*$volume" | awk '{ print $2 }'`"" ]; then
		ntfs3g=yes
		PID=`ps -ef | egrep "mount\.ntfs-3g.*$volume" | awk '{ print $2 }'`
fi

#Launch pumount.real
/usr/bin/pumount.real $*
status=$?
[ $status -ne 0 ] && exit $status

#If ntfs-3g, wait until the process $PID is gone 
if [ $ntfs3g = "yes" ]; then
		while [ -n `ps -e |grep $PID | awk '{ print $1 }'`"" ]; do wait; done
fi

exit 0
