#!/bin/sh
# this script depends on /lib/udev/vol_id from the udev package

# usage: un_vol_id <device> <fs_type>
# <device> may be any device that should be checked.
# if no <fs_type> is given, the check fails for any valid filesystem
# if <fs_type> is given, the check fails when a filesystem type <fs_type>
# is found on the device.

# note that the 'minix' fs is filtered out if checking for any valid fs,
# as it has been reported that this fs my be detected erroneously by vol_id.

if test ! -x "/lib/udev/vol_id"; then
  echo " - WARNING: vol_id is not available, impossible to run checks."
  exit 0
fi

dev=$1
fs=$2

vol_id="$(/lib/udev/vol_id -t $dev 2>&1)"

# vol_id output if $dev has an unknown filesystem
pattern="$(echo $dev | sed 's/\//\\\//g')"
unknown="$(/lib/udev/vol_id -t /dev/null 2>&1 | sed "s/\/dev\/null/$pattern/g")"

if [ "$vol_id" != "$unknown" ] && [ "$vol_id" != "minix" ] && [ -z "$fs" ]; then
  echo " - The device $dev contains a valid filesystem type $vol_id."
  exit 1
elif [ -n "$fs" ] && [ "$vol_id" = "$fs" ]; then
  echo " - The device $dev contains a filesystem type $fs."
  exit 1
fi
