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

_realname=nsis
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.10
pkgrel=2
url='https://nsis.sourceforge.io/'
pkgdesc='Windows installer development tool (mingw-w64)'
license=('spdx:Zlib')
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-scons"
             "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://sourceforge.net/projects/nsis/files/NSIS%203/${pkgver}/${_realname}-${pkgver}-src.tar.bz2"
        001-fhs-directory-structure.patch
        002-compile-fixes.patch
        003-remove-redundant-architecture-suffix.patch
        004-crossmingw-tools-fix.patch
        005-fix-call-to-non-constexpr-function.patch
        006-force-c++11.patch
        007-fix-mingw-compile.patch
        008-fix-uninitialized-vars.patch
        009-fix-clang-asm.patch
        010-fix-clang-rc-file.patch
        011-add-arm64-support.patch)
sha256sums=('11b54a6307ab46fef505b2700aaf6f62847c25aa6eebaf2ae0aab2f17f0cb297'
            '184635b95a441ffe82f7164f09419dc5594265ddb5b96666c4083e63095f291a'
            '1a14c915f67ad7b4e30727853e2c15f39adb6be9a78f51e08ab19851040870c7'
            'e76bc23c4f683f8e2158bf84b8c1207c861475c22bbad79ddb9cfb4df48aa6ef'
            'bb5720841a88296821563a0e55130dcf74da134518c8138dba8c0ab57737ddea'
            'fe2e7a45c9bbab39f20c9ed98c08146a3f24f74874a7f98bceaa92026316b1ba'
            'e4e910abfe9e1f7b17c89d8f952f4419000fa7f5e8c1352818bf8775ca010088'
            'fd79c2f470aaf2a6248cbfa48034a3adefe6e90066431f4d7adc0da076b632c8'
            'f4422b218f4f17a17b33485c76238b8cf8159a20f3fdd1e44e2838b4aaab03ba'
            '385af959dba8bf2333303844e7a765fb8e1df39c2cf245dde8aa945efe694ff8'
            '22549f35edde30b712b3c40acbf9127a59b792b5c8afb32cf07207ac2010729f'
            'b25703cf1881188da6b29aa2b278c4fa413f705df8cef32640a3169d42e301fd')

# Circumvent problem where makepkg will add the exe extension to some files
# when compressing the 64-bit package
options=('!strip')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}-src"
  _apply_patch_with_msg \
    001-fhs-directory-structure.patch \
    002-compile-fixes.patch \
    003-remove-redundant-architecture-suffix.patch \
    004-crossmingw-tools-fix.patch \
    005-fix-call-to-non-constexpr-function.patch \
    006-force-c++11.patch \
    007-fix-mingw-compile.patch \
    008-fix-uninitialized-vars.patch

  if [[ ${MSYSTEM} == CLANG* ]]; then
    _apply_patch_with_msg \
      009-fix-clang-asm.patch \
      010-fix-clang-rc-file.patch
  fi

  if [[ ${MSYSTEM} == CLANGARM64 ]]; then
    _apply_patch_with_msg \
      011-add-arm64-support.patch
  fi

  cp -p SCons/Config/{memcpy,memset}.c Contrib/AdvSplash
  cp -p SCons/Config/{memcpy,memset}.c Contrib/nsDialogs
  cp -p SCons/Config/{memcpy,memset}.c Source/exehead
}

build() {
  cp -r "${srcdir}/${_realname}-${pkgver}-src" "${srcdir}/build-${MSYSTEM}"

  local target_arch='amd64'
  if [[ ${CARCH} == aarch64 ]]; then
    target_arch='arm64'
  elif [[ ${CARCH} == i686 ]]; then
    target_arch='x86'
  fi

  local arrVer=(${pkgver//./ })
  local arrLen=${#arrVer[@]}
  local major_version=${arrVer[0]}
  local minor_version=${arrVer[1]}
  local revision_version='0'
  local build_version='0'
  if ((arrLen > 2)); then
    revision_version=${arrVer[2]}
    if ((arrLen > 3)); then
      build_version=${arrVer[3]}
    fi
  fi

  minor_version="${minor_version%%[a-z]*}"
  msg2 "Version parts ${arrLen}"
  msg2 "Major version defined as ${major_version}"
  msg2 "Minor version defined as ${minor_version}"
  msg2 "Revision version defined as ${revision_version}"
  msg2 "Build version defined as ${build_version}"

  cd "${srcdir}/build-${MSYSTEM}"
  scons \
      TARGET_ARCH="${target_arch}" \
      VERSION="${pkgver}" \
      VER_MAJOR="${major_version}" \
      VER_MINOR="${minor_version}" \
      VER_REVISION="${revision_version}" \
      VER_BUILD="${build_version}" \
      CC="${CC}" \
      CXX="${CXX}" \
      TOOLSET=gcc,mingw \
      ZLIB_W32="${MINGW_PREFIX}" \
      UNICODE='yes' \
      NSIS_MAX_STRLEN='8192' \
      NSIS_CONFIG_CONST_DATA_PATH='no' \
      SKIPUTILS='NSIS Menu,SubStart' \
      PREFIX_DEST="${PREFIX_DEST}" \
      NSIS_CONFIG_LOG='yes' \
      ${target}
}

package() {
  PREFIX_DEST="${pkgdir}${MINGW_PREFIX}" target=install build

  install -Dm644 "${srcdir}/build-${MSYSTEM}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING"
}
