#!/bin/sh

# call without arguments to see usage.
# mount filesystems to $HOME/mnt/{source_basename_or_label}
# benefits: mount paths are automatically chosen, mount paths are predictable, multiple sources can be mounted in one call

mount_directory="$HOME/mnt"
option_label=0
option_sudo=0
default_mount_options=""

while test $# -gt 0
do
  case "$1" in
    -s)
      option_sudo=1
      ;;
    -l)
      option_label=1
      ;;
    -o)
      shift
      default_mount_options="$1"
      ;;
    --)
      shift
      break
      ;;
    -*)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
    *)
      break
      ;;
  esac
  shift
done

if test $# -eq 0
then
  echo arguments: "[options]" source ...
  echo options
  echo "  -s use sudo"
  echo "  -l interpret sources as disk labels"
  echo "  -o string  custom mount options"
  echo mount directory: "$mount_directory"
fi

rmdir "$mount_directory/*" 2> /dev/null

for a in "$@"
do
  mount_options="$default_mount_options"
  if test 1 -eq "$option_label"
  then
    b="$mount_directory/$a"
    mount_options="-L $mount_options"
  else
    b="$mount_directory/$(basename "$a")"
  fi
  if mountpoint --quiet "$b"
  then
    echo "$b already mounted"
    continue
  fi
  if test 1 -eq "$option_sudo"
  then
    mount="sudo mount"
  else
    mount="mount"
  fi
  mkdir -p "$b"
  $mount $mount_options "$a" "$b"
done