#ident	"@(#)sadmin:admin/menu/diskmgmt/badtrack/updbadlist	35.1"
#menu#  update the bad track file with the correct list
#help# 
#help#  It may be necessary to update the copy of the bad track list in 
#help#  /etc/badtracks because of the current copy having been destroyed,
#help#  having done track redirection from a bootable tape, or the list 
#help#  not having been kept up to date.

flags="-qq -k$$"
trap 'exit 1' 1 2 15
def_dev=1
MANUF=2
SIZE=2
FMT=/etc/dinit
sfmt=1
pdef=2
idef=2
file_def=y
fmtflag=
CONV=/etc/xformtrk
PRBAD=/usr/lbin/prbadlist
ROPT=-r

validdevs='
	1.  m320_0
	2.  m320_1
	3.  m320_1d0 
	4.  m320_1d1 
	5.  m360_0 		
	6.  m360_2 		
	7.  m360_1d0
	8.  m360_1d2
	9.  m323_0
	10. m323_1
	11. m323_1d0
	12. m323_1d1'

getdevice() {
	echo "\n	Valid Devices Are:\c"
	echo "${validdevs}\n"
	device=`checklist ${flags} -D"${def_dev}" -H "Valid Devices: ${validdevs}"\
	-fe "enter the device (default: ${def_dev}) [q] :" 1 2 3 4 5 6 7 8 9 10 11 12`
	case "${device}" in
		'1') device=m320_0 
		     def_dev=1 ;;
		'2') device=m320_1 
		     def_dev=2 ;;
		'3') device=m320_1d0 
		     def_dev=3 ;;
		'4') device=m320_1d1 
		     def_dev=4 ;;
		'5') device=m360_0 
		     def_dev=5 ;;
		'6') device=m360_2 
		     def_dev=6 ;;
		'7') device=m360_1d0 
		     def_dev=7 ;;
		'8') device=m360_1d2 
		     def_dev=8 ;;
		'9') device=m323_0
		     def_dev=9 ;;
		'10') device=m323_1
		     def_dev=10 ;;
		'11') device=m323_1d0
		     def_dev=11 ;;
		'12') device=m323_1d1
		     def_dev=12 ;;
	esac
	case $device in
	    m320_[01] | m320_1d[01])
		echo '
	Valid Disk Sizes are:
	1. 40 Megabyte
	2. 70 Megabyte\n'
		SIZE=`checklist ${flags} -D"${SIZE}" -ef "What size is your disk (default: ${SIZE})?" 1 2 `
		case "${SIZE}" in
			'1') Sz=40 
			     pr_sz='40  Meg';;
			'2') Sz=70 
			     pr_sz='70  Meg';;
		esac
		DTYPE=m320${Sz}
		echo '
	Valid Manufacturers are:
	1. Toshiba
	2. Micropolis
	3. Miniscribe
	4. Priam\n'
		MANUF=`checklist ${flags} -D"${MANUF}" -ef "Who Manufactures your Winchester (default: ${MANUF})?" 1 2 3 4`
		case "${MANUF}" in
			'1') Mfg=t 
			     pr_man=Toshiba ;;
			'2') Mfg=m 
			     pr_man=Micropolis ;;
			'3') Mfg=s 
			     pr_man=Miniscribe ;;
			'4') Mfg=p 
			     pr_man=Priam ;;
		esac
		DTYPE=${DTYPE}${Mfg}
		;;

	    m360_[02] | m360_1d[02])
		DTYPE=m360337
		pr_man=NA
	 	pr_sz=NA
		;;

	    m323_[01] | m323_1d[01])
		DTYPE=m323182
		pr_man=NA
	 	pr_sz=NA
		;;
	    *)
		    ;;
	esac
    FMTDEV=/dev/rdsk/${device}s7
    TFILE=/etc/badtracks/${device}
}

# add bad tracks in head, cylinder, bfi format; append to TFILE
addnewbad() {
	echo "\nEnter bad track numbers as <head> <cylinder> <bfi>  (Use"
	echo "-1 for an unknown bfi field);  End with a period (.): "
	while read INP 
	do
		if [ "${INP}" = "." ]
		then
			break
		fi
		set ${INP}
		if [ $# -ne 3 ]
		then
			echo "Bad input: enter as <head> <cylinder> <bfi>"
		else
			echo $1 $2 $3 >> ${TFILE}
		fi
	done
}

get323source() {
	echo '
	Valid options are:
	1. Use existing disk bad track file
	2. Use manufacturer\047s original list\n'
	sfmt=`checklist ${flags} -D"${sfmt}" -ef "Which bad track list do you want (default: ${sfmt})? " 1 2`
	case "${sfmt}" in
		'1') MANFLAG='no'
		     ROPT=-r ;;
		'2') MANFLAG='yes' 
		     ROPT=-R ;;
	esac
}

get320inpfmt() {
	echo '
	Valid INPUT formats are:
	1. <Track>
	2. <Head> <Cylinder>
	3. <Log Device> <Block>\n'
	ifmt=`checklist ${flags} -H"
You may enter track numbers in any of the valid forms.  The numbers will be
converted, if necessary, to the format of the badtrack file before being
entered.

	Valid INPUT formats are:
	1. <Track>
	2. <Head> <Cylinder>
	3. <Log Device> <Block>

" -D"${idef}" -ef "In what format will you enter the data (default: ${idef})? " 1 2 3`
	case "${ifmt}" in
		'1') IFMT=t ;;
		'2') IFMT=c ;;
		'3') IFMT=d ;;
	esac
}

get320prfmt() {
	echo '
	Valid PRINT formats are:
	1. <Track>
	2. <Head> <Cylinder> \n'
	pfmt=`checklist ${flags} -H"
You may view the file in either format.  The original file will NOT be 
changed, regardless of the format you choose.

	Valid PRINT formats are:
	1. <Track>
	2. <Head> <Cylinder>

" -D"${pdef}" -ef "Display file in which format (default: ${pdef})? " 1 2 `
	case "${pfmt}" in
		'1') PFMT=t ;;
		'2') PFMT=c ;;
	esac
}

echo '\nAnytime you want to quit, type "q".
If you are not sure how to answer any prompt, type "?" for help,
or see the Administrator\047s Guide.
\nIf a default appears in the question, press <RETURN> for the default.
'
while true
do
	getdevice
	case $device in
	    m323_[01] | m323_1d[01])
		get323source
		;;
	    *)
		MANFLAG=NA
		ROPT=-r
		;;
	esac
	echo "\nThis is the information for the bad track update:\n
	entered device:	${device}
	manufacturer:	${pr_man}
	disk size:    	${pr_sz}
	device name:	${FMTDEV}
	device type:	${DTYPE}
	use mfr list:	${MANFLAG}\n"
	case `checklist ${flags} -fep 'Do you want to begin the update, or change this data [b, c, q]?' begin change ` in
	begin )
		list=`${FMT} ${ROPT} ${DTYPE} ${FMTDEV} 2>/dev/null` 
		if [ $? != 0 ]
		then
			echo "\nCan't read disk bad track list."
			exit 1
		fi
			echo "${list}" > ${TFILE}
		# make sure there are no slashes in the file
		sed -e "s/\// /g" ${TFILE} > /tmp/$$_sed
		mv /tmp/$$_sed ${TFILE}
		echo "\nThe file '${TFILE}' has been updated.\n"
		break
		;;
	change)
		;;
	esac
done

while true
do
	case `checklist ${flags} -H"
If you want to add any other badtrack entries, do it here.  If you are 
formatting a 320 device, you will be able to enter the data in track or
head/cylinder format, regardless of the format of the file.  The data will
be stored in head, cylinder format but you can view the file in any other 
format to check your entries.

If you are formatting something other than a 320 device, you must enter
the badspots in Head, Cylinder, BytesFromIndex form.  If you do not know
the bytes from index for an entry, enter -1.

If you wish to neither add nor view, just select 'continue'.

" -fep 'Do you want to view the bad track list, add to the list 
or continue [v, a, c, q]?' view add continue ` in
	view )
	   case $device in
	   m320_[01] | m320_1d[01])
	    	get320prfmt			# assume dinit prints it hd,cyl
	    if [ "${PFMT}" = 'c' ]
	    then
	    		${PRBAD} ${PFMT} < ${TFILE}
	    else
	    	${CONV} -I c -O ${PFMT} -t ${TFILE} ${DTYPE} ${device} 2>/dev/null
	    		${PRBAD} ${PFMT} < /tmp/xf_${device} 
	    	rm -f /tmp/xf_${device}
	    fi
	    ;;

           m360_[02] | m360_1d[02] | m323_[01] | m323_1d[01])
		PFMT=b
	    	${PRBAD} ${PFMT} < ${TFILE}
		;;
	   *)
		;;
	   esac
	    ;;
	add )
	    case $device in
	    m320_[01] | m320_1d[01])
	    	get320inpfmt
	    ${CONV} -I ${IFMT} -O c ${DTYPE} ${device} 2>/dev/null
	    if [ $? != 0 ]
	    then
			echo "\nUnable to get new input for bad track file. "
		echo "The file ${TFILE} remains unchanged.\n"
	    	rm -f /tmp/xf_${device}
	    else
	    	cat /tmp/xf_${device} >> ${TFILE} 2>/dev/null
	    	rm -f /tmp/xf_${device}
  	    		echo "\n\tThe new bad tracks have been appended"
	    		echo "\tto ${TFILE}.\n"
	    fi
	    ;;

	    * )
		addnewbad
		;;
	esac
	    ;;   # end case 'add'
	* )
	    break
	    ;;
	esac
done
