# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=libargp
pkgname=(${_realname} ${_realname}-devel)
pkgver=20241207
pkgrel=1
arch=('i686' 'x86_64')
pkgdesc="Interface for parsing command-line arguments"
url="https://www.gnu.org/software/gnulib/"
license=('spdx:LGPL-3.0-or-later')
makedepends=('autotools' 'gcc' 'git')
_commit=5e1283bb13d74c6619afb71c6a322c648a8cc91e
source=("git+git://git.savannah.gnu.org/gnulib.git#commit=${_commit}"
        libargp-config.patch
        libargp-global-variables.patch
        libargp-so.patch)
msys2_repository_url="https://git.savannah.gnu.org/cgit/gnulib.git/tree/modules/argp"
sha256sums=('03cb3660580d3f0a9742cc72bd8aaf27392375a66049f09588836f093c1fddef'
            'daa133c2b8cbc8218ba2c11b86783e3e5acef79f833a9e126ce86acf30336564'
            '2ec8108e7583711768355a8d30226219cdfe1bc9a4d49d89c6c659dbfaeb2d39'
            'be94afdd1e6f3cbf886c1de707a699214c836a2505ab9e82cfa464135e8b8a0f')

prepare() {
  cd "gnulib"
  ./gnulib-tool --verbose --create-testdir --dir="${srcdir}/argp" --libtool --lib=libargp --lgpl 3 argp
  cd "${srcdir}/argp"

  # from cygwin (https://cygwin.com/cgit/cygwin-packages/libargp/tree/)
  patch -p2 -i ${srcdir}/libargp-config.patch
  patch -p2 -i ${srcdir}/libargp-global-variables.patch
  patch -p2 -i ${srcdir}/libargp-so.patch

  autoreconf -fi
}

build() {
  mkdir -p "${srcdir}/build-${CHOST}"
  mkdir -p "${srcdir}/install-${CHOST}"
  cd "${srcdir}/build-${CHOST}"

  ../argp/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST}

  cp config.h gllib/argp-config.h
  make
  make DESTDIR="${srcdir}/install-${CHOST}" install

  cp gllib/argp-config.h "${srcdir}/install-${CHOST}/usr/include"
  mkdir -p "${srcdir}/install-${CHOST}/usr/share/licenses/${_realname}"
  cp ${srcdir}/gnulib/doc/COPYING.LESSERv3 "${srcdir}/install-${CHOST}/usr/share/licenses/${_realname}"
}

package_libargp() {
  cd "${srcdir}/install-${CHOST}"
  mkdir "${pkgdir}/usr"
  cp -r usr/{bin,share} "${pkgdir}/usr"
}

package_libargp-devel() {
  groups=('development')
  depends=(${_realname}=${pkgver})

  cd "${srcdir}/install-${CHOST}"
  mkdir "${pkgdir}/usr"
  cp -r usr/{lib,include} "${pkgdir}/usr"
}
