# Maintainer: Andrew Sun <adsun701@gmail.com>

pkgname=nawk
pkgver=20250116
pkgrel=1
pkgdesc="The one, true implementation of AWK"
url="https://github.com/onetrueawk/awk"
license=('MIT')
arch=('i686' 'x86_64')
makedepends=('make' 'gcc' 'git' 'bison')
options=('!makeflags')
source=("git+https://github.com/onetrueawk/awk.git#tag=${pkgver}"
        'manpage-naming.patch')
sha256sums=('4458a33bb325e69d159a73e51a164dd81aa1217ec0c59577edc6d34130480367'
            'fa1bade363896033a50fbdadc8fa2f490f85a044d11afcd537f317067c70d3b5')

prepare() {
  cd awk
  # awk -> nawk
  patch -Np1 <"${srcdir}"/manpage-naming.patch

  # create license
  sed -n '/Copyright/,/THIS SOFTWARE\./p' README.md > LICENSE

  # fix build
  sed -i '/YACC = yacc -d -S/ s|^|#|' makefile
}

build() {
  cd awk
  make "CPPFLAGS=-DHAS_ISBLANK ${CPPFLAGS}" \
    "LDFLAGS=${LDFLAGS}" \
    "CFLAGS=${CFLAGS}"
}

package() {
  cd awk
  install -Dm755 a.exe "${pkgdir}/usr/bin/nawk.exe"
  install -Dm644 awk.1 "${pkgdir}/usr/share/man/man1/nawk.1"
  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/nawk/LICENSE"
}
