description "Auto mount and unmount hot pluggable zoox disks"

start on (block-device-added or block-device-removed)

# Use 'instance' so that when multiple events are emitted simultaneously
#   (e.g. at boot).  Lack of this results a pseudo-random selection
#   of block devices actually mounting at boot time.
instance $DEVNAME

script
  # If the disk label contains 'zooxdata' then do the action
  if [ -n "$(echo ${ID_FS_LABEL} | grep zooxdata)" ]; then
    MOUNTPOINT=/media/zoox/${ID_FS_LABEL}
    if [ "${ACTION}" = "add" ]; then
      # On 'add', create the mount point if needed with the proper
      #   owner, then mount in the background
      if [ ! -d "${MOUNTPOINT}" ]; then
        mkdir ${MOUNTPOINT}
        chown zoox:zoox ${MOUNTPOINT}
      fi
      echo "$(date) Mounting ${MOUNTPOINT}"
      # mount may take time, start in a subshell
      (mount ${DEVNAME} ${MOUNTPOINT})
    elif [ "${ACTION}" = "remove" ]; then
      # On 'remove' unmount then remove the created directory.
      echo "$(date) UnMounting ${MOUNTPOINT}"
      (umount -f ${MOUNTPOINT})
      if [ -d "${MOUNTPOINT}" ]; then
        rmdir ${MOUNTPOINT}
      fi
    fi
  fi
end script
