#!/bin/sh
#
# (C) Copyright 1993, 1994 Mike Jagdis (jaggy@purplet.demon.co.uk)
#
#
# General functions used by init scripts.

# Figure out how use backslash escapes with this echo.
if [ -n "$BASH_VERSION" ]; then
	use_escapes="-e"
else
	use_escapes=""
fi

# Figure out what tty to use for interactive programs. If we don't have
# a controlling terminal we're probably running against the console (which
# can't become a controlling terminal under Linux) at boot time. In which
# case tty1 (the first VC) is probably a good guess. Hey, I'm not psychic!
if tty > /dev/null 2>&1; then
	CTTY=`tty`
else
	CTTY="/dev/tty1"
fi

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

	if [ -n "$BASH_VERSION" ]; then
		PATH=$path type -p $1
		exit
	fi

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


logger=`pathof logger`
function msg()
{
	if [ -n "$logger" ]; then
		$logger -s -p kern.$1 -t linux "$2"
	else
		echo "$2"
	fi
}


function killall_f()
{
	signal=-0
	procs=

	while [ $# -gt 0 ]; do
		case "$1" in
			-*)	signal=$1
				;;
			*)	procs="$procs $1"
				;;
		esac
		shift
	done

	doneit=1
	for p in $procs
	do
		for i in `pidof $p`
		do
			kill $signal $i
			doneit=0
		done
	done

	return $doneit
}


function wipeout()
{
	maxretries=5
	sleeptime=3
	procs=

	while [ $# -gt 0 ]; do
		case "$1" in
			-retries)	maxretries=$2
					shift
					;;
			-sleep|-pause)	sleeptime=$2
					shift
					;;
			*)		procs="$procs $1"
					;;
		esac
		shift
	done

	if [ -z "$killall" ]; then
		killall=`pathof killall /bin:/usr/bin`
		if [ -z "$killall" ]; then
			killall=killall_f
		fi
	fi

	if [ -z "$procs" ]; then
		return 0
	fi

	if $killall $procs 2> /dev/null; then
		sleep 1
	else
		return 1
	fi

	retries=1
	while $killall $procs 2> /dev/null && [ $retries -le $maxretries ]
	do
		retries=`expr $retries + 1`
		sleep $sleeptime
	done
	$killall -9 $procs 2> /dev/null

	return 0
}


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


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

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

	# If this is a forced check then force it.
	if [ "$checkfs" = "YES" -o "$checkfs" = "yes" ]; then
		check_mode=${check_mode}f
	fi

	if [ -z "$fstype" ]; then
		echo "WARNING: Type of filesystem on $device unknown. Trying everything."
	fi
	if [ -z "$fstype" -o "$fstype" = "minix" ]; then
		fsck=`pathof fsck.minix`
		if [ -z "$fsck" ]; then
			fsck=`pathof fsck`
		fi
		if [ -n "$fsck" ]; then
			$fsck $check_mode $device
			fstype="done"
		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
		xfsck=`pathof fsck.xiafs`
		if [ -z "$xfsck" ]; then
			xfsck=`pathof xfsck`
		fi
		if [ -n "$xfsck" ]; then
			$xfsck $check_mode $device
			fstype="done"
		else
			echo "WARNING: Xia fsck not found. Xia fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ -z "$fstype" -o "$fstype" = "ext" ]; then
		efsck=`pathof fsck.ext`
		if [ -z "$efsck" ]; then
			efsck=`pathof efsck`
		fi
		if [ -n "$efsck" ]; then
			$efsck $check_mode $device
			fstype="done"
		else
			echo "WARNING: Ext fsck not found. Ext fsck of $device filesystem *NOT* done"
		fi
	fi
	if [ -z "$fstype" -o "$fstype" = "ext2" ]; then
		e2fsck=`pathof fsck.ext2`
		if [ -z "$e2fsck" ]; then
			e2fsck=`pathof e2fsck`
		fi
		if [ -n "$e2fsck" ]; then
			$e2fsck $check_mode $device
			fstype="done"
		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.
		fstype="done"
	fi
	if [ "$fstype" = "iso9660" ]; then
		: We do not check ISO filesystems...
		fstype="done"
	fi
	if [ "$fstype" = "nfs" ]; then
		: We don't check them and probably shouldn't be here...
		fstype="done"
	fi
	if [ "$fstype" != "done" ]; then
		echo "WARNING: No suitable fsck found"
		echo "WARNING: Filesystem integrity on $device is not guaranteed..."
		return 1
	fi

	return 0
}
