summaryrefslogtreecommitdiff
path: root/dmount
blob: f281f4388842ff5a05af438dffa444f15f05c72b (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh

printhelp () {
  printf "tmp\n"
}

show () {
  printf "$@\n"
  notify-send "dmount" "$@"
}

error_msg () {
  show "$@"
  exit 1
}

while getopts "hpPugd:" o; do case "${o}" in
  d) ARGS="$ARGS $OPTARG" ;;
  p) USE_USER_ID="y" ;;
  P) unset USE_USER_ID ;;
  u) USERID="$OPTARG" ;;
  g) GROUPID="$OPTARG" ;;
  h)
    printhelp
    exit
    ;;
  *)
    printhelp
    exit 1;;
esac done

IS_HFS=""

if [ -n "$WAYLAND_DISPLAY" ]; then
  DMENU="tofi"
  DEFARGS="-c $HOME/.config/tofi/themes/dmenu_vertical --prompt-text"
elif [ -n "$DISPLAY" ]; then
  DMENU="dmenu"
  DEFARGS="-l 10 -p"
else
  error_msg "Can't tell if Wayland or Xorg; is there a display server?"
fi

case "$DMENU" in
  dmenu) PASSARG="-P" ;;
  tofi) PASSARG="--hide-input=true" ;;
esac

check_crypto () {
  [ "$(lsblk -oFSTYPE "$1" | wc -l)" -gt 2 ] && printf "y"
}

is_crypto () {
  lsblk -oFSTYPE "$1" | grep -q "crypto_LUKS" && printf "y"
}

unlock_crypto () {
  NAME="$(cat /dev/null | $DMENU $DEFARGS "Name" $ARGS)"
  PASSWORD="$(cat /dev/null | $DMENU $DEFARGS "Password" $PASSARG $ARGS)"
  echo "$PASSWORD" | sudo cryptsetup open "$1" "$NAME" || FAILED="y"
  unset PASSWORD
  [ -n "$FAILED" ] && \
    error_msg "Failed to decrypt partition $1!"
  show "Decrypted $1 successfully."

}

get_available () { lsblk -oPATH,FSTYPE,MOUNTPOINT | tail -n+2 | while read i; do
    [ -z "$(echo "$i" | awk '{print $2}')" ] && continue
    [ -n "$(echo "$i" | awk '{print $3}')" ] && continue
    #echo "${i%% *}"
    #echo "$(lsblk -oMOUNTPOINT "${i%% *}" | tail -n+2 | grep -v -E '^\s*$')"
    [ -n "$(echo "$i" | grep -q "crypto_LUKS" && check_crypto "${i%% *}")" ] \
      && continue
    echo "$i"
  done
}

getmount() { \
  [ -z "$chosen" ] && exit 1
  # shellcheck disable=SC2086
  mp="$(find $1 2>/dev/null | $DMENU $ARGS $DEFARGS "Type in mount point.")" || error_msg "Canceled; no mount point chosen."
  test -z "$mp" && \
    exit 1
  if [ ! -d "$mp" ]; then
    mkdiryn=$(printf "No\\nYes" | $DMENU $ARGS $DEFARGS "$mp does not exist. Create it?") || \
      exit 1
    [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo mkdir -p "$mp")
  fi
}

mount_device () {
  getmount "/mnt /media /mount /home -maxdepth 3 -type d -empty $alreadymounted"
  if [ -n "$USE_USER_ID" ]; then
    if [ -n "$USERID" -a -n "$GROUPID" ]; then
      MOUNTPROPS="-o uid=$USERID,gid=$GROUPID"
    elif [ -n "$USERID" ]; then
      MOUNTPROPS="-o uid=$USERID"
    elif [ -n "$GROUPID" ]; then
      MOUNTPROPS="-o gid=$GROUPID"
    else
      MOUNTPROPS="-o $(grep "$(whoami)" /etc/passwd | sed 's/:/ /g' | awk '{print "uid="$3",gid="$4}')"
    fi
  fi
  if [ -n "$IS_HFS" ]; then
    MOUNTARGS="-t hfsplus"
    if [ -z "$MOUNTPROPS" ]; then
      MOUNTPROPS="-o force,rw"
    else
      MOUNTPROPS="${MOUNTPROPS},force,rw"
    fi
  fi
  sudo mount $MOUNTARGS $MOUNTPROPS "$1" "$mp" && show "Mounted successfully\n"
}

DEVICE="$(get_available | $DMENU $DEFARGS "Devices" $ARGS )"
[ -z "$DEVICE" ] && exit 1
echo "$DEVICE" | grep -q "hfsplus" && IS_HFS="y"
chosen="${DEVICE%% *}"

if [ -n "$(is_crypto "$chosen")" ]; then
  unlock_crypto "$chosen"
  unset DEVICE chosen DMENU DEFARGS ARGS PASSARG
  dmount
fi

mount_device "$chosen"
unset DEVICE chosen DMENU DEFARGS ARGS PASSARG