#! /bin/bash

# Copyright (c) 2009 by Michael Tautschnig <mt@debian.org>

vol_id="/lib/udev/vol_id"
blkid="/sbin/blkid"


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_uuid() {

  if [ -n "$blkid" ] ; then
    $blkid -c /dev/null -s UUID -o value $1
    exit $?
  fi

  $vol_id -u $1
  exit $?
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_label() {

  if [ -n "$blkid" ] ; then
    $blkid -c /dev/null -s LABEL -o value $1
    exit $?
  fi

  $vol_id -L $1
  exitcode=$?
  if [ $exitcode -eq 3 ] ; then
    exitcode=0
  fi
  exit $exitcode
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# main program

if [ -x $blkid ] ; then
  vol_id=""
else
  blkid=""
  if [ ! -x $vol_id ] ; then
    "Neither udev vol_id nor blkid found!"
    exit 1
  fi
fi

while getopts ul opt ; do
      case "$opt" in
        u) shift ; get_uuid $1 ;;
        l) shift ; get_label $1 ;;
      esac
done
