#!/bin/sh

# set -xv

#
# install.vt320-k
#
# automated installation of the vt320-k terminal capabilities information
# for both /etc/termcap and terminfo.
#

# usage:  install.vt320-k [view] [terminfo] [termcap]

# initialize local variables

TERMCAP=0
TERMINFO=0

VIEW=0

# parse command-line, get info, report errors/typos

while [ $# -gt 0 ]
do
    case "$1" in
        "terminfo" )  TERMINFO=1 ;;
	"termcap" )   TERMCAP=2 ;;
	"view" )      VIEW=1 ;;
    esac
    shift
done

ACTION=`expr $TERMINFO + $TERMCAP`

if [ $VIEW -eq 1 ]
then

    if [ $ACTION -eq 0 ]
    then
	echo "You must specify which file you want to view"
	exit 1
    fi

    if [ $ACTION -eq 3 ]
    then
	echo "You can only view one file at a time"
	exit 1
    fi

fi

if [ $ACTION -eq 0 ]
then
    TERMINFO=1
    TERMCAP=2
    ACTION=`expr $TERMINFO + $TERMCAP`
fi

if [ $VIEW -eq 0 ]
then
    eff_uid="`id | sed 's/^uid=\([0-9]*\)(.*/\1/g'`"
    if [ "$eff_uid" != "0" ]
    then
        echo "*** ERROR: this program should be run only by user 'root'"
        echo "           Login or su to root and run this program again."
        exit 2
    fi

    if [ ! -w . ]
    then
        echo "*** ERROR current working directory is non-writeable"
        exit 1
    fi
    if [ $TERMCAP -gt 0 -a -f /etc/termcap ]
    then
        echo "Building termcap.vt320-k"
        awk '/^# Start of TermCap Entry/,/^# End of TermCap Entry/' $0 | \
        sed '1d
	     $d' > termcap.vt320-k
        echo "Installing termcap.vt320-k"
        if grep -q vt320-k /etc/termcap
        then
            echo "Stripping existing termcap.vt320-k from /etc/termcap"
            sed '/^..\|vt320-k/,/[^\\]$/d' /etc/termcap > tmp.termcap
        else
            cp /etc/termcap tmp.termcap
        fi

        if [ -w /etc/termcap ]
        then
            cat tmp.termcap termcap.vt320-k > /etc/termcap
        else
            echo "*** ERROR unable to install to /etc/termcap"
        fi

        rm -f tmp.termcap termcap.vt320-k
    fi

    if [ $ACTION -eq 3 ]
    then
        echo ""
    fi

    if [ $TERMINFO -gt 0 ]
    then
        if [ "`uname -sr | awk '/^HP-UX .*9\.00$/'`" ]
        then
            echo "Building terminfo.vt320-k for HP-UX 9"
            awk '/^# Start of HP-UX Version 9 TermInfo Entry/,\
	    /^# End of HP-UX Version 9 TermInfo Entry/' $0 | sed '1d
            $d' > terminfo.vt320-k
        else
            echo "Building terminfo.vt320-k"
            awk '/^# Start of TermInfo Entry/,\
            /^# End of TermInfo Entry/' $0 | \
            sed '1d
            $d' > terminfo.vt320-k
        fi
        echo "Installing terminfo.vt320-k"
        tic terminfo.vt320-k 2> /dev/null
        rm -f terminfo.vt320-k
    fi
else
    if [ $ACTION -eq $TERMCAP ]
    then
        awk '/^# Start of TermCap Entry/,/^# End of TermCap Entry/' $0 | \
        sed '1d
	     $d'
    else
        if [ "`uname -sr | awk '/^HP-UX .*9\.00$/'`" ]
        then
            awk '/^# Start of HP-UX Version 9 TermInfo Entry/,\
	    /^# End of HP-UX Version 9 TermInfo Entry/' $0 | sed '1d
            $d'
        else
            awk '/^# Start of TermInfo Entry/,\
            /^# End of TermInfo Entry/' $0 | \
            sed '1d
            $d'
        fi
    fi
fi

exit 0

# Imbedded Data ############################################################

# Start of TermCap Entry for vt320-k
3k|vt320-k|Digital Equipment Co VT220/320/330 Enhanced:\
	:ms:eo:km:it#8:li#24:co#80:\
	:hs:es:ts=\E7\E[1$}\E[2K\E[0;1H:fs=\E[0$}\E8:\
	:cr=^M:cd=\E[J:ce=\E[K:bl=^G:\
	:sf=^J:al=\E[L:dl=\E[M:cl=\E[H\E[2J:\
	:cm=\E[%i%d;%dH:ec=\E[%dX:\
	:up=\E[A:UP=\E[%dA:ku=\E[A:\
	:do=\E[B:DO=\E[%dB:kd=\E[B:\
	:nd=\E[C:RI=\E[%dC:kr=\E[C:\
	:le=^H:LE=\E[%dD:kl=\E[D:\
	:ti=\E[?7l\E[?1l\E(B:\
	:mb=\E[5m:md=\E[1m:mr=\E[7m:me=\E[0m\E(B:\
	:so=\E[7m:se=\E[0m:us=\E[4m:ue=\E[0m:\
	:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:\
	:k6=\E[17~:k7=\E[18~:k8=\E[19~:k9=\E[20~:\
	:k;=\E[21~:F1=\E[23~:F2=\E[24~:F3=\E[25~:\
	:F4=\E[26~:F7=\E[31~:F8=\E[32~:F9=\E[33~:\
	:FA=\E[34~:kb=^H:kD=\177:F6=\E[29~:\
	:kI=\E[2~:kN=\E[6~:\
	:kP=\E[5~:F5=\E[28~:vi=\E[?25l:ve=\E[?25h:\
	:vs=\E[?25h:ps=\E[0i:po=\E[5i:pf=\E[4i:\
	:vb=\E[?5h\200\200\200\200\200\200\E[?5l\200\200\200\200\200\200:\
	:ko=do,nd,up:
# End of TermCap Entry for vt320-k

# Start of TermInfo Entry for vt320-k
vt320-k|Digital Equipment Co VT220/320/330,
	batt1=f1,
	batt2=f1md,
	bel=^G,
	blink=\E[5m,
	bold=\E[1m,
	box1=\154\161\153\170\152\155\167\165\166\164\156,
	box2=\154\161\153\170\152\155\167\165\166\164\156,
	civis=\E[?25l,
	clear=\E[H\E[2J,
	cnorm=\E[?25h,
	cols#80,
	cr=^M,
	cub1=^H,
	cub=\E[%p1%dD,
	cud1=\E[B,
	cud=\E[%p1%dB,
	cuf1=\E[C,
	cuf=\E[%p1%dC,
	cup=\E[%i%p1%d;%p2%dH,
	cuu1=\E[A,
	cuu=\E[%p1%dA,
	cvvis=\E[?25h,
	dl1=\E[M,
	ech=\E[%p1%dX,
	ed=\E[J,
	el=\E[K,
	eo,
	eslok,
	font0=\E(B,
	font1=\E(0,
	fsl=\E[0$}\E8,
	hs,
	il1=\E[L,
	ind=^J,
	it#8,
	kbs=^H,
	kcub1=\E[D,
	kcud1=\E[B,
	kcuf1=\E[C,
	kcuu1=\E[A,
	kdch1=\177,
	kdo=\E[29~,
	kf10=\E[21~,
	kf11=\E[23~,
	kf12=\E[24~,
	kf13=\E[25~,
	kf14=\E[26~,
	kf17=\E[31~,
	kf18=\E[32~,
	kf19=\E[33~,
	kf1=\EOP,
	kf20=\E[34~,
	kf2=\EOQ,
	kf3=\EOR,
	kf4=\EOS,
	kf6=\E[17~,
	kf7=\E[18~,
	kf8=\E[19~,
	kf9=\E[20~,
	khlp=\E[28~,
	kich1=\E[2~,
	km,
	knl=\r,
	knp=\E[6~,
	kpp=\E[5~,
	ktab=^I,
	lines#24,
	mc0=\E[0i,
	mc4=\E[4i,
	mc5=\E[5i,
	msgr,
	rev=\E[7m,
	rmcup=\E[?7h,
	rmso=\E[0m,
	rmul=\E[0m,
	sgr0=\E[0m\E(B,
	sgr=\E[%?%p1%t;7%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t;5%;%?%p6%t;1%;m%?%p9%t\E(0%e\E(B%;,
	smcup=\E[?7l\E[?1l\E(B,
	smso=\E[7m,
	smul=\E[4m,
	tsl=\E7\E[1$}\E[2K\E[0;1H,
# End of TermInfo Entry for vt320-k

# Start of HP-UX Version 9 TermInfo Entry for vt320-k
vt320-k|Digital Equipment Co VT220/320/330 Enhanced,
	am,
	bel=^G,
	blink=\E[5m,
	bold=\E[1m,
	civis=\E[?25l,
	clear=\E[H\E[2J,
	cnorm=\E[?25h,
	cols#80,
	cr=\r,
	csr=\E[%i%p1%d;%p2%dr,
	cub1=\b,
	cub=\E[%p1%dD,
	cud1=\n,
	cud=\E[%p1%dB,
	cuf1=\E[C,
	cuf=\E[%p1%dC,
	cup=\E[%i%p1%d;%p2%dH,
	cuu1=\E[A,
	cuu=\E[%p1%dA,
	cvvis=\E[?25h,
	dch1=\E[P,
	dch=\E[%p1%dP,
	dl1=\E[M,
	dl=\E[%p1%dM,
	ech=\E[%p1%dX,
	ed=\E[J,
	el=\E[K,
	eo,
	eslok,
	fsl=\E[0$}\E8,
	home=\E[H,
	hs,
	hts=\EH,
	ht=\t,
	ich1=\E[@,
	ich=\E[%p1%d@,
	il1=\E[L,
	il=\E[%p1%dL,
	ind=\n,
	it#8,
	kbs=\b,
	kcub1=\E[D,
	kcud1=\E[B,
	k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