#!/bin/sh -e
# Mount a partition by label or device identifier automatically
test "$1" = "--help" && echo "$0 <device> [mountpoint] [options...]" && exit 0
if test "$1" = "-u"
then
  mountpoint="$2"
  sudo umount "$mountpoint"
  code=$?
  sudo rm -d "$mountpoint"
  exit $?
fi

if grep -e "LABEL=$1[^\w/]" /etc/fstab
then mount -L "$@"; exit $?
fi
if grep -e "[^\w=/]$1[^\w/]" /etc/fstab
then mount "$@"; exit $?
fi
mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$1}"
if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab
then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code
fi
case "$1" in
  (sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$1";;
  (*) partition="-L $1";;
esac
shift $(expr 2 \& $# \> 1 \| 1)
#uid=$(id --user),gid=$(id --group), \
mountpoint "$mountpoint" >/dev/null 2>&1 || sudo mount -vo \
  users,X-mount.mkdir,noatime $partition $mountpoint "$@"
cd $mountpoint
exec $SHELL