# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Contributor: Baptiste Jonglez <archlinux at bitsofnetworks.org>
# Contributor: Christian Hesse <mail@eworm.de>
# Contributor: Andreas Wagner <andreas.wagner@lowfatcomputing.org>
# Contributor: Jeff Mickey <jeff@archlinux.org>

pkgname=icmake
pkgver=10.03.03
pkgrel=2.1
pkgdesc='A program maintenance (make) utility using a C-like grammar'
arch=('x86_64' 'aarch64')
url='https://fbb-git.gitlab.io/icmake/'
license=('AGPL-3.0-or-later')
_bobcatver=6.02.02
source=("https://gitlab.com/fbb-git/${pkgname}/-/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz"
        'disable-clear-screen.patch'
        'icmake-10.03.03-1-cstdint.patch'
        "https://gitlab.com/fbb-git/bobcat/-/archive/${_bobcatver}/bobcat-${_bobcatver}.tar.gz")
b2sums=('4711dee5d66882afeeb6e7206b97341690e2e1c0e511fcf9f43d41ec04bec2ecf5fee10113bba47475b3166dc8dc53f7b236c42dcf0969582a59bbf30b7ca618'
        '6689ce74a985742e079f95bfaa22dcee22ad667f45cf21f79d4c3d2271f925f53bcd6729ca762a7cfa6a63af9b0fd48c2b9c024bd35ca580e83a6a54758a7bed'
        '8c80ee554855f3466ad89006a6d5f73413f069c456be47cf40bd50586de95bbb3a0f253a0aca99953e58cd27f1be5f076ecc3c9778d2f56997bdba8cc83591a5'
        '4908fef5685e5402795000fafee7fcb3c42c4cf94571121df7e1afd977961f3d3036393f7140867abb9a019713ddb886053e4900172ef1072944b77444d2b11d')
makedepends=('base-devel') # 'icmake' 'yodl'
depends=('libunwind') # 'libbobcat'

prepare() {
    cd "${srcdir}/${pkgname}-${pkgver}"
    patch -Np1 -i "${srcdir}/disable-clear-screen.patch"
    patch -Np1 -i "${srcdir}/icmake-10.03.03-1-cstdint.patch"

    # Make it okay to have no manpages; can't build them with yodl
    sed -i '2i shopt -s nullglob' icmake/icm_bootstrap

    # fix paths
    sed -i 's|usr/libexec/icmake|usr/lib/icmake|' icmake/INSTALL.im

    # for some reason we have to do this
    sed -e '/verbinclude(\/home\/frank\/.icmake\/AUTHOR)/d' -i icmake/doc/manpage/icmconf.yo
    export AUTHOR="Frank B. Brokken (f.b.brokken@rug.nl)"
    echo "${AUTHOR}" > AUTHOR

    (
        cd "${srcdir}/bobcat-${_bobcatver}/bobcat"
        cp "${srcdir}/${pkgname}-${pkgver}/${pkgname}/bobcatbootstrap" .
        mkdir -p "${srcdir}/bobcat-tmp"
        sed -i "s#hdrDest=#hdrDest=${srcdir}/bobcat-tmp#" bobcatbootstrap
        sed -i "s#libDest=#libDest=${srcdir}/bobcat-tmp#" bobcatbootstrap
        sed -i "s#g++ -O2#g++ -O2 -I${srcdir}/bobcat-tmp/usr/include#" bobcatbootstrap
    )
}

build() {
    set -x

    local _tmpbobcatdir="${srcdir}/bobcat-tmp"
    (
        cd "${srcdir}/bobcat-${_bobcatver}/bobcat"
        /bin/yes | ./bobcatbootstrap
    )

    cd "${srcdir}/${pkgname}-${pkgver}"
    # when upgrading to new SONAME, remove ./manpages from HOME variable
    # to allow compiling the package without invoiking icmake itself
    # HOME=./ ./manpages

    cd "${srcdir}/${pkgname}-${pkgver}/${pkgname}"

    export CXXFLAGS="${CXXFLAGS} -Wp,-U_GLIBCXX_ASSERTIONS -std=c++20 -I${_tmpbobcatdir}/usr/include"
    export LDFLAGS="${LDFLAGS} -L${_tmpbobcatdir}/usr/lib"

    ./icm_prepare /
    ./icm_bootstrap /
}

package() {
    cd "${srcdir}/${pkgname}-${pkgver}/${pkgname}"

    ./icm_install all "${pkgdir}"
}
