# Contributor (MSYS2): David Macek <david.macek.0@gmail.com>
# Maintainer (Arch Linux): Anatol Pomozov <anatol pomozov at gmail>
# Contributor (Arch Linux): Daniel YC Lin <dlin.tw at gmail>
# Contributor (Arch Linux): Konstantin Plotnikov <kostyapl at gmail dot com>

_realname=fossil
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.16
_pkgsha=7aedd56758
pkgrel=1
pkgdesc='Simple, high-reliability, distributed software configuration management (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('BSD')
url='https://www.fossil-scm.org/'
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-tcl")
optdepends=("${MINGW_PACKAGE_PREFIX}-putty: SSH transport")
source=("https://www.fossil-scm.org/home/tarball/${_pkgsha}/${_realname}-src-${pkgver}.tar.gz"
        "win.patch"
        "readline.patch"
        "win-tcl-unix-path.patch")
sha256sums=('fab37e8093932b06b586e99a792bf9b20d00d530764b5bddb1d9a63c8cdafa14'
            'fb39c12fa1068dffa18713aae604c94d02eea5c1af279654a8f25c337c6517a9'
            '887c8b16de984156cb13bfdbc5715b1eff0123af0d07e6dc08265dbef0a1d519'
            '96090936b1aee3539211c400a4c8a7a9475c71f47238967a1924229798a2910b')

prepare() {
  cd "${srcdir}/${_realname}-src-${pkgver}"
  patch -p1 -i "${srcdir}/win.patch"
  patch -p0 -i "${srcdir}/readline.patch"
  patch -p1 -i "${srcdir}/win-tcl-unix-path.patch"
  # this doesn't get expanded, so delete it just to be sure
  sed -i '/ENABLE_MINIZ/d' Makefile.in
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL=--prefix \
  ../${_realname}-src-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-zlib=${MINGW_PREFIX} \
    --debug \
    --disable-internal-sqlite

  make \
    CFLAGS+="-DBROKEN_MINGW_CMDLINE=not_actually_broken"
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-src-${pkgver}/COPYRIGHT-BSD2.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
