aboutsummaryrefslogtreecommitdiffstats
path: root/setup-drive.sh
blob: f678178decec51cb264ec30dccdfa7d8c230b53b (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
#!/bin/sh
set -e

main() {
  parse_args "$@"
  ensure_fstab_entry
}

parse_args() {
  case "${1:-}" in
  -h | --help) usage ;;
  esac
}

usage() {
  echo "Usage: sudo [-E] ./setup-drive.sh [-h]"
  echo ""
  echo "Adds an fstab entry for an external media drive."
  echo ""
  echo "Environment variables (all optional):"
  echo "  FSTAB_UUID        UUID of the external media drive (default: 2c882f17-...)"
  echo "  FSTAB_MOUNT_POINT Mount point for the media drive  (default: /mnt/media)"
  echo "  FSTAB_FS_TYPE     Filesystem type of the drive     (default: xfs)"
  exit 0
}

ensure_fstab_entry() {
  _fstab_uuid="${FSTAB_UUID:-2c882f17-50e7-4099-a305-693f4aa0113b}"
  _fstab_mount_point="${FSTAB_MOUNT_POINT:-/mnt/media}"
  _fstab_fs_type="${FSTAB_FS_TYPE:-xfs}"
  _fstab_options="defaults"
  _fstab_dump="0"
  _fstab_pass="2"
  _fstab_entry="UUID=${_fstab_uuid} ${_fstab_mount_point} ${_fstab_fs_type} ${_fstab_options} ${_fstab_dump} ${_fstab_pass}"

  if ! grep -q "UUID=${_fstab_uuid}" /etc/fstab; then
    echo "Adding mount entry to /etc/fstab..."
    echo "${_fstab_entry}" | sudo tee -a /etc/fstab >/dev/null
    echo "Creating mount point if it doesn't exist..."
    sudo mkdir -p "${_fstab_mount_point}"
  else
    echo "Mount entry already exists in /etc/fstab"
  fi
}

main "$@"