# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=shfmt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.12.0
pkgrel=1
pkgdesc='Format shell programs (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/mvdan/sh'
msys2_references=(
  'anitya: 242179'
  'archlinux: shfmt'
  'gentoo: dev-util/sh'
  'purl: pkg:golang/mvdan.cc/sh/v3/cmd/shfmt'
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-scdoc")
options=('!strip')
source=("https://github.com/mvdan/sh/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('ac15f42feeba55af29bd07698a881deebed1cd07e937effe140d9300e79d5ceb')

build() {
    cd "${srcdir}/sh-${pkgver}/cmd/shfmt/"
    export GOOS=windows
    export GOROOT=${MINGW_PREFIX}/lib/go
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      i686|x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    go build
    scdoc < "${_realname}.1.scd" | gzip > "${_realname}.1.gz"
}

check(){
    cd "${srcdir}/sh-${pkgver}/"
    go test ./... || warning "Tests failed"
}

package() {
    cd "${srcdir}/sh-${pkgver}/"
    install -Dm755 -t "${pkgdir}${MINGW_PREFIX}/bin/" "cmd/shfmt/shfmt.exe"
    install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1/" "cmd/shfmt/shfmt.1.gz"
    install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/shfmt/" LICENSE
}
