# Maintainer: lilydjwg <lilydjwg@gmail.com>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

_hkgname=ShellCheck
pkgname=shellcheck-static
pkgver=0.11.0
pkgrel=1
pkgdesc="Shell script analysis tool (static version not depending on GHC libraries)"
url="https://www.shellcheck.net"
license=("GPL-3.0-or-later")
arch=('x86_64')
depends=('gmp')
makedepends=('curl' 'pandoc')
provides=("shellcheck=$pkgver")
conflicts=(shellcheck)
source=("$_hkgname-$pkgver.tar.gz::https://github.com/koalaman/shellcheck/archive/v$pkgver.tar.gz")
sha512sums=('97700b6997cdf4e4d8af62ab77401c315797f5effbe79100bb048870b549f0722ba471248d807c40d87cf753ad61271a8048f9fded25d01dcc1fec76ccd6a2c5')

build() {
    cd shellcheck-$pkgver

    if [[ ! -f ~/.ghcup/env ]]; then
      curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | BOOTSTRAP_HASKELL_NONINTERACTIVE=1 BOOTSTRAP_HASKELL_MINIMAL=1 sh
    fi
    source ~/.ghcup/env
    ghcup install stack latest
    # stack upgrade
    # -Wl,-z,pack-relative-relocs is not supported by ld.gold
    export LDFLAGS="-Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now"
    stack build --ghc-options="-j$(nproc)"
    ./manpage
}

check() {
    cd shellcheck-$pkgver
    stack test
}

package() {
    cd shellcheck-$pkgver

    install -D -m644 shellcheck.1 "${pkgdir}/usr/share/man/man1/shellcheck.1"
    stack install --local-bin-path="${pkgdir}/usr/bin"
}
