#!/bin/sh printhelp () { printf "tmp\n" } show () { printf "$@\n" notify-send "dmount" "$@" } error_msg () { show "$@" exit 1 } while getopts "hpPugd:" o; do case "${o}" in d) ARGS="$ARGS $OPTARG" ;; p) USE_USER_ID="y" ;; P) unset USE_USER_ID ;; u) USERID="$OPTARG" ;; g) GROUPID="$OPTARG" ;; h) printhelp exit ;; *) printhelp exit 1;; esac done IS_HFS="" if [ -n "$WAYLAND_DISPLAY" ]; then DMENU="tofi" DEFARGS="-c $HOME/.config/tofi/themes/dmenu_vertical --prompt-text" elif [ -n "$DISPLAY" ]; then DMENU="dmenu" DEFARGS="-l 10 -p" else error_msg "Can't tell if Wayland or Xorg; is there a display server?" fi case "$DMENU" in dmenu) PASSARG="-P" ;; tofi) PASSARG="--hide-input=true" ;; esac check_crypto () { [ "$(lsblk -oFSTYPE "$1" | wc -l)" -gt 2 ] && printf "y" } is_crypto () { lsblk -oFSTYPE "$1" | grep -q "crypto_LUKS" && printf "y" } unlock_crypto () { NAME="$(cat /dev/null | $DMENU $DEFARGS "Name" $ARGS)" PASSWORD="$(cat /dev/null | $DMENU $DEFARGS "Password" $PASSARG $ARGS)" echo "$PASSWORD" | sudo cryptsetup open "$1" "$NAME" || FAILED="y" unset PASSWORD [ -n "$FAILED" ] && \ error_msg "Failed to decrypt partition $1!" show "Decrypted $1 successfully." } get_available () { lsblk -oPATH,SIZE,FSTYPE,MOUNTPOINT | tail -n+2 | while read i; do [ -z "$(echo "$i" | awk '{print $3}')" ] && continue [ -n "$(echo "$i" | awk '{print $4}')" ] && continue #echo "${i%% *}" #echo "$(lsblk -oMOUNTPOINT "${i%% *}" | tail -n+2 | grep -v -E '^\s*$')" [ -n "$(echo "$i" | grep -q "crypto_LUKS" && check_crypto "${i%% *}")" ] \ && continue echo "$i" done } getmount() { \ [ -z "$chosen" ] && exit 1 # shellcheck disable=SC2086 mp="$(find $1 2>/dev/null | $DMENU $ARGS $DEFARGS "Type in mount point.")" || error_msg "Canceled; no mount point chosen." test -z "$mp" && \ exit 1 if [ ! -d "$mp" ]; then mkdiryn=$(printf "No\\nYes" | $DMENU $ARGS $DEFARGS "$mp does not exist. Create it?") || \ exit 1 [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo mkdir -p "$mp") fi } mount_device () { getmount "/mnt /media /mount /home -maxdepth 3 -type d -empty $alreadymounted" if [ -n "$USE_USER_ID" ]; then if [ -n "$USERID" -a -n "$GROUPID" ]; then MOUNTPROPS="-o uid=$USERID,gid=$GROUPID" elif [ -n "$USERID" ]; then MOUNTPROPS="-o uid=$USERID" elif [ -n "$GROUPID" ]; then MOUNTPROPS="-o gid=$GROUPID" else MOUNTPROPS="-o $(grep "$(whoami)" /etc/passwd | sed 's/:/ /g' | awk '{print "uid="$3",gid="$4}')" fi fi if [ -n "$IS_HFS" ]; then MOUNTARGS="-t hfsplus" if [ -z "$MOUNTPROPS" ]; then MOUNTPROPS="-o force,rw" else MOUNTPROPS="${MOUNTPROPS},force,rw" fi fi sudo mount $MOUNTARGS $MOUNTPROPS "$1" "$mp" && show "Mounted successfully\n" } DEVICE="$(get_available | $DMENU $DEFARGS "Devices" $ARGS )" [ -z "$DEVICE" ] && exit 1 echo "$DEVICE" | grep -q "hfsplus" && IS_HFS="y" chosen="${DEVICE%% *}" if [ -n "$(is_crypto "$chosen")" ]; then unlock_crypto "$chosen" unset DEVICE chosen DMENU DEFARGS ARGS PASSARG dmount fi mount_device "$chosen" unset DEVICE chosen DMENU DEFARGS ARGS PASSARG