# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=aws-sdk-cpp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.149
pkgrel=2
pkgdesc="AWS SDK for C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/aws/aws-sdk-cpp"
license=('Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-aws-c-common"
         "${MINGW_PACKAGE_PREFIX}-aws-c-event-stream")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' '!debug' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"${url}/archive/${pkgver}.tar.gz"
        "aws-sdk-cpp-pr-1333.patch"
        "0001-fix-dll-import-libary-folder.patch")
sha256sums=('929401ee268681ad67b4f42cc43e616341aa588699e5ae2cd0ec14d6acced21d'
            '6d923d6eb72213bdfe44c6b41444be1817fbe30c5ef21bdd7c94200d417539f3'
            '2b37b187b41cb567b702d01bfa6c2eddd0121c28f52a25514e777155ea0b5cd7')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/aws-sdk-cpp-pr-1333.patch"
  patch -Np1 -i "${srcdir}/0001-fix-dll-import-libary-folder.patch"

  # For clang
  sed -e "s/-fPIC//g" -e "s/-Werror//g" -i cmake/compiler_settings.cmake
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  echo "Building static library"
  [[ -d "${srcdir}/build-${CARCH}-static" ]] && rm -rf "${srcdir}/build-${CARCH}-static"
  mkdir -p "${srcdir}/build-${CARCH}-static" && cd "${srcdir}/build-${CARCH}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_ONLY="config;identity-management;s3;sts;transfer" \
      -DENABLE_UNITY_BUILD=ON \
      -DENABLE_TESTING=OFF \
      -DBUILD_DEPS=OFF \
      ../${_realname}-${pkgver}

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

  echo "Building shared library"
  [[ -d "${srcdir}/build-${CARCH}-shared" ]] && rm -rf "${srcdir}/build-${CARCH}-shared"
  mkdir -p "${srcdir}/build-${CARCH}-shared" && cd "${srcdir}/build-${CARCH}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G "Ninja" \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_ONLY="config;identity-management;s3;sts;transfer" \
      -DENABLE_UNITY_BUILD=ON \
      -DENABLE_TESTING=OFF \
      -DBUILD_DEPS=OFF \
      ../${_realname}-${pkgver}

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

package() {
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
