# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=ccache
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.7.9
pkgrel=2
pkgdesc="Compiler cache that speeds up recompilation by caching previous compilations (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://ccache.dev/"
license=("GPL3")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("https://github.com/ccache/ccache/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "01-directory-separator.patch"
        "010-ucrt.patch")
sha256sums=('f893da7543bfb9172bd55e603fcbdfcd83e6def176a28689c13235695b4cf44b'
            'SKIP'
            '888748bd62a23b909dda4df714ffb2bb09db3fa698d199754f372a01b5a55179'
            '9a569472260674d61e983ae9c617eb5ec90e78b525741a40267b92de0f9f35ae')
validpgpkeys=('5A939A71A46792CF57866A51996DDA075594ADB8') # Joel Rosdahl <joel@rosdahl.net>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/01-directory-separator.patch"
  patch -p1 -i "${srcdir}/010-ucrt.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}  && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  # hack: use bash scripts as shortcuts since we cannot use symlinks
  install -d ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  cd ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  scripts=(c++ cc cpp gcc g++ ${MINGW_CHOST}-g++ ${MINGW_CHOST}-c++ ${MINGW_CHOST}-gcc)
  for fn in ${scripts[*]}; do
    echo -e '#!/bin/bash\n\nccache '${MINGW_PREFIX}'/bin/'$fn' "$@"' > $fn
  done
}
