echo 
echo "Setting up the ti files ..."
echo 
#ident	"@(#)pkg.terminf:terminf/setup	1.9"
#
#	Script for installation of terminfo entries
#
cd /install/install

rm -rf /usr/tmp/all.ti
COMPILE="tic -v "
PERMLOC=/usr/lib/terminfo
TIFILES=`ls -aFx *.ti`;
TMPLOC=/usr/tmp/terminf.wrk

#
#	This routine locates a terminal name within a terminfo file
#
locate_ent()
{
	#
	#	This routine searches for the terminal name within
	#	the ti files, if found the file's name is displayed.
	#
	disp_msg()
	{
		FILE=`egrep -l "^${TERMNAME}\||^[^#].*\|${TERMNAME}\|" *.ti`
		if [ -n "${FILE}" ]
			then echo "Terminal ${TERMNAME} is located within terminfo file '${FILE}'"
			else echo Terminal ${TERMNAME} not found.
		fi
	}

	#
	#	Prompt user for terminal name, reject a NULL reply
	#
	echo
	echo "Enter terminal name to be located: \c"
	read TERMNAME
	if [ -n "${TERMNAME}" ]
		then disp_msg
		else echo Null reply ignored!
	fi
}
#
#	Search for the given terminal name - If found compile it
#	into /usr/tmp and then move the SINGLE terminal entry to
#	${PERMLOC}. Perform all necessary links.
#
single_file()
{
	#
	#	This routine performs the actual build, moves
	#	and links.
	#
	tmpbuild()
	{
		#
		#	Clean up from prior run
		#
		rm -rf ${TMPLOC}
		mkdir ${TMPLOC}

		#
		#	Compile entire file into a temporary location
		#
		TERMINFO=${TMPLOC} tic ${FILE}

		#
		#	Locate the subdirectory of the requested device
		#
		PREFIX=`expr ${TERMNAME} : '\(.\)' \| ${TERMNAME}`

		#
		#	To perform links we get the inode of the actual
		#	file created by tic, then search for all files
		#	in the ${TMPLOC} directory with the same inode.
		#	Those files with the same inode are aliases for
		#	The original entry.
		#
		CURDIR=`pwd`
		cd ${TMPLOC}
		INODE=`echo \`ls -i ${PREFIX}/${TERMNAME}\` | cut -f 1 -d ' '`
		echo Working in ${PERMLOC}

		#
		#	Ensure ${PERMLOC} directory exists
		#
		if [ ! -d "${PERMLOC}" ]
			then mkdir ${PERMLOC}
			chmod 775 ${PERMLOC}
			chown bin ${PERMLOC}
			chgrp bin ${PERMLOC}
		fi

		#
		#	Ensure directory exists for upcoming copy
		#
		if [ ! -d "${PERMLOC}/${PREFIX}" ]
			then mkdir ${PERMLOC}/${PREFIX}
		fi

		cp ${TMPLOC}/${PREFIX}/${TERMNAME} ${PERMLOC}/${PREFIX}
		echo Created ${PREFIX}/${TERMNAME}
		ls -i */* | grep ${INODE} | cut -c7- | while read LINKTO
		do
			PARTIAL=${PREFIX}/${TERMNAME}
			DEST=${PERMLOC}/${LINKTO}
			#
			#	Locate the subdirectory of the requested device
			#
			LNKPRE=`expr ${LINKTO} : '\(.\)' \| ${LINKTO}`

			#
			#	Ensure LNKPRE dir exists
			#
			if [ ! -d "${PERMLOC}/${LNKPRE}" ]
				then mkdir "${PERMLOC}/${LNKPRE}"
			fi

			if [ "${PARTIAL}" != "${LINKTO}" ]
				then ln ${PERMLOC}/${PARTIAL} ${DEST}
				echo Linked ${LINKTO}
			fi
		done
		cd ${CURDIR}
		rm -rf ${TMPLOC}
	}

	#
	#	This routine determines the file which contains the
	#	terminal desired. If found it is compiled via tmpbuild()
	#
	looktic()
	{
		FILE=`egrep -l "^${TERMNAME}\||^[^#].*\|${TERMNAME}\|" *.ti`
		if [ -n "${FILE}" ]
			then tmpbuild
			else echo Terminal ${TERMNAME} not found.
		fi
	}

	#
	#	Prompt user for terminal name, reject NULL reply
	#
	echo
	echo "Enter terminal name: \c"
	read TERMNAME
	if [ -n "${TERMNAME}" ]
		then looktic
		else echo Null reply ignored!
	fi
}
#
#	List all eligible files, and allow building of any one file
#
tic_file()
{
	#
	#	Display list of available files
	#
	list_files()
	{
		echo
		echo "The following terminfo files may be selected for installation:"
		echo
		ls -aFx *.ti
	}

	#
	#	The user's file must be in our list
	#
	valid_file()
	{
		echo /usr/tmp/all.ti ${TIFILES} | grep ${FILE} > /dev/null
		return $?
	}

	#
	#	Ensure only one file is specified
	#
	multiple()
	{
		echo ${FILE} | egrep " |	" > /dev/null
		if [ $? -eq 0 ] 
			then echo "Error: Only one file may be specified!"
				return 0
			else return 1
		fi
	}

	#
	#	Display file names, and loop over the user's replies
	#
	list_files
	while :;
	do
		rm -rf /usr/tmp/all.ti
		echo
		echo "Enter a file name, 'all', 'done', or 'files': \c"
		read FILE
		if [ -n "${FILE}" ]
			then if [ "${FILE}" = "all" ]
				then rm -rf /usr/tmp/all.ti
					cat *.ti > /usr/tmp/all.ti
					FILE=/usr/tmp/all.ti
			fi
			if multiple 
				then :;
				#
				#	If the user's reply is not NULL
				#	continue parseing
				#
				elif [ "${FILE}" = "done" ] 
						then return
		
					elif [ "${FILE}" = "files" ]
						then list_files
		
					#
					#	If user gave us a file name,
					#	verify it as a .ti file, and 
					#	compile it
					#
					elif [ -f "${FILE}" ] 
						then if valid_file
							then ${COMPILE} ${FILE}
							else echo Error: ${FILE} is not in the list!
						fi
		
						else echo Error: ${FILE} is not a file!
					fi
		else echo Null reply ignored!
		fi
	done
}

#
#	Main loop of the script, handles main menu
#
echo "Linking terminfo files"
echo
export PATH
PATH=/bin:/usr/bin:/etc
ROOT=/
cd ${ROOT}/usr/lib/terminfo
mkdir p
ln 1/1620 1/1640
ln 1/1620 4/450
ln 1/1620 d/diablo
ln 2/2621 2/2621-wl
ln 2/2621 2/2621A
ln 2/2621 2/2621a
ln 2/2621 h/hp2621
ln 2/2621 h/hp2621-wl
ln 2/2621 h/hp2621A
ln 2/2621 h/hp2621a
ln 2/2622 2/2622a
ln 2/2622 h/hp2622
ln 2/2622 h/hp2622a
ln 2/2623 2/2623a
ln 2/2623 h/hp2623
ln 2/2623 h/hp2623a
ln 2/2624 2/2624-4p
ln 2/2624 2/2624a
ln 2/2624 2/2624b
ln 2/2624 h/hp2624
ln 2/2624 h/hp2624a
ln 2/2624 h/hp2624b
ln 2/2624 h/hp2624b-4p
ln 2/2626 2/2626A
ln 2/2626 2/2626P
ln 2/2626 2/2626a
ln 2/2626 2/2626p
ln 2/2626 h/hp2626
ln 2/2626 h/hp2626a
ln 2/2626 h/hp2626p
ln 2/2640 2/2640a
ln 2/2640 h/hp2640a
ln 2/2640b 2/2644a
ln 2/2640b h/hp2640b
ln 2/2640b h/hp2644a
ln 2/2645 h/hp2645
ln 2/2645 h/hp45
ln 2/2648 2/2648A
ln 2/2648 2/2648a
ln 2/2648 h/hp2648
ln 2/2648 h/hp2648a
ln 3/31 a/adm31
ln 3/36 a/adm36
ln 3/3a a/adm3a
ln 4/4012 t/tek
ln 4/4012 t/tek4012
ln 4/4013 t/tek4013
ln 4/4014 t/tek4014
ln 4/4015 t/tek4015
ln 4/4023 t/tek4023
ln 4/4023 t/tex
ln 4/4024 4/4025
ln 4/4024 4/4025cu
ln 4/4024 4/4027
ln 4/4024 4/4027cu
ln 4/4024 t/tek4024
ln 4/4024 t/tek4025
ln 4/4024 t/tek4027
ln 4/4112 4/4112-nd
ln 4/4112 4/4113
ln 4/4112 4/4114
ln 4/4112 t/tek4112
ln 4/42 a/adm42
ln 4/4410 5/5410
ln 4/4410 A/ATT4410
ln 4/4410 A/ATT5410
ln 4/4410 a/att4410
ln 4/4410 a/att5410
ln 4/4410 t/tty5410
ln 4/4415 5/5420
ln 4/4415 A/ATT4415
ln 4/4415 A/ATT5420
ln 4/4415 a/att4415
ln 4/4415 a/att5420
ln 4/4415 t/tty5420
ln 4/4418 5/5418
ln 4/4418 A/ATT4418
ln 4/4418 A/ATT5418
ln 4/4418 a/att4418
ln 4/4418 a/att5418
ln 4/4424 4/4424-1
ln 4/4424 4/4424-3
ln 4/4424 A/ATT4424
ln 4/4424 a/att4424
ln 4/4424 t/tty4424
ln 4/4424-2 A/ATT4424-2
ln 4/4424-2 a/att4424-2
ln 4/4424-2 t/tty4424-2
ln 4/4425 5/5425
ln 4/4425 A/ATT4425
ln 4/4425 A/ATT5425
ln 4/4425 a/att4425
ln 4/4425 a/att5425
ln 4/4425 t/tty5425
ln 4/4426 A/ATT4426
ln 4/4426 a/att4426
ln 4/4426 t/tty4426
ln 5/513bct A/ATT513
ln 5/513bct a/att513
ln 5/5420_2 A/ATT5420_2
ln 5/5420_2 a/att5420_2
ln 5/5620 A/ATT5620
ln 5/5620 a/att5620
ln 5/5620 d/dmd
ln 5/5620 t/tty5620
ln 5/5620 t/ttydmd
ln 6/610bct A/ATT610BCT
ln 6/610bct a/att610bct
ln 9/925 t/tvi925
ln 9/950 t/televideo950
ln 9/950 t/tvi950
ln A/ATTPT505 a/attpt505
ln A/ATTPT505 p/pt505
ln b/bantam p/pe550
ln b/bantam p/pe6100
ln c/c100 c/c100-4p
ln c/c100 c/c104
ln c/c100 c/concept
ln c/c100 c/concept100
ln c/c108 c/c108-8p
ln c/c108 c/concept108
ln c/c108 c/concept108-8p
ln d/decwriter d/dw
ln d/decwriter d/dw2
ln d/dw3 l/la120
ln i/intext2 i/intextii
ln l/lp l/lpr
ln l/lp p/print
ln l/lp p/printer
ln l/lp p/printing
ln r/regent200 r/regent60
ln v/vt100 v/vt100-am
cd /
ln /usr/lib/libcurses.a /usr/lib/libtermcap.a
ln /usr/lib/libcurses.a /usr/lib/libtermlib.a
#
cd /install/install
while :;
	do
		echo
		echo "       0       Terminate installation"
		echo
		echo "       1       Install terminfo file(s)"
		echo
		echo "       2       Locate a specific terminal within terminfo file(s)"
		echo
		echo "       3       Compile a SINGLE terminal entry"
		echo
		echo "Enter option: \c"
		read OPTION
		if [ -n "${OPTION}" ]
			then
			case ${OPTION} in
				0)	exit;;
				1)	tic_file;;
				2)	locate_ent;;
				3)	single_file;;
				*)	echo "Invalid option";;
			esac
			else echo "Null reply ignored!"
		fi
	done
