File handle.linux of Package ncurses.9557

#!/bin/bash

tmp1=$(mktemp ${TMPDIR:-/tmp}/${0##*/}.XXXXXX) || exit 1
trap "rm -f $tmp1; exit" EXIT SIGPIPE SIGTERM SIGINT
tmp2=$(mktemp ${TMPDIR:-/tmp}/${0##*/}.XXXXXX) || exit 1
trap "rm -f $tmp1 $tmp2; exit" EXIT SIGPIPE SIGTERM SIGINT

: ${TERMCAP:=/etc/termcap}
: ${TERMINFO:=/usr/share/terminfo}
: ${BUILD_TIC:=/usr/bin/tic}
: ${BUILD_INFOCMP:=/usr/bin/infocmp}
acsc=
sgr=
mpch=
new=
ed=
declare -i line=0

if test -r run_cmd.sh ; then
    function tc { sh run_cmd.sh $BUILD_TIC -U -C -r ${1+"$@"}; }
    function ic { sh run_cmd.sh $BUILD_INFOCMP -A $TERMINFO ${1+"$@"}; }
else
    function tc { $BUILD_TIC -U -C -r ${1+"$@"}; }
    function ic { $BUILD_INFOCMP -A $TERMINFO ${1+"$@"}; }
fi

cp $TERMCAP ${TERMCAP##*/}.new
set -o noglob
OIFS="$IFS"
IFS='
'
for l in $(grep '^linux' $TERMCAP); do
    [[ $l =~ linux-vt* ]] && continue
    [[ $l =~ linux-basic* ]] && continue
    ic -1 -T ${l%%|*} > $tmp2

    grep -E '^linux|acsc=' < $tmp2 > $tmp1
    acsc="$(tc $tmp1 | grep -v '^linux')"

    grep -E '^linux|sgr=' < $tmp2 > $tmp1
    sgr="$(tc $tmp1 | grep -v '^linux')"

    grep -E '^linux|(smpch|rmpch)=' < $tmp2 > $tmp1
    mpch="$(tc $tmp1 | grep -v '^linux')"

    entry="$(tc $tmp2 | grep -v '^#')"

    new=$(echo "$entry" | sed -e '/:a[el]=.*/a\
	:ac=' | sed -e "s/\t:ac=/${acsc//\\/\\\\}\\\\/")
    new=$(echo "$new" | sed -e '/:a[el]=.*/i\
	:S2=' | sed -e "s/\t:S2=/${mpch//\\/\\\\}\\\\/")
    new=$(echo "$new" | sed -e '/:s.=.*/i\
	:\.\.sa=' | sed -e "s/\t:\.\.sa=/${sgr//\\/\\\\}\\\\/")

    if test "${#new}" -gt 1024 ; then
	new=$(echo "$entry" | sed -e '/\t:a[el]=.*/a\
	:ac=' | sed -e "s/\t:ac=/${acsc//\\/\\\\}\\\\/")
	new=$(echo "$new" | sed -e '/\t:a[el]=.*/i\
	:S2=' | sed -e "s/\t:S2=/${mpch//\\/\\\\}\\\\/")
    fi

    if test "${#new}" -gt 1024 ; then
	new=$(echo "$entry" | sed -e '/\t:a[el]=.*/i\
	:S2=' | sed -e "s/\t:S2=/${mpch//\\/\\\\}\\\\/")
    fi

    if test "${#new}" -gt 1024 ; then
	new="$entry"
    fi

    line=$(grep -n "${l//\\/\\\\}" ${TERMCAP##*/}.new| sed 's/:.*//')
    : $((line--))
    echo "$entry" > $tmp1
    echo "$new"   > $tmp2

    ed=$(diff -e $tmp1 $tmp2| sed "s/^\([0-9]\+a\)/$line\n+\1/")

    ed ${TERMCAP##*/}.new &> /dev/null <<-EOF
	${ed}
	w
	q
	EOF
done
IFS="$OIFS"
unset entry acsc sgr mpch