#!/bin/ksh
#
#ident	"@(#)slice_opts.sh	1.9	05/06/08 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


conf_file="/opt/SUNWstaf/config-util/config.s+h.info"
reserved_conf_file="/opt/SUNWstaf/config-util/mount.info"
USAGE="Usage: $0 -a|-c|-g|-s"

. /opt/SUNWhcts/prgs/lib.sh

umount_all() {

	for i in $*
	do
		ret=`umount_them $i`
		if [ $? -ne 0 ]
		then
			echo $ret
			exit 1
		fi
	done
}

get_firstmp() {
	typeset adisk
	if [ $# -ne 1 ]
	then
		echo "Usage: $0 device"
		exit 1
	fi
	adisk=$1
	adisk=`echo $adisk | sed -e "s/[a-z][0-9][0-9]*$//"`
	tt=`mount | grep $adisk | head -1 | awk '{print $1}'`
	echo $tt
}

save_reserved_config() {

	rsh -l root $hostname ":>${reserved_conf_file}"
	if [ $? -ne 0 ]
	then
		echo "!Error: Can not write ${reserved_conf_file}."
		exit 1
	fi

	for i in $*
	do
		i=`echo $i | sed -e "s/s[0-9][0-9]*//"`
		tt=`mount | grep $i | head -1 | awk '{print $3 "  " $1}'`
		rsh -l root $hostname "echo \"$tt\" >>${reserved_conf_file}"
	done
}

save_config() {

	rsh -l root $hostname ":>${conf_file}"
	if [ $? -ne 0 ]
	then
		echo "!Error: Can not write ${conf_file}."
		exit 1
	fi

	for i in $*
	do
		i=`echo $i | sed -e "s/s[0-9][0-9]*//"`
		rsh -l root $hostname "echo \"$i 4 fr test\" >>${conf_file}"
	done
}

set_filter() {

        if [ $# -ne 1 ]
        then
                echo "Usage: $0 tech"
                return 1
        fi
        tech=$1
        
	case "$tech" in
	"usbdisk")
                filters[${#filters[*]}]="${tech}_filter"
		disk_required=1
	;;
	"sata" | "scsi" | "raid" | "fibre") 
		reserved_disks=""
		disk_required=1
	;;
	esac

}

check_slice() {

	typeset tech
	typeset disks
	typeset disk_count
	typeset reget
	
	tech=""	
	disks=""
	reget=0
	disk_count=0
        config_dir="/opt/SUNWstaf/config-util"
        conf_file="${config_dir}/config.s+h.info"

	# parse the arguments
	if [ $# -ne 1 ]
	then
		echo "Usage: $0 tech"
		return 1
	fi
	tech=$1

	#exit $err_reset

#        if [ -f ${conf_file} ]
#        then
#                while read line
#                do
#                        line=`echo $line | awk '{print $1}'`
#                        disks="$disks ${line}s2"
#			let "disk_count = ${disk_count} + 1"
#                done<${conf_file}
#		if [ $disk_count -lt ${disk_required} ]
#		then
#			reget=1
#			disks=""
#		fi
#
#		for i in $disks
#		do
#			j=0
#			while [ $j -lt ${#filters[*]} ]
#                	do
#				${filters[$j]} ${i} >/dev/null
#				if [ $? -eq 0 ]
#				then
#					reget=1
#					disks=""
#					break
#				fi
#				let "j = $j + 1"
#                        done
#
#		done
#	fi
#	# if file does not exist or file is corrupt
#	if [ ! -f ${conf_file} ] || [ $reget -eq 1 ]
#	then
		disks=`get_avail`
		ret=$?
		if [ $ret -eq ${err_lack} ]
		then
			# We display the message in cgi page. We reserve a outputed error message because the cgi page check status of harddisks by the message.
			echo "!ERROR:"
			exit ${err_lack}
		elif [ $ret -ne 0 ]
		then
			echo "!Error:"
			exit ${err_unknown}
		fi
		exit 0
#	fi
#
#	for i in $disks
#	do
#		ret=`check_slices $i`
#		if [ $? -ne 0 ]
#		then
#			echo "!Error: The size of the slices does not fit the test requirement. Please perform /opt/SUNWstaf/config-util/make.slices to create the slices."
#			exit $err_unknown
#		fi
#	done
#	for i in $disks
#	do
#		ret=`mount_them $i 2>&1`
#		if [ $? -ne 0 ]
#		then
#			echo "$ret"
#			exit $err_unknown
#		fi
#	done
#        if [ ! -f ${conf_file} ]
#	then
#		ret=`echo $disks | sed -e "s/\/dev\/[r]*dsk\///g"`
#		if [ $? -ne 0 ]
#		then
#			echo $ret
#			exit $err_unknown
#		fi
#	fi
#	disks=`echo $disks | sed -e 's/\/dev\/[r]*dsk\///g'`
#	save_config $disks
	
}

if [ $# -lt 3 ]
then
	echo "$USAGE"
	exit 2
elif [ $# -eq 3 ]
then
	if [ $2 != "-s" ] && [ $2 != "-g" ] && [ $2 != "-c" ] && [ $2 != "-u" ] && [ $2 != "-a" ] && [ $2 != "-m" ] && [ $2 != "-r" ] && [ $2 != "-t" ] && [ $2 != "-f" ]
	then
		echo "$USAGE"
		exit 2
	fi
elif [ $# -gt 3 ]
then
	echo "$USAGE"
	exit 2
fi

hostname=$1
shift
case $1 in
-s)           save_config $2;;
-u)           umount_all $2;;
-c)           set_filter $2; check_slice $2;;
-a)           set_filter $2; get_avail;;
-g)           size_filter $2;;
-m)           get_mp $2;;
-r)           reserved_filter $2;;
-t)           save_reserved_config $2;;
-f)           get_firstmp $2;;
esac
