# Maintainer: Renato Silva <br.renatosilva@gmail.com>

_realname='nsis-nsisunz'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0
pkgrel=2
url='https://nsis.sourceforge.io/Nsisunz_plug-in'
pkgdesc='NSIS plugin which allows you to extract files from ZIP archives (mingw-w64)'
license=('spdx:Zlib')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
msys2_references=(
  'aur: nsis-nsisunz-bin'
)
depends=("${MINGW_PACKAGE_PREFIX}-nsis"
         "${MINGW_PACKAGE_PREFIX}-minizip")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-zlib")
source=('https://nsis.sourceforge.io/mediawiki/images/5/5a/NSISunzU.zip'
        001-use-system-libraries.patch
        LICENSE)
sha256sums=('8c2b7ad6984e3137e4c51c763ec64cbd36364e72838b85e14fa287dac976c46b'
            '491a4ae7dad9230b68f2365cac0ce21a8df2f1185b4b60ad50b20da9abd9d1a7'
            '29c0b103390cb4798ccdad9afdd1a3f5d5cf0bf025dd8a6307d252fc9ca0bdaa')

prepare() {
    cd "${srcdir}/NSISunzU"
    patch -p1 < "${startdir}"/001-use-system-libraries.patch
}

build() {
    cd "${srcdir}/NSISunzU/Source"
    local plugin_api="${MINGW_PREFIX}/share/doc/nsis/Examples/Plugin"

    # Unicode version
    ${CC} -c nsisunz.cpp -o nsisunz-unicode.o -I${plugin_api} -DUNICODE -D_UNICODE
    ${CC} -shared -o nsisunz-unicode.dll nsisunz-unicode.o --static $(pkg-config --static --libs minizip) ${plugin_api}/nsis/libpluginapi-unicode.a

    # ANSI version for x86
    if [[ "${CARCH}" = i686 ]]; then
        ${CC} -c nsisunz.cpp -o nsisunz-ansi.o -I${plugin_api}
        ${CC} -shared -o nsisunz-ansi.dll nsisunz-ansi.o --static $(pkg-config --static --libs minizip) ${plugin_api}/nsis/libpluginapi-ansi.a
    fi
}

package() {
    cd "${srcdir}/NSISunzU/Source"
    install -Dm644 "${srcdir}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
    install -Dm755 nsisunz-unicode.dll "${pkgdir}${MINGW_PREFIX}/share/nsis/Plugins/unicode/nsisunz.dll"
    if [[ "${CARCH}" = i686 ]]; then
        install -Dm755 nsisunz-ansi.dll "${pkgdir}${MINGW_PREFIX}/share/nsis/Plugins/ansi/nsisunz.dll"
    fi
}
