swaydots

my dotfiles
git clone https://git.awy.one/swaydots.git
Log | Files | Refs | README | LICENSE

mounter (3999B)


      1 #!/bin/bash
      2 
      3 IFS='
      4 '
      5 # Function for escaping cell-phone names.
      6 escape(){ echo "$@" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g" ;}
      7 
      8 # Check for phones.
      9 phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/📱/")"
     10 mountedphones="$(rg "simple-mtpfs" /etc/mtab)"
     11 # If there are already mounted phones, remove them from the list of mountables.
     12 [ -n "$mountedphones" ] && phones="$(for phone in $phones; do
     13   for mounted in $mountedphones; do
     14     escphone="$(escape "$phone")"
     15     [[ "$mounted" =~ "$escphone" ]] && break 1
     16   done && continue 1
     17   echo "$phone"
     18 done)"
     19 
     20 # Check for drives.
     21 lsblkoutput="$(doas lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")"
     22 # Get all LUKS drives
     23 allluks="$(echo "$lsblkoutput" | rg crypto_LUKS)"
     24 # Get a list of the LUKS drive UUIDs already decrypted.
     25 decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")"
     26 # Functioning for formatting drives correctly for mew:
     27 filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}' ; }
     28 
     29 # Get only LUKS drives that are not decrypted.
     30 unopenedluks="$(for drive in $allluks; do
     31   uuid="${drive%% *}"
     32   uuid="${uuid//-}"	# This is a bashism.
     33   [ -n "$decrypted" ] && for open in $decrypted; do
     34     [ "$uuid" = "$open" ] && break 1
     35   done && continue 1
     36   echo "🔒 $drive"
     37 done | filter)"
     38 
     39 # Get all normal, non-encrypted or decrypted partitions that are not mounted.
     40 normalparts="$(echo "$lsblkoutput"| rg -v crypto_LUKS | rg 'part|rom|crypt' | sed "s/^/💾 /" | filter )"
     41 
     42 # Add all to one variable. If no mountable drives found, exit.
     43 alldrives="$(echo "$phones
     44 $unopenedluks
     45 $normalparts" | sed "/^$/d;s/ *$//")"
     46 
     47 # Quit the script if a sequential command fails.
     48 set -e
     49 
     50 test -n "$alldrives"
     51 
     52 # Feed all found drives to mew and get user choice.
     53 chosen="$(echo "$alldrives" | mew -p "Mount which drive?" -i)"
     54 
     55 # Function for prompting user for a mountpoint.
     56 getmount(){
     57   mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | mew -i -p "Mount this drive where?")"
     58   test -n "$mp"
     59   if [ ! -d "$mp" ]; then
     60     mkdiryn=$(printf "No\\nYes" | mew -i -p "$mp does not exist. Create it?")
     61     [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || doas_askpass mkdir -p "$mp")
     62   fi
     63 }
     64 
     65 attemptmount(){
     66   # Attempt to mount without a mountpoint, to see if drive is in fstab.
     67   doas mount "$chosen" || return 1
     68   notify-send "💾Drive Mounted." "$chosen mounted."
     69   exit
     70 }
     71 
     72 case "$chosen" in
     73   💾*)
     74     chosen="${chosen%% *}"
     75     chosen="${chosen:1}"	# This is a bashism.
     76     parttype="$(echo "$lsblkoutput" | rg "$chosen" | awk '{print $NF}' | tr -d '[:space:]\r\n')"
     77     attemptmount || getmount
     78     case "${parttype##* }" in
     79       vfat) doas mount -t vfat "$chosen" "$mp" -o rw,umask=0000 ;;
     80       btrfs) doas mount "$chosen" "$mp" ;;
     81       ntfs) doas mount -t ntfs3 "$chosen" "$mp" ;;
     82       *) doas mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)" ;;
     83     esac
     84     notify-send "💾Drive Mounted." "$chosen mounted to $mp."
     85     ;;
     86 
     87   🔒*)
     88     chosen="${chosen%% *}"
     89     chosen="${chosen:1}"	# This is a bashism.
     90     # Number the drive.
     91     while true; do
     92       [ -f "/dev/mapper/usb$num" ] || break
     93       num="$(printf "%02d" "$((num +1))")"
     94     done
     95 
     96     # Decrypt in a terminal window
     97     ${TERMINAL:-st} -a termfloat -e doas cryptsetup open "$chosen" "usb$num"
     98     # Check if now decrypted.
     99     test -b "/dev/mapper/usb$num"
    100 
    101     attemptmount || getmount
    102     doas mount "/dev/mapper/usb$num" "$mp"
    103     notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp."
    104 		;;
    105 
    106   📱*)
    107     notify-send "❗Note" "Remember to allow file access on your phone now."
    108     getmount
    109     number="${chosen%%:*}"
    110     number="${chosen:1}"	# This is a bashism.
    111     doas_askpass simple-mtpfs -o allow_other -o fsname="simple-mtpfs-$(escape "$chosen")" --device "$number" "$mp"
    112     notify-send "🤖 Android Mounted." "Android device mounted to $mp."
    113     ;;
    114 esac