150276Speter#!/bin/sh
2166124Srafan##############################################################################
3166124Srafan# Copyright (c) 1998,2006 Free Software Foundation, Inc.                     #
4166124Srafan#                                                                            #
5166124Srafan# Permission is hereby granted, free of charge, to any person obtaining a    #
6166124Srafan# copy of this software and associated documentation files (the "Software"), #
7166124Srafan# to deal in the Software without restriction, including without limitation  #
8166124Srafan# the rights to use, copy, modify, merge, publish, distribute, distribute    #
9166124Srafan# with modifications, sublicense, and/or sell copies of the Software, and to #
10166124Srafan# permit persons to whom the Software is furnished to do so, subject to the  #
11166124Srafan# following conditions:                                                      #
12166124Srafan#                                                                            #
13166124Srafan# The above copyright notice and this permission notice shall be included in #
14166124Srafan# all copies or substantial portions of the Software.                        #
15166124Srafan#                                                                            #
16166124Srafan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
17166124Srafan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
18166124Srafan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
19166124Srafan# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
20166124Srafan# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
21166124Srafan# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
22166124Srafan# DEALINGS IN THE SOFTWARE.                                                  #
23166124Srafan#                                                                            #
24166124Srafan# Except as contained in this notice, the name(s) of the above copyright     #
25166124Srafan# holders shall not be used in advertising or otherwise to promote the sale, #
26166124Srafan# use or other dealings in this Software without prior written               #
27166124Srafan# authorization.                                                             #
28166124Srafan##############################################################################
29166124Srafan# $Id: capconvert,v 1.4 2006/04/22 21:46:17 tom Exp $
3050276Speter#
3150276Speter# capconvert -- automated conversion from termcap to terminfo
3250276Speter#
3350276Speter
3450276Speterecho "This script tries to automatically set you up so that your applications"
3550276Speterecho "that now use termcap can use terminfo and the ncurses library."
3650276Speterecho ""
3750276Speter
3850276Speter# Note, except for telling if we're running under xterm we don't use TERM at
3950276Speter# all.  This is because BSD users not infrequently have multiple termtypes
4050276Speter# selected by conditionals in tset -- unless they're xterm users, in which
4150276Speter# case they're on a workstation and probably don't.
4250276Speter
4350276Speter# Check to make sure TERMINFO is not already defined
4450276Speterif test -n "$TERMINFO"
4550276Speterthen
4650276Speter	echo "TERMINFO is already defined in your environment.  This means"
4750276Speter	echo "you already have a local terminfo tree, so you do not need any"
4850276Speter	echo "conversion."
4950276Speter	if test ! -d $TERMINFO ; then
5050276Speter		echo "Caution: TERMINFO does not point to a directory!"
5150276Speter	fi
5250276Speter	exit;
5350276Speterfi
5450276Speter
5550276Speter# Check to see if terminfo is present in one of the standard locations.
5650276Speterterminfo=no
5750276Speterfor p in $TERMINFO \
5850276Speter	/usr/lib/terminfo \
5950276Speter	/usr/share/lib/terminfo \
6050276Speter	/usr/share/terminfo \
6150276Speter	/usr/local/lib/terminfo \
6250276Speter	/usr/local/share/terminfo
6350276Speterdo
6450276Speter	if test -d $p ; then
6550276Speter		terminfo=yes
6650276Speter		break
6750276Speter	fi
6850276Speterdone
6950276Speter
7050276Speterif test $terminfo = yes
7150276Speterthen
7250276Speter	echo "Your system already has a system-wide terminfo tree."
7350276Speter	echo ""
7450276Speter	if test -z "$TERMCAP"
7550276Speter	then
7650276Speter		echo "You have no TERMCAP variable set, so we are done."
7750276Speter		# Assumes the terminfo master covers all canned terminal types
7850276Speter		exit;
7950276Speter	fi
8050276Speter	if test "$TERM" = "xterm"
8150276Speter	then
8250276Speter		echo "You are running xterm, which usually sets TERMCAP itself."
8350276Speter		echo "We can ignore this, because terminfo knows about xterm."
8450276Speter		echo "So you will just use the system-wide terminfo tree."
8550276Speter		exit;
8650276Speter	else
8750276Speter		echo "We will have to make a local one for you anyway, to capture the effect"
8850276Speter		echo "of your TERMCAP variable."
8950276Speter	fi
9050276Speterelse
9150276Speter	echo "No system-wide terminfo tree.  We will make you a local one."
9250276Speterfi
9350276Speterecho "";
9450276Speter
9550276Speter# Check if test -x works (it's not portable, but useful)
9650276SpeterOPT="-x"
9750276SpeterTMP=test$$; touch $TMP && chmod 755 $TMP
9850276Speterif test $OPT $TMP ; then
9950276Speter	chmod 644 $TMP
10050276Speter	test $OPT $TMP && OPT="-f"
10150276Speterelse
10250276Speter	OPT="-f"
10350276Speterfi
10450276Speterrm -f $TMP
10550276Speter
10650276Speter# First step -- go find tic
10750276SpeterTIC=
10850276SpeterIFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
10950276Speterfor x in $PATH .
11050276Speterdo
11150276Speter	if test $OPT $x/tic
11250276Speter	then 
11350276Speter		TIC=$x/tic
11450276Speter		break
11550276Speter	fi
11650276Speterdone
11750276SpeterIFS="$ac_save_ifs"
11850276Speter
11950276Speterif test -n "$TIC"
12050276Speterthen
12150276Speter	echo "I see tic at $TIC."
12250276Speter	case $TIC in # (vi
12350276Speter	./tic)
12450276Speter		if test $OPT ../misc/shlib ; then
12550276Speter			TIC="../misc/shlib $TIC"
12650276Speter		fi
12750276Speter		;;
12850276Speter	esac
12950276Speterelse
13050276Speter	echo "You do not have tic installed anywhere I can see, please fix that."
13150276Speter	exit;
13250276Speterfi
13350276Speterecho "";
13450276Speter
13550276Speter# We have tic.  Either there's no system terminfo tree or there is one but
13650276Speter# the user has a TERMCAP variable that may modify a stock description.
13750276Speter#
13850276Speter
13950276Speter# Make the user a terminfo directory
14050276Speterif test -d $HOME/.terminfo
14150276Speterthen
14250276Speter	echo "It appears you already have a private terminfo directory"
14350276Speter	echo "at $HOME/.terminfo; this seems odd, because TERMINFO"
14450276Speter	echo "is not defined.  I am not going to second-guess this -- if you" 
14550276Speter	echo "really want me to try auto-configuring for you, remove or"
14650276Speter	echo "rename $HOME/terminfo and run me again."
14750276Speter	exit;
14850276Speterelse
14950276Speter	echo "I am creating your private terminfo directory at $HOME/.terminfo"
15050276Speter	mkdir $HOME/.terminfo
15150276Speter	# Ensure that that's where tic's compilation results.
15250276Speter	# This isn't strictly necessary with a 1.9.7 or later tic.
15350276Speter	TERMINFO="$HOME/.terminfo"; export TERMINFO
15450276Speterfi
15550276Speterecho "";
15650276Speter
15750276Speter# Find a terminfo source to work from
15850276Speterif test -f ../misc/terminfo.src
15950276Speterthen
16050276Speter	echo "I see the terminfo master source is handy; I will use that."
16150276Speter	master=../misc/terminfo.src
16250276Speterelse
16350276Speter	# Ooops...looks like we're running from somewhere other than the
16450276Speter	# progs directory of an ncurses source tree.
16550276Speter	master=`find $HOME -name "*terminfo.src" -print`
16650276Speter	mcount=`echo $master | wc -l`
16750276Speter	case $mcount in
16850276Speter	0)
16950276Speter		echo "I can not find a terminfo source file anywhere under your home directory."
17050276Speter		echo "There should be a file called terminfo.src somewhere in your"
17150276Speter		echo "ncurses distribution; please put it in your home directotry"
17250276Speter		echo "and run me again (it does not have to live there permanently)."
17350276Speter		exit;
17450276Speter	;;
17550276Speter	1)
17650276Speter		echo "I see a file called $master."
17750276Speter		echo "I am going to assume this is the terminfo source included with"
17850276Speter		echo "the ncurses distribution.  If this assumption is wrong, please"
17950276Speter		echo "interrupt me now!  OK to continue?"
18050276Speter		read ans;
18150276Speter	;;
18250276Speter	2)
18350276Speter		echo "I see more than one possible terminfo source.  Here they are:"
18450276Speter		echo $master | sed "/^/s//	/";
18550276Speter		while :
18650276Speter		do
18750276Speter			echo "Please tell me which one to use:"
18850276Speter			read master;
18950276Speter			if test -f $master
19050276Speter			then
19150276Speter				break
19250276Speter			else
19350276Speter				echo "That file does not exist. Try again?";
19450276Speter			fi
19550276Speter		done
19650276Speter	;;
19750276Speter	esac
19850276Speterfi
19950276Speterecho "";
20050276Speter
20150276Speter# Now that we have a master, compile it into the local tree
20250276Speterecho "OK, now I will make your private terminfo tree.  This may take a bit..."
20350276Speter#
20450276Speter# Kluge alert: we compile terminfo.src in two pieces because a lot of machines
20550276Speter# with < 16MB RAM choke on tic's core-hog habits.
20650276Spetertrap "rm -f tsplit$$.*" 0 1 2 5 15
20750276Spetersed -n $master \
20850276Speter	-e '1,/SPLIT HERE/w 'tsplit$$.01 \
20950276Speter	-e '/SPLIT HERE/,$w 'tsplit$$.02 \
21050276Speter	2>/dev/null
21150276Speterfor x in tsplit$$.*; do eval $TIC $x; done
21250276Speterrm tsplit$$.*
21350276Spetertrap 0 1 2 5 15
21450276Speter#
21550276Speterecho "You now have a private tree under $HOME/.terminfo;"
21650276Speterecho "the ncurses library will automatically read from it,"
21750276Speterecho "and ncurses tic will automatically compile entries to it." 
21850276Speter
21950276Speter# We're done unless user has a .termcap file or equivalent named by TERMCAP
22050276Speterif test -z "$TERMCAP"
22150276Speterthen
22250276Speter	echo "You have no TERMCAP set, so we are done."
22350276Speterfi
22450276Speter
22550276Speter# OK, here comes the nasty case...user has a TERMCAP.  Instead of
22650276Speter# trying to follow all the convolutions of the relationship between
22750276Speter# TERM and TERMCAP (partly because it's too painful, and partly because
22850276Speter# we don't actually know what TERM will be nor even if it always has
22950276Speter# the same value for this user) we do the following three steps...
23050276Speter
23150276Speterif test -f $HOME/.termcap
23250276Speterthen
23350276Speter	echo 'I see you have a $HOME/.termcap file.  I will compile that.'
23450276Speter	eval $TIC $HOME/.termcap
23550276Speter	echo "Done."
23650276Speter	echo "Note that editing $HOME/.termcap will no longer change the data curses sees."
23750276Speterelif test -f "$TERMCAP"
23850276Speterthen 
23950276Speter	echo "Your TERMCAP names the file $TERMCAP.  I will compile that."
24050276Speter	eval $TIC $TERMCAP
24150276Speter	echo "Done."
24250276Speter	echo "Note that editing $TERMCAP will no longer change the data curses sees."
24350276Speterelse
24450276Speter	echo "Your TERMCAP value appears to be an entry in termcap format."
24550276Speter	echo "I will compile it."
24650276Speter	echo $TERMCAP >myterm$$
24750276Speter	eval $TIC myterm$$
24850276Speter	rm myterm$$
24950276Speter	echo "Done."
25050276Speter	echo "Note that editing TERMCAP will no longer change the data curses sees."
25150276Speterfi
25250276Speterecho "To do that, decompile the terminal decription you want with infocmp(1),"
25350276Speterecho "edit to taste, and recompile using tic(1)."
25450276Speter
25550276Speter# capconvert ends here
25650276Speter
257