#!/bin/sh
#
# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

max_tries=10
force_opt=""
while getopts "fm:" opt; do
    case $opt in
        f)
            force_opt="-f" ;;
        m)
            max_tries="$OPTARG" ;;
	*)
            echo "Usage: $0 [-f] [-m <int>]" ;
	    exit 1 ;;
    esac
done

onefs_mount=`mount -t efs`

# If /ifs is unmounted, exit
if [ "${onefs_mount}" = "" ]; then
    echo "/ifs already unmounted"
    exit 0
fi

tries="0"
result="1"

while [ $tries -lt $max_tries ]; do
    /usr/sbin/isi_kill_busy
    result=$?

    if [ $result = "0" ]; then
        # If this is our last try, pass $force_opt to umount.
        if [ $tries = `/bin/expr $max_tries - 1` ]; then
            /sbin/umount $force_opt -a -t efs
            result=$?
        else
       	    /sbin/umount -a -t efs
            result=$?
	fi

	# If this worked, exit
	echo "result: $result"
        if [ $result = "0" ]; then
            exit 0
        fi
    fi

    tries=`/bin/expr $tries + 1`
done

# Failed to unmount /ifs in $max_tries times
echo "Error: failed to unmount /ifs"
exit 1
