#! /bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: addswap,v 1.6.2.1 92/06/24 13:34:55 marker Exp $

#
# Add swap directories named in /etc/fstab.
#

PATH=/bin:/usr/bin

main()
{
	case "$1" in
		start)
			add_swap
			;;
		stop)
			;;
		*)
			echo "$0: usage: $0 [start|stop]"
			exit 1
			;;
	esac
}

#
# find each swap device in /etc/fstab, and add it to the swap space
#

add_swap()
{
	get_dev | while read swapdev
	do
		swaplen=`get_len "$swapdev"`
		case "$swaplen" in
			[0-9]*)
				;;
			*)
				echo "Could not get length for device $swapdev: $swaplen"
				continue
				;;
		esac
		echo "Adding swap device $swapdev"
		/etc/swap -a "$swapdev" 0 "$swaplen"
	done
}

#
# get devices from /etc/fstab marked "swap".  The regular expression here
# takes the last path element of the first field if the third field is
# the word "swap".  We don't use awk because floating point wierdness can
# cause awk to pass all of the filesystems, causing them to be mounted as
# swap, which can destroy a system.
#

get_dev()
{
	sed -n 's/^\([^#][^ 	]*\)[ 	][ 	]*[^ 	]*[ 	][ 	]*swap[ 	].*$/\1/p' /etc/fstab
}

#
# get the length of the named partition
#

get_len()
{
	part=`expr "$1" : '^.*s\([0-9][0-9]*\)$'`

	rdev=`echo $1 | sed 's,^/dev/,/dev/r,'`
	/etc/prtvtoc -h "$rdev" | awk '
	($1 == "'"$part"'") {
		print $5;
	}'
}

main ${1+"$@"}
exit 0
