#ident	"@(#)proto:desktop/menus/fs	1.1.1.33"

function fs_recalc
{
	integer fstot=0 i n
	integer dsk=$1

	for i in $VTOC_SLICES
	do
		if [ "${SLDISK[i]}" = "$dsk" ]
		then
			if [ "${SLTYPE[i]}" != "$off" -a "${SLTYPE[i]}" != "memfs" ]
			then
				let fstot+=SLSIZE[i]
			fi
		fi
	done
	if [ ! "${SLTYPE[17]}" = "$off" -a "$dsk" = 2 ]
	then
		if [ "$fstot" -eq "${SLSIZE[17]}" ]
		then
			integer type=SLFLD[17]
			integer size=SLFLD[17]+1
			let fstot-=SLSIZE[17]
			fld_change $FSFID $type "$off"
			fld_change $FSFID $size ""
			fld_gray $FSFID $size 1
		fi
	fi
	let i=$FSTOT_DISPLAY+dsk
	let n=SLFLD[i]+2 
	fld_pchange $FSFID $n "$fstot"
}

function inodechk
{
	return 0
}
function blkschk
{
	integer i=$1
	case "${SLTYPE[i]}" in
	bfs)
		case "${SLBLKSIZE[i]}" in
		512)
			;;
		*)
			errmsg $FS_BLKSIZE_ERR
			return 1 ;;
		esac
		;;
	s5)
		case "${SLBLKSIZE[i]}" in
		512 | 1024 | 2048)
			;;
		*)
			errmsg $FS_BLKSIZE_ERR
			return 1 ;;
		esac
		;;
	ufs | sfs)
		case "${SLBLKSIZE[i]}" in
		4096 | 8192)
			;;
		*)
			errmsg $FS_BLKSIZE_ERR
			return 1 ;;
		esac
		;;
	vxfs)
		case "${SLBLKSIZE[i]}" in
		1024 | 2048 | 4096 | 8192)
			;;
		*)
			errmsg $FS_BLKSIZE_ERR
			return 1 ;;
		esac
		;;
	esac
	msgnoerr
	return 0
}
function choose_inodes
{ 
	integer i=$1
	choose  -help "helpwin fsadvopts" "${lim_inode[i]}" "$No" "$Yes"
}

function check_inodes
{
integer i=$1
typeset -l arg1=${lim_inode[i]}
case $arg1 in
	$Yes | $Y)
		SLINODES[i]="$Yes"
		return 0;
		;;
	$No | $N)
		SLINODES[i]="$No"
		return 0;
		;;
	*)
		msg $Enter_yesno
		return 1;
		;;
esac
}
function choose_blksize
{
	integer i=$ATTRSLC
	case "${SLTYPE[i]}" in
	bfs)
		blksizes="512"
		;;
	s5)
		blksizes="512 1024 2048"
		;;
	sfs)
		blksizes="4096 8192"
		;;
	ufs)
		blksizes="4096 8192"
		;;
	vxfs)
		blksizes="1024 2048 4096 8192"
		;;
	esac
	CHOOSE_TITLE="$BLKTYPE_CHOOSE"
	CHOOSE_FOOTER="$BLKTYPE_FOOTER"
	case " $blksizes " in
	*" ${SLBLKSIZE[i]} "*)
		choose  -f -help "helpwin fsadvopts" "${SLBLKSIZE[i]}" $blksizes
		;;
	*)
		choose  -f -help "helpwin fsadvopts" "${blksizes%% *}" $blksizes
		;;
	esac
}

function choose_attrs
{
	integer i=$ATTRSLC
	if [ "${SLTYPE[i]}" = "$off" -o "${SLTYPE[i]}" = memfs -o "${SLTYPE[i]}" = "$slice" -o "$CHOOSING_FSTYPE" -eq 1 ]
	then
		return 0
	fi

	open_form 
	CUR_FSFIDX=$FID
	let FSFIDX[i]=$FID
	setup_table -offset 1 -rows 2 -cols 2 -titles FSX_TITLE -widths FSX_WIDTH
	next_field_prompt -l "$blocksDESC"
	let FSFIDXFLD[i]=$FIELDCNT
	next_field -entry msgnoerr  -exit "blkschk $i" -help "helpwin fsadvopts" -choice "choose_blksize $i" SLBLKSIZE[i]
	next_row
	next_field_prompt -l "$inodesDESC"
	lim_inode[i]=${SLINODES[i]}
	next_field -entry msgnoerr -exit "check_inodes $i" -help "helpwin fsadvopts" -choice "choose_inodes $i" lim_inode[i]
	next_row
	let FSXTABLE_WIDTH=TABLE_WIDTH
	place_window -left 15 -above 15 $FSXTABLE_WIDTH+2 10 -fg $COMBO2_FG -bg $COMBO2_BG -title "$FSCONF_ENTRY" -current 'footer "$GENERIC_FORM_FOOTER"'
	typeset widX=$CURWIN
	run_form $CUR_FSFIDX
	return 0
}
function set_attrchoose
{
	integer i=$1
	ATTRSLC=$i
	if [ "${SLTYPE[i]}" = "$off" -o "${SLTYPE[i]}" = memfs -o "${SLTYPE[i]}" = "$slice" ]
	then
		footer "$GENERIC_FORM_FOOTER"
	else
		footer "$ATTR_FOOTER"
	fi
	set_hotkey 6 choose_attrs 
	return 0
}

function choose_fstype
{
	CHOOSING_FSTYPE=1
	CHOOSE_TITLE="$FSTYPE_CHOOSE"
	CHOOSE_FOOTER="$FD_FS_CHOOSE_FOOTER"
	eval choose  -f \"${SLTYPE[$1]}\" ${SLCHOICES[$1]}
}

function check_fstype
{
	integer i=$1 t
	typeset _blksize _inodes
	integer size=SLFLD[i]+1

	if [ -n "${FSFIDX[i]}" ]
	then
		destroy_form ${FSFIDX[i]}
		FSFIDX[i]=
	fi
	CHOOSING_FSTYPE=0
	if (( !FLD_CHANGED ))
	then
		return 0
	fi
	case " ${SLCHOICES[i]} " in
	*" \"${SLTYPE[i]}\" "*)
		;;
	*)
		errmsg "$FS_ERR_TYPE"
		return 1
	esac
	if [ "${SLTYPE[i]}" = "$off" ]
	then

		if [ "${_SLTYPE[i]}" =  memfs ]
		then
			let 't=SLSIZE[2]-SLSIZE[i]'
			fld_change $FSFID ${SLFLD[2]}+1 "$t"
		fi
		fs_recalc ${SLDISK[i]}
		fld_change $FSFID $size ''
		fld_gray $FSFID $size 1
		_SLTYPE[i]="$off"
		if (( NDISKS > 1 ))
		then	
			case $i
			in
			8|15|17)
				;;
			*)
				integer disk=SLFLD[i]+2
				fld_change $FSFID $disk '1'
				fld_gray $FSFID $disk 1
				_SLDISK[i]=${SLDISK[i]}
				;;
			esac
		fi
	else

		fld_gray $FSFID $size 0
		if [ -z "${SLSIZE[i]}" ]
		then
			_SLSIZE[i]=0
			fld_change $FSFID $size 0
		fi
		if [ "${_SLTYPE[i]}" =  memfs ]
		then
			let 't=SLSIZE[2]-SLSIZE[i]'
			fld_change $FSFID ${SLFLD[2]}+1 "$t"
		fi
		if [ "${SLTYPE[i]}" =  memfs ]
		then
			let 't=SLSIZE[2]+SLSIZE[i]'
			fld_change $FSFID ${SLFLD[2]}+1 "$t"
		fi
		_SLTYPE[i]="${SLTYPE[i]}"
		fs_recalc ${SLDISK[i]}
		if [ ! "${SLTYPE[i]}" =  "$slice" ]
		then
			case "${SLTYPE[i]}" in
			bfs)
				_blksize=512
				_inodes=no
				;;
			s5)
				_blksize=1024
				_inodes=yes
				;;
			ufs)
				_blksize=4096
				_inodes=yes
				;;
			sfs)
				_blksize=4096
				_inodes=yes
				;;
			vxfs)
				_blksize=1024
				_inodes=yes
				;;
			esac
			SLBLKSIZE[i]=$_blksize
			SLINODES[i]=$_inodes
			if (( NDISKS > 1 ))
			then
				integer disk=SLFLD[i]+2
					if [ -z "${SLDISK[i]}" ]
					then
						fld_change $FSFID $disk 1
					fi
					if [ "${SLTYPE[i]}" =  memfs ]
					then
						fld_change $FSFID $disk 1
						fld_gray $FSFID $disk 1
					else
						fld_gray $FSFID $disk 0
					fi
			fi
		fi
	fi
	return 0
}

function check_diskno
{
	if (( !FLD_CHANGED ))
	then
		return 0
	fi
	case "${SLDISK[$1]}" in
	[0-9]*([0-9]))
		if (( SLDISK[$1] <= NDISKS ))
		then
			integer i=${SLDISK[$1]}-1
			INSTDISKS="$INSTDISKS $i"
			if [ "${SLDISK[$1]}" = 2 ]
			then
				if [ "${SLTYPE[17]}" = "$off" ]
				then
					integer type=SLFLD[17]
					integer size=SLFLD[17]+1
					integer alts
					(( (alts=USABLE_SIZE[1]*ALTS1/100) == 0 )) && alts=1
					fld_change $FSFID $type "$slice"
					fld_gray $FSFID $size 0
					fld_change $FSFID $size "$alts"
				fi
			fi
			_SLDISK[$1]=${SLDISK[$1]}
			fs_recalc 1
			fs_recalc 2
			return 0
		fi
		;;
	esac
	errmsg "$FS_ERR_DISKNUM"
	return 1
}

function check_fssize
{
	integer i min=${SLMIN[$1]} MAX_FS_SIZE

	if (( !FLD_CHANGED ))
	then
		return 0
	fi
	if [ "${SLTYPE[$1]}" = "$off" ]
	then
		return 0
	fi
	case "${SLSIZE[$1]}" in
	0)
		eval errmsg "\"  ${SLFS[$1]}: $FS_ERR_TOOSMALL\""
		return 1
		;;
	[0-9]*([0-9]))
		MAX_FS_SIZE=$(get_max_fs ${SLTYPE[$1]})
		if (( SLSIZE[$1] > MAX_FS_SIZE ))
		then
			errmsg "${SLFS[$1]}: $FS_ERR_HUGE $MAX_FS_SIZE"
			return 1
		else
			if [ "${SLTYPE[$1]}" =  memfs ]
			then
				let "i=SLSIZE[2]+(SLSIZE[$1]-_SLSIZE[$1])"
				fld_change $FSFID ${SLFLD[2]}+1 "$i"
			fi
			fs_recalc ${SLDISK[$1]}
			_SLSIZE[$1]=${SLSIZE[$1]}
			if [ "$1" = 10 ] && check_stand
			then
				footer "$FOOTER_HIGHCYLS"
				call sleep 3
			fi
			return 0
		fi
		;;
	*)
		eval errmsg "\"  ${SLFS[$1]}: $FS_ERR_TOOSMALL\""
		return 1
		;;
	esac
}

function fscheck
{
	typeset i
	integer dsk=0 disk
	while (( (dsk=dsk+1) <= NDISKS ))
	do
		disk=dsk-1
		let FSTOT[dsk]=0
		for i in $VTOC_SLICES
		do
			if [ "${SLDISK[i]}" = "$dsk" ]
			then
				if [ "${SLTYPE[i]}" != "$off" -a "${SLTYPE[i]}" != "memfs" ]
				then
					if  check_fssize $i 
					then
					:
					else
						return 1
					fi
					if  inodechk $i 
					then
					:
					else
						return 1
					fi
					if  blkschk $i 
					then
					:
					else
						return 1
					fi
					[ -n "${SLSIZE[i]}" ] && let FSTOT[dsk]+=SLSIZE[i]
				fi
			fi
		done
	done
	if [ "${FSTOT[2]}" -gt 0 -a  -z "${SLSIZE[17]}" ]
	then
		eval errmsg "$FS_ERR_NOALTS"
		return 1
	fi
	size_chk
	return 0
}

function fs
{
	integer i

	if [ "${UNIX_PARTITION[0]}" = NO ]
	then
		footer "$DISK0WARN"
		display -w "$DISK0warn" -bg $MAGENTA -fg $WHITE
		return
	fi

	if [ -z "$FSFID" ]
	then
		typeset gray=

		open_form -exit fscheck
		FSFID=$FID
		if (( NDISKS > 1 ))
		then
			setup_table -offset 1 -rows 20 -cols 5 -titles FS_TITLE -widths FS_WIDTH
		else
			setup_table -offset 1 -rows 20 -cols 4 -titles FS_TITLE -widths FS_WIDTH
		fi
		for i in 1 2 10 3 4 5 6 7 9 11 12 13 14 15 16 8 17
		do
			if [ -n "${SLFS[i]}" ]
			then
				next_field_prompt -l "${SLFS[i]}"
				let SLFLD[i]=$FIELDCNT+1
				next_field_prompt -l "${SLNAME[i]}"
				gray=
				if [ "${SLTYPE[i]}" = "$off" ]
				then
					gray=-gray
				fi
				case "${SLCHOICES[i]}" in
				*'" "'*)
					next_field -help "helpwin fs${i} fstype diskconf" -entry "set_attrchoose $i; msgnoerr" -exit "check_fstype $i" -choice "choose_fstype $i" SLTYPE[$i]
					;;
				*)
					next_field_prompt -l "${SLTYPE[i]}"
				esac

				next_field $gray -help "helpwin fs${i} fssize diskconf" -exit "check_fssize $i" -entry "set_attrchoose $i; msgnoerr" SLSIZE[$i]

				if [ "${SLTYPE[i]}" = "memfs" ]
				then
					gray=-gray
				fi

				if (( NDISKS > 1 ))
				then
					if (( SLDISK2[i] ))
					then
						next_field $gray -exit "check_diskno $i" -entry msgnoerr SLDISK[$i]
					else
						next_field_prompt -l ${SLDISK[$i]}
					fi
				fi
				next_row
			fi
		done
		next_row

# Display the Available/Current totals at bottom of window
		let FSTOT_DISPLAY=20
		i=8
		(( NDISKS > 1 )) && i=12
		add_field -p "$FS_DISK1" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
		let i+=3+${#FS_DISK1}
		add_field -p "$FS_AVAIL" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
		let SLFLD[FSTOT_DISPLAY+1]=$FIELDCNT
		let i+=5+${#FS_AVAIL}
		add_field -p "${USABLE_SIZE[0]}" -ilen 0 -py $TABLE_CUR_ROW+1 -px $i
		next_row
		let i-=5+${#FS_AVAIL}
		add_field -p "$FS_USED" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
		let i+=5+${#FS_USED}
		add_field -p "${FSTOT[1]}" -ilen 0 -py $TABLE_CUR_ROW+1 -px $i
		if (( NDISKS > 1 ))
		then
			next_row
			i=12
			add_field -p "$FS_DISK2" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
			let i+=3+${#FS_DISK2}
			add_field -p "$FS_AVAIL" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
			let SLFLD[FSTOT_DISPLAY+2]=$FIELDCNT
			let i+=5+${#FS_AVAIL}
			add_field -p "${USABLE_SIZE[1]}" -ilen 0 -py $TABLE_CUR_ROW+1 -px $i
			next_row
			let i-=5+${#FS_AVAIL}
			add_field -p "$FS_USED" -ilen 0 -py $TABLE_CUR_ROW+1 -px  $i
			let i+=5+${#FS_USED}
			add_field -p "${FSTOT[2]}" -ilen 0 -py $TABLE_CUR_ROW+1 -px $i
		fi
		next_row

		let FSTABLE_WIDTH=TABLE_WIDTH
	fi
	place_window -left 0 -above 0 $FSTABLE_WIDTH+2 20 -fg $COMBO2_FG -bg $COMBO2_BG -title "$FSCONF_ENTRY" -current 'footer "$ATTR_FOOTER"'
	typeset wid=$CURWIN
	run_form $FSFID
	fld_pchange $FSFID ${SLFLD[FSTOT_DISPLAY+1]} "${USABLE_SIZE[0]}"
	fld_pchange $FSFID ${SLFLD[FSTOT_DISPLAY+1]}+2 "${FSTOT[1]}"
	if (( NDISKS > 1 ))
	then
		fld_pchange $FSFID ${SLFLD[FSTOT_DISPLAY+2]} "${USABLE_SIZE[1]}"
		fld_pchange $FSFID ${SLFLD[FSTOT_DISPLAY+2]}+2 "${FSTOT[2]}"
	fi
}
