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

_realname='nsis'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.06.1
pkgrel=1
url='https://nsis.sourceforge.io/'
pkgdesc='Windows installer development tool (mingw-w64)'
license=(ZLIB)
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=(${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-libwinpthread)
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc
             'dos2unix'
             'scons>=2.3.4-3'
             rsync)
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-fix-jump-to-label-error.patch
        005-fix-call-to-non-constexpr-function.patch
        py3-fixes.patch)
sha256sums=('9b5d68bf1874a7b393432410c7e8c376f174d2602179883845d2508152153ff0'
            'a2719915676106bc0c545fca6d5871c96044c7e4ba8c95ea5ca03a0a49b2b6e3'
            '4b94e5f5979ea13e19a37606ec5861dfdc0004b9e4eaf984e4aae4a7fe5e85b7'
            'f43af517d7821084335a38bce15c7644cc114c71cf6f8ab860aa4d9322795343'
            '44d854b1d54befeb381ec8712a8baf624d08b014bdf1f1da18a8af2f77d4b8f4'
            'c07e7c13ebcfe38495dfdb886f2cea363e5a8a1c17bd786ece0f15625ac3f864'
            'b3a31606df9dbce9fd0b4e6b3f4b2655f8695f945bb391be35a90bd39e109a02')

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

consolidate() {
  msg2 'Converting to unix line breaks'
  find -name 'SConstruct' | xargs dos2unix --quiet
  find -name 'SConscript' | xargs dos2unix --quiet
  find -name '*.[ch]'     | xargs dos2unix --quiet
  find -name '*.cpp'      | xargs dos2unix --quiet
  find -name '*.py'       | xargs dos2unix --quiet
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}-src"
  consolidate
  patch -p1 -i "${srcdir}"/001-fhs-directory-structure.patch
  patch -p1 -i "${srcdir}"/002-compile-fixes.patch
  patch -p1 -i "${srcdir}"/003-remove-redundant-architecture-suffix.patch
  #patch -p1 -i "${srcdir}"/004-fix-jump-to-label-error.patch
  patch -p1 -i "${srcdir}"/005-fix-call-to-non-constexpr-function.patch
  patch -p1 -i "${srcdir}"/py3-fixes.patch
}

_build() {
  local target_arch="${CARCH/i686/x86}"
  target_arch="${target_arch/x86_64/amd64}"
  
  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-${CARCH}-${_realname}-${pkgver}"
  scons \
      TARGET_ARCH="${target_arch}" \
      PREFIX="${MINGW_PREFIX}" \
      VERSION="${pkgver}" \
      VER_MAJOR="${major_version}" \
      VER_MINOR="${minor_version}" \
      VER_REVISION="${revision_version}" \
      VER_BUILD="${build_version}" \
      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}
}

build() {
  msg2 'Synchronizing build directory'
  rsync --recursive --times "${srcdir}/${_realname}-${pkgver}-src"/* "${srcdir}/build-${CARCH}-${_realname}-${pkgver}"
  _build
}

package() {
  PREFIX_DEST="${pkgdir}" target='install' _build
  install -Dm644 "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING"
}
