first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / production / msys / bin / mount
1 #!/bin/sh
2 #
3 # File: mount
4 # $Id$
5 #
6 # =====================================================================
7 #
8 # Copyright (C) 2006, 2007 by Keith Marshall
9 #  mailto:keithmarshall@users.sourceforge.net
10 #
11 # This file is part of MSYS
12 #  http://www.mingw.org/msys.shtml
13 #
14 # MSYS is free software.  It is provided "as is", in the hope that it
15 # may be useful; there is NO WARRANTY OF ANY KIND, not even an implied
16 # warranty of MERCHANTABILITY or FITNESS FOR ANY PARTICULAR PURPOSE.
17 # At no time will the author accept liability for damages, however
18 # caused, resulting from the use of this software.
19 #
20 # Permission is granted to copy and redistribute this software, either
21 # as is, or in modified form, provided that:--
22 #
23 #   1) All such copies are distributed with the same rights
24 #      of redistribution.
25 #
26 #   2) The preceding disclaimer of warranty and liabality is
27 #      retained verbatim, in all copies.
28 #
29 #   3) Accreditation of the original author remains in place.
30 #
31 #   4) Modified copies are clearly identified as such, with
32 #      additional accreditation given to the authors of each
33 #      modified version.
34 #
35 # =====================================================================
36 #
37 #
38 # Ensure the the `fstab' mount registry exists, and is writeable.
39 #
40   >> ${MNTTAB="/etc/fstab"}
41 #
42 # Select the operation to be performed, based on number of argumemnts.
43 #
44   case $# in
45     0)
46 #
47 #     No arguments specified...
48 #     Simply report the current state of the mount table.
49 #
50       /bin/msysmnt.exe ;;
51 #
52     1)
53 #
54 #     One argument specified...
55 #     Check for any pre-existing mount which may conflict; if none,
56 #     try to match it to a default mount specification from the `fstab'
57 #     configuration file, and mount the specified directory, if any,
58 #     on its associated mount point.
59 #
60       FSTAB=${FSTAB-"$MNTTAB.conf"}
61       MNTPATH=`echo "$1" | tr '\\\\' /`
62 #
63       if cat $MNTTAB | tr '\\' / | awk '
64 #
65 #       Check for pre-existing mount of specified argument;
66 #       set exit status to:--
67 #         0: if no such mount exists;
68 #         1: if argument is an already mounted directory;
69 #         2: if argument is a mount point already in use.
70 #
71         BEGIN { status = 0 }
72         /^#/ { $0 = "" }
73         $1 == "'$MNTPATH'" { status = 1 }
74         $2 == "'$MNTPATH'" { status = 2 }
75         END { exit status }'
76       then
77 #
78 #       No pre-existing mount conflicts...
79 #
80         if WINPATH=`cat 2>/dev/null $FSTAB | tr '\\' / | awk '
81 #
82 #         Look up the default mount point specification;
83 #         set exit status, (assigned to "errno"), to:--
84 #           0: if found; (it is assigned to WINPATH);
85 #           1: if found, but multiply and ambiguously defined;
86 #           2: if not found.
87 #
88           BEGIN { status = 0; mount = "" }
89           /^#/ { $0 = "" }
90           $1 == "'$MNTPATH'" {
91              if( mount == "" ) mount = $0
92              else if( mount != $0 ) status = 1
93            }
94           $2 == "'$MNTPATH'" {
95              if( mount == "" ) mount = $0
96              else if( mount != $0 ) status = 1
97            }
98           END {
99              if( mount == "" ) exit 2
100              print mount
101              exit status
102            }'` errno=$?
103         then
104 #
105 #         Found a default mount specification; activate it.
106 #
107           echo $WINPATH >> $MNTTAB
108 #
109         elif test -f $FSTAB && test -r $FSTAB
110         then
111 #
112 #         Read the configuration file, but could not find
113 #         a mount specification matching the argument.
114 #
115           case $errno in
116             1) echo >&2 "$0: $FSTAB: ambiguous reference for $MNTPATH" ;;
117             2) echo >&2 "$0: $FSTAB: no mount specification for $MNTPATH" ;;
118           esac
119 #
120         elif test -f $FSTAB
121         then
122 #
123 #         Found the configuration file, but could not read it.
124 #
125           echo >&2 "$0: $FSTAB: cannot read configuration file"
126 #
127         else
128 #
129 #         Could not find the configuration file.
130 #
131           echo >&2 "$0: $FSTAB: configuration file not found"
132         fi
133 #
134       else
135 #
136 #       Found a conflicting active mount...
137 #       Display an appropriate diagnostic message, depending on
138 #       whether the argument represented a directory path name,
139 #       or a mount point, and bail out.
140 #
141         case $? in
142           1) echo >&2 "$0: '$MNTPATH' is already mounted" ;;
143           2) echo >&2 "$0: mount point '$MNTPATH' is already in use" ;;
144         esac
145         exit 1
146       fi ;;
147 #
148     2)
149 #
150 #     Two arguments specified...
151 #     First is directory path name, second is mount point.
152 #
153       WINPATH=`echo "$1" | tr '\\\\' /`
154       MNTPATH=`echo "$2" | tr '\\\\' /`
155 #
156       if cat $MNTTAB | tr '\\' / | awk '
157 #
158 #       Check that the mount point is not already in use;
159 #       set exit status to:--
160 #         0: if no existing mount table entry matches;
161 #         1: if mount point already in mount table.
162 #
163         BEGIN { status = 0 }
164         /^#/ { $0 = "" }
165         $2 == "'$MNTPATH'" { status = 1 }
166         END { exit status }'
167       then
168 #
169 #       Mount point not yet assigned...
170 #       Update the mount table, to include the new specification.
171 #
172         echo -e "$WINPATH\t$MNTPATH" >> "$MNTTAB"
173 #
174       else
175 #
176 #       Specified mount point is already in use...
177 #       Diagnose, and bail out.
178 #
179         echo >&2 "$0: mount point '$MNTPATH' is already in use"
180         exit 1
181       fi ;;
182 #
183     *)
184 #
185 #     More than two arguments specified...
186 #     Complain, and bail out.
187 #
188       echo >&2 "$0: incorrect number of arguments"
189       echo >&2 "usage: mount [<win32path> <msyspath>]"
190       exit 2 ;;
191   esac
192 #
193 # On successful completion, ensure we set the exit status appropriately.
194 #
195   exit 0
196 #
197 # $RCSfile$: end of file