summaryrefslogtreecommitdiff
path: root/.local/bin/mounter
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/mounter')
-rwxr-xr-x.local/bin/mounter94
1 files changed, 94 insertions, 0 deletions
diff --git a/.local/bin/mounter b/.local/bin/mounter
new file mode 100755
index 0000000..9e3c6a5
--- /dev/null
+++ b/.local/bin/mounter
@@ -0,0 +1,94 @@
+#!/bin/bash
+
+# Mounts Android Phones and USB drives (encrypted or not). This script will
+# replace the older `dmenumount` which had extra steps and couldn't handle
+# encrypted drives.
+# TODO: Try decrypt for drives in crtypttab
+# TODO: Add some support for connecting iPhones (although they are annoying).
+
+IFS='
+'
+# Function for escaping cell-phone names.
+escape() { echo "$@" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g"; }
+
+# Check for drives.
+lsblkoutput="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")"
+# Get all LUKS drives
+allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)"
+# Get a list of the LUKS drive UUIDs already decrypted.
+decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")"
+# Functioning for formatting drives correctly for dmenu:
+filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}'; }
+
+# Get only LUKS drives that are not decrypted.
+unopenedluks="$(for drive in "${allluks[@]}"; do
+ uuid="${drive%% *}"
+ uuid="${uuid//-/}" # This is a bashism.
+ if [ "$decrypted" != "" ]; then
+ for open in "${decrypted[@]}"; do
+ [ "$uuid" = "$open" ] && break 1
+ done && continue 1
+ fi
+ echo "🔒 $drive"
+done | filter)"
+
+# Get all normal, non-encrypted or decrypted partitions that are not mounted.
+normalparts="$(echo "$lsblkoutput" | grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/💾 /" | filter)"
+
+# Add all to one variable. If no mountable drives found, exit.
+alldrives="$(echo "$unopenedluks
+$normalparts" | sed "/^$/d;s/ *$//")"
+
+# Quit the script if a sequential command fails.
+set -e
+
+test "$alldrives" != ""
+
+# Feed all found drives to dmenu and get user choice.
+chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)"
+
+# Function for prompting user for a mountpoint.
+getmount() {
+ mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")"
+ test "$mp" != ""
+ if [ ! -d "$mp" ]; then
+ mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?")
+ [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || doas mkdir -p "$mp")
+ fi
+}
+
+attemptmount() {
+ # Attempt to mount without a mountpoint, to see if drive is in fstab.
+ doas mount "$chosen" || return 1
+ notify-send "💾Drive Mounted." "$chosen mounted."
+ exit
+}
+
+case "$chosen" in
+💾*)
+ chosen="${chosen%% *}"
+ chosen="${chosen:1}" # This is a bashism.
+ attemptmount || getmount
+ doas mount "$chosen" "$mp" # -o uid="$(id -u)",gid="$(id -g)"
+ notify-send "💾Drive Mounted." "$chosen mounted to $mp."
+ ;;
+
+🔒*)
+ chosen="${chosen%% *}"
+ chosen="${chosen:1}" # This is a bashism.
+ # Number the drive.
+ while true; do
+ [ -f "/dev/mapper/usb$num" ] || break
+ num="$(printf "%02d" "$((num + 1))")"
+ done
+
+ # Decrypt in a terminal window
+ "${TERMINAL:-st}" -n floatterm -g 60x1 -e doas cryptsetup open "$chosen" "usb$num"
+ # Check if now decrypted.
+ test -b "/dev/mapper/usb$num"
+
+ attemptmount || getmount
+ doas mount "/dev/mapper/usb$num" "$mp" # -o uid="$(id -u)",gid="$(id -g)"
+ notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp."
+ ;;
+esac