about summary refs log tree commit diff stats
path: root/home-manager/packages/scripts/apps/aumo
blob: 014df638bb9bbe5094792de5318fba364b21ad71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="1.1.4" . %SHELL_LIBRARY_PATH

# TODO rewrite this

if [ "$1" = "-u" ];then udisksctl unmount -b "$(cat "$HOME/.cache/aumo/mnt_disk")"; exit 0; fi
all_available_disks=$(mktemp)
awk '{print $4}' /proc/partitions | sed '1 s|.*||' > "$all_available_disks"

mounted_disk=$(mktemp)
awk '{print $1}' /proc/mounts > "$mounted_disk"

unmounted_disks=$(mktemp)

for i in $(cat $all_available_disks);do
    if ! grep -qw "/dev/$i" $mounted_disk && ! ls /sys/block/$i/*/partition > /dev/null 2> /dev/null;then echo $i >> $unmounted_disks;fi
done


u=1
disk=-1
while [ $disk -lt 0 ] || [ $disk -gt $(wc -l $unmounted_disks | awk '{print $1}') ];do

    [ "$1" = "-v" ] &&  for i in $(cat $unmounted_disks);do
                            sudo fdisk -l "/dev/$i"
                        done

    for i in $(cat $unmounted_disks);do
        printf "%4s) %s\n" "$u" "$i"
        u=$(( u + 1 ))
    done

    printf "%4s) Exit\n" "0"
    printf "Input number: "
    read -r disk

    [ $disk -lt 0 ] || [ $disk -gt $(wc -l $unmounted_disks | awk '{print $1}') ] && printf "Wrong number. Please retry\n"
    [ $disk -eq 0 ] && exit 0
done

mnt_disk="/dev/$(awk -v n=$disk 'NR==n' $unmounted_disks)"

if ! [ "$1" = "-v" ] && [ -n "$1" ];then sudo mount "$mnt_disk" "$1" && exit 0;fi
[ -n "$2" ] && sudo mount "$mnt_disk" "$1" && exit 0
if mount | grep -q ~/mnt ;then dien "Something is mounted at ~/mnt";else udisksctl mount -b "$mnt_disk";fi

[ -e $HOME/.cache/aumo/mnt_disk ] || mkdir -p $HOME/.cache/aumo
echo $mnt_disk >> $HOME/.cache/aumo/mnt_disk
rm $all_available_disks
rm $mounted_disk
rm $unmounted_disks
[ -d /tmp/LIB_FILE_TEMP_DIR/ ] && rm -r /tmp/LIB_FILE_TEMP_DIR/