#!/bin/sh
#
# $Id: LIB,v 4.1 1993/03/23 16:17:08 mike Exp $
#
# General functions used by init scripts.

if [ -n "$BASH_VERSION" ]; then
	use_escapes="-e"
else
	use_escapes=""
fi

function pathof()
{
	if [ "$2" = "" ]; then
		path=/etc:/sbin:/bin:/usr/bin:/usr/local/bin:/usr/lib:/usr/local/lib
	else
		path=$2
	fi

	echo $path | tr ':' '\n' | while read p
	do
		if [ -e $p/$1 ]; then
			echo $p/$1
			exit
		fi
	done
}


function pidof()
{
	ps ax | awk "/[^]]\\(?$1\\)?\$/ { print \$1 }"
}


function checkfs()
{
	device=$1
	fstype=$2

	# Find the fsck programs
	fsck=`pathof fsck /sbin:/etc:/bin:/usr/bin`
	efsck=`pathof efsck /sbin:/etc:/bin:/usr/bin`
	e2fsck=`pathof e2fsck /sbin:/etc:/bin:/usr/bin`
	xfsck=`pathof xfsck /sbin:/etc:/bin:/usr/bin`

	# Decide what options we use with each
	if [ "$AUTOBOOT" = "YES" ]; then
		check_mode=-a
	else
		check_mode=-r
	fi

	if [ -z "$fstype" ]; then
		echo "WARNING: Type of filesystem on $device unknown. Trying everything."
	fi
	if [ -z "$fstype" -o "$fstype" = "minix" ]; then
		if [ -n "$fsck" ]; then
			if $fsck $check_mode $device; then
				fstype="done"
			fi
		else
			echo "WARNING: Minix fsck not found. Minix fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ -z "$fstype" -o "$fstype" = "xia" -o "$fstype" = "$xiafs" ]; then
		if [ -n "$xfsck" ]; then
			if $xfsck $check_mode $device; then
				fstype="done"
			fi
		else
			echo "WARNING: Xia fsck not found. Xia fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ -z "$fstype" -o "$fstype" = "ext" ]; then
		if [ -n "$efsck" ]; then
			if $efsck $check_mode $device; then
				fstype="done"
			fi
		else
			echo "WARNING: Ext fsck not found. Ext fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ -z "$fstype" -o "$fstype" = "ext2" ]; then
		if [ -n "$e2fsck" ]; then
			if $e2fsck $check_mode $device; then
				fstype="done"
			fi
		else
			echo "WARNING: Ext2 fsck not found. Ext2 fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ "$fstype" = "msdos" ]; then
		: No support for msdos filesystem checks under Linux.
		roottype="done"
	fi
	if [ "$fstype" = "iso9660" ]; then
		: We do not check ISO filesystems...
		roottype="done"
	fi
	if [ "$fstype" != "done" ]; then
		echo "WARNING: No suitable fsck found or fsck encountered errors"
		echo "WARNING: Filesystem integrity on $device is not guaranteed..."
	fi
}
