Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 174 lines (150 sloc) 3.09 KB
#!/usr/bin/env zsh
TEMP=$(getopt -o 'shlum' --long 'help' -n 'automount.shy' -- "$@")
if [ $? -ne 0 ]; then
echo 'getopt failed: $!' >&2
exit 1
fi
eval set -- "${TEMP}"
unset TEMP
CONFIGFILE="${HOME}/automount_config"
TODO=list
SHOW=
while true; do
case "$1" in
'-s')
shift
SHOW="show"
continue
;;
'-l')
shift
TODO="list"
continue
;;
'-m')
shift
TODO="mount"
continue
;;
'-u')
shift
TODO="unmount"
continue
;;
'-h'|'--help')
echo "Usage:"
echo " $0 [options] [pattern]"
echo " -l list"
echo " -m mount"
echo " -u unmount"
echo " -s show commands"
echo " [pattern] shows mount matches"
shift
exit 0
;;
'--')
shift
break
;;
*)
echo 'Internal error!' >&2
exit 1
;;
esac
done
if [ ! -z $1 ]; then
PATTERN=$1
shift
fi
for arg; do
PATTERN=${PATTERN}"|"$arg
done
# l list
# m mount
# u unmount
# "sshfs -o reconnect -o idmap=user bee@macheteinfach.molgen.mpg.de:/project/beehive /home/wwwutz/project/beehive"
declare -a SSH
if [ -e $CONFIGFILE ]; then
. $CONFIGFILE
else
print "missing config file $CONFIGFILE"
exit 1
fi
ROOT=${HOME}
if [ -z ${PATTERN} ]; then
# kein pattern, liste alle
case "${TODO}" in
'list'|'unmount')
PATTERN="/"
;;
'mount')
echo "mount needs pattern"
exit 1
;;
*)
echo 'Internal error!' >&2
exit 1
;;
esac
fi
declare -A FSTYPE
FSTYPE=( [macfuse]=OK [nodir]='-' [notmounted]='-' )
GUID=$( id -g )
for AMD in "${SSH[@]}"; do
if [[ "${AMD}" =~ "${PATTERN}" ]]; then
SRC="${AMD% *}"
DST="${AMD//* }"
if [ "${DST}" = "${SRC}" ]; then
DST="${HOME}${AMD//*:}"
fi
if [ -d "${DST}" ]; then
STAT=( "$( mount | awk -v dst=$DST '$3 == dst {print $4}' | tr -cd '[[a-zA-Z]]' )" )
# macfuse == already mounted
if [[ "x$STAT" == "x" ]]; then
STAT='notmounted'
fi
else
STAT='nodir'
fi
if [ -n "$SHOW" ]; then
echo -n '# '
fi
if [ $TODO = "list" ]; then
if [ $STAT = "macfuse" ]; then
printf "%-12s %3s %s on %s\n" ${DST##*/} ${FSTYPE[${STAT}]} ${SRC} ${DST}
else
printf "%-12s %3s %s\n" ${DST##*/} ${FSTYPE[${STAT}]} ${SRC}
fi
fi
if [ $TODO = "mount" ]; then
if [ $STAT != "macfuse" ]; then
if [ "$STAT" = "nodir" ]; then
CMD="mkdir -vp ${DST}"
if [ $SHOW ]; then
echo "${CMD}"
else
${CMD}
fi
fi
CMD="sshfs -o reconnect -o idmap=user -o uid=${UID} -g gid=${GUID} ${SRC} ${DST}"
if [ -n "$SHOW" ]; then
echo "${CMD}"
else
echo "mounting ${SRC} to ${DST}"
eval ${CMD}
fi
fi
fi
if [ $TODO = "unmount" ]; then
if [ $STAT = "macfuse" ]; then
CMD="umount -v ${DST}"
if [ -n "$SHOW" ]; then
echo "${CMD}"
else
echo "unmounting ${DST}"
eval ${CMD}
fi
fi
fi
fi
done