#!/bin/sh
#
# File: umount
# $Id: umount,v 1.2 2010/03/24 21:28:30 keithmarshall Exp $
#
# =====================================================================
#
# Copyright (C) 2006, 2007, 2009 by Keith Marshall
#  mailto:keithmarshall@users.sourceforge.net
#
# This file is part of MSYS
#  http://www.mingw.org/msys.shtml
#
# 2009-04-06: First published implementation for MSYS-1.0.11
#
# MSYS is free software.  It is provided "as is", in the hope that it
# may be useful; there is NO WARRANTY OF ANY KIND, not even an implied
# warranty of MERCHANTABILITY or FITNESS FOR ANY PARTICULAR PURPOSE.
# At no time will the author accept liability for damages, however
# caused, resulting from the use of this software.
#
# Permission is granted to copy and redistribute this software, either
# as is, or in modified form, provided that:--
#
#   1) All such copies are distributed with the same rights
#      of redistribution.
#
#   2) The preceding disclaimer of warranty and liabality is
#      retained verbatim, in all copies.
#
#   3) Accreditation of the original author remains in place.
#
#   4) Modified copies are clearly identified as such, with
#      additional accreditation given to the authors of each
#      modified version.
#
# =====================================================================
#
# Exactly one argument is required...
#
  if test $# -eq 1
  then
#
#   Normally, it specifies the mount point to be released,
#   but it may also represent a mounted directory path name,
#   for which all bound mount points are to be filtered out
#   of the "mount table" file.
#
    MNTPATH=`echo "$1" | tr '\\\\' /`
    TMPFILE=${TMPDIR-"/tmp"}/mnttab$$.tmp
    MNTTAB=${MNTTAB-"/etc/fstab"}
#
    if cat "$MNTTAB" | tr '\\' / | awk '
#
#     Copy the "mount table" to a temporary file, filtering
#     out all active mount point records which match MNTPATH,
#     (the specified argument); set exit status to:--
#       0: if at least one mount point is matched;
#       1: if no match is found.
#
      BEGIN { status = 1 }
      { keep = $0 }
      /^#/ { print; keep = "no"; $0 = "!'$MNTPATH'" }
      $2 == "'$MNTPATH'" { keep = "no"; status = 0 }
      { $2 = "!" } $0 == "'$MNTPATH' !" { keep = "no"; status = 0 }
      keep != "no" { print keep }
      END { exit status }' > "$TMPFILE"
    then
#
#     At least one mount point was selected to release...
#     Replace the active "mount table" file with the regenerated
#     copy, so completing the operation.
#
      cp "$TMPFILE" "$MNTTAB"
      rm -f "$TMPFILE"
#
    else
#
#     No active mount point matched the specified argument...
#     Discard the temporary file, complain, and bail out.
#
      rm -f "$TMPFILE"
      echo >&2 "$0: '$1' is not mounted"
      exit 1
    fi
#
  else
#
#   The command line did not specify exactly one argument...
#   Complain, and bail out.
#
    echo >&2 "$0: incorrect number of arguments"
    echo >&2 "usage: umount <path>"
    exit 2
  fi
#
# On successful completion, ensure we set the exit status appropriately.
#
  exit 0
#
# $RCSfile: umount,v $: end of file
