# Maintainer: Jose Riha <jose1711 gmail com>
# Maintainer: Sebastian J. Bronner <waschtl@sbronner.com>
# Maintainer: Kevin Stolp <kevinstolp@gmail.com>
# Contributor: Patrick Jackson <PatrickSJackson gmail com>
# Contributor: Christoph Vigano <mail@cvigano.de>

pkgname=st
pkgver=0.9
pkgrel=1
pkgdesc='A simple virtual terminal emulator for X.'
arch=('i686' 'x86_64' 'armv7h' 'aarch64')
license=('MIT')
depends=(libxft)
url=https://st.suckless.org
source=(https://dl.suckless.org/$pkgname/$pkgname-$pkgver.tar.gz
        terminfo.patch
        README.terminfo.rst
        https://st.suckless.org/patches/alpha/st-alpha-20220206-0.8.5.diff
        https://st.suckless.org/patches/xresources/st-xresources-20200604-9ba7ecf.diff
        https://st.suckless.org/patches/delkey/st-delkey-20201112-4ef0cbd.diff
        st-desktopentry-0.8.4-edit.diff
        https://st.suckless.org/patches/scrollback/st-scrollback-20210507-4536f46.diff
        https://st.suckless.org/patches/scrollback/st-scrollback-mouse-20220127-2c5edf2.diff
        https://st.suckless.org/patches/scrollback/st-scrollback-mouse-altscreen-20220127-2c5edf2.diff
        st-scrollback-mouse-increment-0.8.2-fixed.diff
        )
sha256sums=('f36359799734eae785becb374063f0be833cf22f88b4f169cd251b99324e08e7'
            'f9deea445a5c6203a0e8e699f3c3b55e27275f17fb408562c4dd5d649edeea23'
            '0ebcbba881832adf9c98ce9fe7667c851d3cc3345077cb8ebe32702698665be2'
            '42e4803ce2a67835f7e533a707a8a28e3804a26ced163145108970b9aee5fb81'
            '5be9b40d2b51761685f6503e92028a7858cc6571a8867b88612fce8a70514d5b'
            '946051d123dfe21d8f5ef0f1070c473443f4779dc0bd7edf7c8497f67e325a49'
            '51d22293b6a2c19471f31160bfebf832dfe2a2768e226520d46c3321d7592649'
            '19d8f4e7fd0d1933dc6fcf6c7333db08e1b40fc75795464660c4d723eb62511c'
            '46ac9bcdbfeb0011533207cb0ab31657a3eb9196da1d0db346e6a9d1fc4b4f76'
            '8f2f17683f12d57b1c80461247fe15234f5f5a6fc52cdf48176c8358e699101d'
            '3f5dc269614b930b738b78ad9f8f4538baede1e0ab7d16e9f5d8a590a25ef52d')
_sourcedir=$pkgname-$pkgver
_makeopts="--directory=$_sourcedir"

prepare() {
  patch --directory="$_sourcedir" --strip=0 < terminfo.patch
  for file in "${source[@]}"; do
    if [[ "$file" == *.diff ]]; then
      # Apply patches
      #echo "Applying patch: $(basename ${file})"
      patch --directory="$_sourcedir" <"$srcdir/$(basename ${file})"
    fi
  done
  sed 's/Liberation Mono/Hack/;s/float alpha = 0.8;/float alpha = 0.85;/' "$_sourcedir/config.def.h" > "$_sourcedir/config.h"

  # This package provides a mechanism to provide a custom config.h. Multiple
  # configuration states are determined by the presence of two files in
  # $BUILDDIR:
  #
  # config.h  config.def.h  state
  # ========  ============  =====
  # absent    absent        Initial state. The user receives a message on how
  #                         to configure this package.
  # absent    present       The user was previously made aware of the
  #                         configuration options and has not made any
  #                         configuration changes. The package is built using
  #                         default values.
  # present                 The user has supplied his or her configuration. The
  #                         file will be copied to $srcdir and used during
  #                         build.
  #
  # After this test, config.def.h is copied from $srcdir to $BUILDDIR to
  # provide an up to date template for the user.
  if [ -e "$BUILDDIR/config.h" ]
  then
    cp "$BUILDDIR/config.h" "$_sourcedir"
  elif [ ! -e "$BUILDDIR/config.def.h" ]
  then
    msg='This package can be configured in config.h. Copy the config.def.h '
    msg+='that was just placed into the package directory to config.h and '
    msg+='modify it to change the configuration. Or just leave it alone to '
    msg+='continue to use default values.'
    echo "$msg"
  fi
  cp "$_sourcedir/config.def.h" "$BUILDDIR"
}

build() {
  make $_makeopts X11INC=/usr/include/X11 X11LIB=/usr/lib/X11
}

package() {
  local installopts='--mode 0644 -D --target-directory'
  local shrdir="$pkgdir/usr/share"
  local licdir="$shrdir/licenses/$pkgname"
  local docdir="$shrdir/doc/$pkgname"
  make $_makeopts PREFIX=/usr DESTDIR="$pkgdir" install
  install $installopts "$licdir" "$_sourcedir/LICENSE"
  install $installopts "$docdir" "$_sourcedir/README"
  install $installopts "$docdir" README.terminfo.rst
  install $installopts "$shrdir/$pkgname" "$_sourcedir/st.info"
}
