#!/bin/sh
#
# File: mount
# $Id: mount,v 1.2 2010/03/24 21:28:27 keithmarshall Exp $
#
# =====================================================================
#
# Copyright (C) 2006, 2007, 2009, 2010 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
# 2010-03-24: Updated for MSYS-1.0.14: --replace option added
#
# 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.
#
# =====================================================================
#
#
# Ensure the the `fstab' mount registry exists, and is writeable.
#
  >> ${MNTTAB="/etc/fstab"}
#
# Check for '--replace' option, so we can honour requests to reassign
# an existing mount table entry.
#
  case $1 in
    --replace | --replac | --repla | --repl) shift ; replace=yes ;;
    *) replace=no ;;
  esac
#
# Select the operation to be performed, based on number of argumemnts.
#
  case $# in
    0)
#
#     No arguments specified...
#     Simply report the current state of the mount table.
#
      /bin/msysmnt.exe ;;
#
    1)
#
#     One argument specified...
#     Check for any pre-existing mount which may conflict; if none,
#     try to match it to a default mount specification from the `fstab'
#     configuration file, and mount the specified directory, if any,
#     on its associated mount point.
#
      FSTAB=${FSTAB-"$MNTTAB.conf"}
      MNTPATH=`echo "$1" | tr '\\\\' /`
#
      if cat $MNTTAB | tr '\\' / | awk '
#
#       Check for pre-existing mount of specified argument;
#       set exit status to:--
#         0: if no such mount exists;
#         1: if argument is an already mounted directory;
#         2: if argument is a mount point already in use.
#
	BEGIN { status = 0 }
	/^#/ { $0 = "" }
	$1 == "'$MNTPATH'" { status = 1 }
	$2 == "'$MNTPATH'" { status = 2 }
	END { exit status }'
      then
#
#       No pre-existing mount conflicts...
#
	if WINPATH=`cat 2>/dev/null $FSTAB | tr '\\' / | awk '
#
#         Look up the default mount point specification;
#         set exit status, (assigned to "errno"), to:--
#           0: if found; (it is assigned to WINPATH);
#           1: if found, but multiply and ambiguously defined;
#           2: if not found.
#
	  BEGIN { status = 0; mount = "" }
	  /^#/ { $0 = "" }
	  $1 == "'$MNTPATH'" {
	     if( mount == "" ) mount = $0
	     else if( mount != $0 ) status = 1
	   }
	  $2 == "'$MNTPATH'" {
	     if( mount == "" ) mount = $0
	     else if( mount != $0 ) status = 1
	   }
	  END {
	     if( mount == "" ) exit 2
	     print mount
	     exit status
	   }'` errno=$?
	then
#
#         Found a default mount specification; activate it.
#
	  echo $WINPATH >> $MNTTAB
#
	elif test -f $FSTAB && test -r $FSTAB
	then
#
#         Read the configuration file, but could not find
#         a mount specification matching the argument.
#
	  case $errno in
	    1) echo >&2 "$0: $FSTAB: ambiguous reference for $MNTPATH" ;;
	    2) echo >&2 "$0: $FSTAB: no mount specification for $MNTPATH" ;;
	  esac
#
	elif test -f $FSTAB
	then
#
#         Found the configuration file, but could not read it.
#
	  echo >&2 "$0: $FSTAB: cannot read configuration file"
#
	else
#
#         Could not find the configuration file.
#
	  echo >&2 "$0: $FSTAB: configuration file not found"
	fi
#
      elif test x"$replace" = xyes
      then
#
#       Found a conflicting active mount,
#       but the 'replace' option WAS specified...
#       Tear it down, and remount.
#
        umount $MNTPATH
        mount $MNTPATH
        exit $?
#
      else
#
#       Found a conflicting active mount,
#       and the 'replace' option was NOT specified...
#       Display an appropriate diagnostic message, depending on
#       whether the argument represented a directory path name,
#       or a mount point, and bail out.
#
	case $? in
	  1) echo >&2 "$0: '$MNTPATH' is already mounted" ;;
	  2) echo >&2 "$0: mount point '$MNTPATH' is already in use" ;;
	esac
	exit 1
      fi ;;
#
    2)
#
#     Two arguments specified...
#     First is directory path name, second is mount point.
#
      WINPATH=`echo "$1" | tr '\\\\' /`
      MNTPATH=`echo "$2" | tr '\\\\' /`
#
      if cat $MNTTAB | tr '\\' / | awk '
#
#       Check that the mount point is not already in use;
#       set exit status to:--
#         0: if no existing mount table entry matches;
#         1: if mount point already in mount table.
#
	BEGIN { status = 0 }
	/^#/ { $0 = "" }
	$2 == "'$MNTPATH'" { status = 1 }
	END { exit status }'
      then
#
#       Mount point not yet assigned...
#       Update the mount table, to include the new specification.
#
	echo -e "$WINPATH\t$MNTPATH" >> "$MNTTAB"
#
      elif test x"$replace" = xyes
      then
#
#       The mount point is already in use,
#       but the 'replace' option WAS specified...
#       Tear it down, and remount.
#
        umount $MNTPATH
	echo -e "$WINPATH\t$MNTPATH" >> "$MNTTAB"
#
      else
#
#       Mount point is already in use,
#       and the 'replace' option was NOT specified...
#       Diagnose, and bail out.
#
	echo >&2 "$0: mount point '$MNTPATH' is already in use"
	exit 1
      fi ;;
#
    *)
#
#     More than two arguments specified...
#     Complain, and bail out.
#
      echo >&2 "$0: incorrect number of arguments"
      echo >&2 "usage: mount [--replace] [<win32path> <msyspath>]"
      exit 2 ;;
  esac
#
# On successful completion, ensure we set the exit status appropriately.
#
  exit 0
#
# $RCSfile: mount,v $: end of file
