#!/bin/sh
#
# File: mount
# $Id$
#
# =====================================================================
#
# Copyright (C) 2006, 2007 by Keith Marshall
#  mailto:keithmarshall@users.sourceforge.net
#
# This file is part of MSYS
#  http://www.mingw.org/msys.shtml
#
# 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"}
#
# 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
#
      else
#
#       Found a conflicting active mount...
#       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"
#
      else
#
#       Specified mount point is already in use...
#       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 [<win32path> <msyspath>]"
      exit 2 ;;
  esac
#
# On successful completion, ensure we set the exit status appropriately.
#
  exit 0
#
# $RCSfile$: end of file
