petits-scripts-persos/interractive_mount

85 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
function usage () {
echo "Usage:"
echo "$0 <action> [mode]"
echo "action is either mount or unmount"
echo "action is text or nothing"
}
# If no display found, full text interface. use dmenu and dunstify else.
if [ "$2" = text ] || [ -z "$DISPLAY" ] ; then
function notify () {
echo -e "== $1 ==\n$2"
}
function ask () {
# $1 is the title
# $2 is the \n separated list of choices
# $3 is the number of choices
IFS=$'\n' read -r -d '' -a args <<< $(echo -e "$2")
dialog --no-items --menu "$1" 15 40 4 "${args[@]}" 2>&1 >/dev/tty
}
else
function notify () {
dunstify "$1" "$2"
}
function ask () {
# See interface specification up
echo -en "$2" | dmenu -i -l "$3" -p "$1"
}
fi
if [ "$1" = "mount" ] ; then
action=mount
elif [ "$1" = umount ] || [ "$1" = unmount ] ; then
action=umount
else
usage
exit 1
fi
mounted=""
umounted=""
mounted_nb=0
umounted_nb=0
# List mounted and unmounted disks
while read disk size mountpoint ; do
# Filters. Add yours
[[ "$disk" = /dev/sda* ]] && continue
[ "$disk" = /dev/mapper/cryptroot ] && continue
# Selection
if [ -z "$mountpoint" ] ; then
umounted="$umounted$disk $size\n"
((umounted_nb++))
else
mounted="$mounted$disk $size $mountpoint\n"
((mounted_nb++))
fi
done <<< $(lsblk -prno NAME,SIZE,MOUNTPOINTS)
# Prompt user for disk action
if [ "$action" = "mount" ] ; then
if [ -z "$umounted" ] ; then
notify "Mounting" "Nothing to mount"
else
read disk size <<< $(ask 'Mount' "$umounted" "$umounted_nb")
if [ -b "$disk" ] ; then
notify "Mounting $disk" "$(udisksctl mount -b "$disk" 2>&1)"
fi
fi
elif [ "$action" = "umount" ] ; then
if [ -z "$mounted" ] ; then
notify "Unmounting" "Nothing to unmount"
else
read disk size mountpoint <<< $(ask 'Unmount' "$mounted" "$mounted_nb")
if [ -b "$disk" ] ; then
notify "Unmounting $disk" "$(udisksctl unmount -b "$disk" 2>&1)"
fi
fi
fi