# Build recipe for xterm.
#
# Copyright (c) 2017-2019, 2021-2022 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xterm
version=370
release=1

# Define a category for the output of the package name
pkgcategory=x-apps

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=https://invisible-mirror.net/archives/xterm/$tarname

description="
Terminal emulator for X.

The xterm program is a terminal emulator for the X Window System.
It provides DEC VT102 and Tektronix 4014 compatible terminals for
programs that can't use the window system directly.
"

homepage=https://invisible-island.net/xterm
license=Custom

# Source documentation
docs="NEWS README README.i18n THANKS"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Patch taken from Alpine Linux (Thanks!)
    # https://git.alpinelinux.org/cgit/aports/tree/community/xterm/posix-ptys.patch
    patch -p1 < "${worktree}/patches/xterm/posix-ptys.patch"

    # These commands modify the terminal description so that the
    # Backspace key is expected to send the character with ASCII
    # code 127.  This is done for consistency with the Linux console.
    # Thanks to Beyond Linux From Scratch!
    sed -i '/v0/{n;s/new:/new:kb=^?:/}' termcap && \
     printf '\tkbs=\\177,\n' >> terminfo

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
    TERMINFO=/usr/share/terminfo \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --enable-exec-xterm \
     --enable-256-color \
     --enable-88-color \
     --enable-luit \
     --enable-wide-chars \
     --enable-dabbrev \
     --enable-setgid \
     --enable-imake \
     --disable-setuid \
     --disable-full-tgetent \
     --with-pcre2 \
     --with-app-defaults=/etc/X11/app-defaults \
     --with-tty-group=tty \
     --with-x \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Conflicts with files provided by ncurses
    #make -j${jobs} DESTDIR="$destdir" install-ti

    # Append extra information.
    #
    # In order for xterm to follow the locale settings in the
    # environment, use TrueType fonts, and follow the GNU/Linux
    # convention about the code sent by the Backspace key, we add
    # the following definitions
    cat << EOF >> "${destdir}/etc/X11/app-defaults/XTerm"
*VT100*locale: true
*VT100*faceName: Monospace
*VT100*faceSize: 14
*backarrowKeyIsErase: true
*ptyInitialErase: true
EOF

    # To handle config files via graft(1)
    touch "${destdir}/etc/X11/app-defaults/.graft-config"

    # Include desktop entries
    mkdir -p "${destdir}/usr/share/applications"
    cp -p xterm.desktop uxterm.desktop \
          "${destdir}/usr/share/applications/"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

