dotfiles

beau's configuration files
git clone https://git.beauhilton.com/dotfiles.git
Log | Files | Refs | README

dmenumount (2279B)


      1 #!/usr/bin/env sh
      2 # Gives a dmenu prompt to mount unmounted drives.
      3 # If they're in /etc/fstab, they'll be mounted automatically.
      4 # Otherwise, you'll be prompted to give a mountpoint from already existsing directories.
      5 # If you input a novel directory, it will prompt you to create that directory.
      6 
      7 getmount() { \
      8 	[ -z "$chosen" ] && exit 1
      9 	mp="$(find $1 2>/dev/null | dmenu -i -p "Type in mount point.")"
     10 	[ "$mp" = "" ] && exit 1
     11 	if [ ! -d "$mp" ]; then
     12 		mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?")
     13 		[ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
     14 	fi
     15 	}
     16 
     17 mountusb() { \
     18 	chosen="$(echo "$usbdrives" | dmenu -i -p "Mount which drive?" | awk '{print $1}')"
     19 	sudo -A mount "$chosen" 2>/dev/null && notify-send "💻 USB mounting" "$chosen mounted." && exit 0
     20 	alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$2=="part"&&$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not \\( -path *%s -prune \\) \\ \n",$3}')
     21 	getmount "/mnt /media /mount /home -maxdepth 5 -type d $alreadymounted"
     22 	partitiontype="$(lsblk -no "fstype" "$chosen")"
     23 	case "$partitiontype" in
     24 		"vfat") sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000;;
     25 		*) sudo -A mount "$chosen" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo -A chown "$user":"$ug" "$mp";;
     26 	esac
     27 	notify-send "💻 USB mounting" "$chosen mounted to $mp."
     28 	}
     29 
     30 mountandroid() { \
     31 	chosen=$(echo "$anddrives" | dmenu -i -p "Which Android device?" | cut -d : -f 1)
     32 	getmount "$HOME -maxdepth 3 -type d"
     33 	simple-mtpfs --device "$chosen" "$mp"
     34 	notify-send "🤖 Android Mounting" "Android device mounted to $mp."
     35 	}
     36 
     37 asktype() { \
     38 	case $(printf "USB\\nAndroid" | dmenu -i -p "Mount a USB drive or Android device?") in
     39 		USB) mountusb ;;
     40 		Android) mountandroid ;;
     41 	esac
     42 	}
     43 
     44 anddrives=$(simple-mtpfs -l 2>/dev/null)
     45 usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}')"
     46 
     47 if [ -z "$usbdrives" ]; then
     48 	[ -z "$anddrives" ] && echo "No USB drive or Android device detected" && exit
     49 	echo "Android device(s) detected."
     50 	mountandroid
     51 else
     52 	if [ -z "$anddrives" ]; then
     53 		echo "USB drive(s) detected."
     54 	       	mountusb
     55 	else
     56 		echo "Mountable USB drive(s) and Android device(s) detected."
     57 		asktype
     58 	fi
     59 fi