#!/bin/sh




#Name:
#un_blkid - negating check for filesystems
#
#
#Synopsis:
#un_blkid <device> [<fs_type>]
#
#
#Description:
#Checks a device for a filesystem of any but the specified type or for the
#non-existence of a filesystem of any known type.
#Only filesystems of types known by blkid are recognised.
#
#
#Operands:
#<device>
#Specifies the device to be checked (for example /dev/sda or
#/dev/disk/by-label/root).
#
#<fs_type>
#Specifies the filesystem type that should be negatively checked for.
#If omitted or empty, the program will check for the non-existence a filesystem
#of any known type.
#
#
#Exit Status:
#0
#The check was true.
#
#>0
#The check was false.
#
#
#Dependencies:
#This script depends on the blkid program from the util-linux package.




device="$1"
fs_type="$2"


#check whether an existing, readable and block special file was specified
if [ ! \( -r "${device}"  -a  -b "${device}" \) ]; then
	printf "Error: The specified device “${device}” does not exist, is not readable or is not a block special file.\n" >&2
	exit 2
fi


#search the device for a filesystem of a type known by blkid
blkid="$( blkid -o value -s TYPE -p "${device}" )"


#determine whether the check is false
if [ -z "${fs_type}" ]; then
	if [ -n "${blkid}" ]; then
		printf "Error: The device “${device}” contains a filesystem of a known type (“${blkid}”).\n" >&2
		exit 1
	fi
else
	if [ "${fs_type}" = "${blkid}" ]; then
		printf "Error: The device “${device}” contains a filesystem of the type “${fs_type}”.\n" >&2
		exit 1
	fi
fi




exit 0
















#Copyright © 2010, Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>.
#All rights reserved.
#
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
