# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=autohotkey
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.25
pkgrel=1
pkgdesc="Automation scripting language for Windows (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.autohotkey.com/'
msys2_repository_url='https://github.com/AutoHotkey/AutoHotkey'
license=('spdx:GPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://github.com/AutoHotkey/AutoHotkey/archive/refs/tags/v${pkgver}.tar.gz"
        CMakeLists.txt
        masm2gas.awk
        AutoHotkey.exe.manifest
        test-basic.ahk
        0001-adapt-precompiled-header-and-config-for-MinGW.patch
        0002-replace-MSVC-structured-exception-handling-with-port.patch
        0003-skip-legacy-wspiapi.h-include-on-MinGW.patch
        0004-fix-C-type-portability-issues-for-GCC.patch
        0005-fix-variadic-macro-trailing-comma-for-GCC.patch
        0006-replace-MSVC-specific-integer-syntax-with-standard-C.patch
        0007-add-type-overloads-and-const-correctness-fixes-for-M.patch
        0008-remove-inline-from-out-of-line-function-definitions.patch
        0009-fix-non-const-lvalue-reference-binding-to-rvalues.patch
        0010-fix-cross-initialization-errors-from-goto-past-decla.patch
        0011-fix-DllCall-float-double-returns-with-GCC.patch
        0012-qualify-member-function-pointer-addresses-for-clang.patch
        0013-cast-function-pointer-to-void-explicitly-in-MdType.h.patch
        0014-fix-const-string-literal-assignment-to-LPTSTR-in-hot.patch
        0015-fix-static-extern-linkage-mismatch-for-sSendMode.patch
        0016-qualify-member-function-addresses-in-ObjectMember-ta.patch
        0017-suppress-deprecated-register-keyword-for-clang.patch
        0018-add-ReturnPtr-LPCTSTR-overload-for-MinGW.patch
        0019-fix-const-string-literal-to-LPTSTR-assignments-for-c.patch
        0020-replace-_countof-with-sizeof-for-arrays-with-incompl.patch
        0021-add-explicit-template-instantiations-for-ScriptItemL.patch)
sha256sums=('083d1f3b084969064528dfa59c66a0d73b0a99a0b80d85c9d267b1df7b058526'
            'de4e313ea798fe5c8f76df1f76c6470ce6492358240615783327fd1502415740'
            '3eccb84dba6a6994053482b5c00f4683e581ebdd565c1fa62812eaf990004746'
            'a7c5d0ebe16295623882f734a62814e23aeee2f2ae9f2004e46c036a91bde67a'
            'd917c016975d26c15c0eb7717db6580bb4ceffc69b8f705876d26f630e5b0ba1'
            '859231aaf689bc70fd9a870839f1b8f24e8ceaf5fe5e41a5ba234b4fbac11a89'
            'e79ddc931c9bca2edd40d8b3d0fda06a752c9daf5d2b7a3cd454fd8a1db39ac4'
            '3787275c7580e70e64074ccc208242da0f8f0671015520c9430d6a18c554f5ce'
            'add61b3b28b2efb7df28a3b25ffd8178076afdfdb775a5ade3cd118ee6f4e71a'
            'ce97ae5d006b4d94e707bcef476121a66937f2a97912a801af7d313e0c92fdd1'
            'd5a2d57a11bb37a465d8619c38deadc6ad4abd55ae87d75756c462a91b42fb92'
            '80278ae38e5f674634e5be9e1c1c32690e253ba8107549095a1b07bf4e5e73cc'
            '25c6430fcf8ce33297560bb1e3f578a9c1e1522f9ff029f3a658f6e60c042df4'
            '5435831bed4e919379e4e42452eccdd1b5c1bbbdf84fb3f39c047250b736ac4b'
            'adecb460fd81593f61bca911405fecf0110c159bbb99febd988b1e37d62b64be'
            '35cfad84a7365afcff499e0fc8d240643214cf09a6114e578fb695b55df1170d'
            '3565c377d8f1da4c77ac425558d7c40e01a6aa1ecd0e9bedf0a120c001147aee'
            '59dd1571773d25fcdeced8f189fe5fe99d43edfc250b5ed09c6954c1897d7389'
            '20d99938fc9f77e4efbce14f780ad32fa41cf2d1de5fbefe300b56febacd2ba1'
            'a591350f30ce880f9130c2d550bb4aed82bdc0c8efdcfd6146d05f1ba3122846'
            '8074e7b4ec18cd7d24c916e3f75e8c5ce6bd0fbfbe85a48f35c2af3ffb266759'
            '49bb295a53c06094bc98763401fa5efc9b94e85217c38160e611908434c5de36'
            'c2241244688348d56ecaefc10b6bcae4e690cd9965c0d77948939516e87245d1'
            '6a45526b8b49b1de373c7b2042a06d540d71acf6ad342a6953f2e52cf1921bc2'
            '7cc0bac3db5eb7f3db318a7132bd4774e02d8319c4cfb2e92e1672b54b03d680'
            '4eee6b0dc8bde9e42cce542ff90aadf3de84883279ea8d852067a1dce4ebbebc')

prepare() {
  cd "AutoHotkey-${pkgver}"

  # Apply MinGW/GCC portability patches
  for p in "${srcdir}"/00[012][0-9]-*.patch; do
    patch -p1 -i "$p" || return 1
  done

  # Copy in the CMake build system
  cp "${srcdir}/CMakeLists.txt" .

  # Convert upstream MASM assembly to GAS (Intel syntax) at build time
  # so the .S files track upstream changes automatically.
  awk -f "${srcdir}/masm2gas.awk" source/libx64call/x64call.asm > x64call.S
  awk -f "${srcdir}/masm2gas.awk" source/libx64call/x64stub.asm > x64stub.S

  # Install the application manifest (referenced by the .rc resource file)
  mkdir -p temp
  cp "${srcdir}/AutoHotkey.exe.manifest" temp/
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  export CFLAGS="-O2 -pipe -flto"
  export CXXFLAGS="-O2 -pipe -flto"
  export LDFLAGS="-flto"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      "${srcdir}/AutoHotkey-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

check() {
  cd build-${MSYSTEM}
  {
    MSYS2_ARG_CONV_EXCL="/ErrorStdOut" \
      ./bin/AutoHotkey64.exe /ErrorStdOut "${srcdir}/test-basic.ahk"
    echo $? >exit.code
  } | tee test.log
  test 0 = "$(cat exit.code)" || return "$(cat exit.code)"
}

package() {
  cd build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Also install as just "AutoHotkey.exe" for convenience
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  cp AutoHotkey64.exe AutoHotkey.exe
}
