# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=rclone
_librealname=librclone
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_librealname}")
pkgver=1.72.1
pkgrel=1
pkgdesc='Cloud-aware file synchonization utility (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://rclone.org/'
msys2_references=(
  'archlinux: rclone'
  "cpe: cpe:/a:rclone:rclone"
  'gentoo: net-misc/rclone'
  'purl: pkg:golang/github.com/rclone/rclone'
)
msys2_repository_url='https://github.com/rclone/rclone/'
msys2_changelog_url='https://rclone.org/changelog/'
msys2_documentation_url='https://rclone.org/docs/'
license=('spdx:MIT')
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-tools")
options=('!strip')
source=("https://downloads.rclone.org/v${pkgver}/${_realname}-v${pkgver}.tar.gz")
sha256sums=('227b84024e7e72056f716ee520c4db8b88ec5f80dc83f0899b431b2ce2b25f9b')

build() {
  cd "$srcdir/${_realname}-v${pkgver}"
  go build -ldflags -s
  (
    cd librclone
    go build --buildmode=c-shared -ldflags -s -o ${_librealname}.dll
    gendef ${_librealname}.dll
    dlltool -d ${_librealname}.def -l ${_librealname}.dll.a
  )
}

package_rclone() {
  cd "${srcdir}/${_realname}-v${pkgver}"
  install -Dm644 ${_realname}.exe -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/rclone.1" -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}

package_librclone() {
  pkgdesc='Cloud-aware file synchonization library (mingw-w64)'
  libdesc='Cloud-aware file synchonization library'
  cd "${srcdir}/${_realname}-v${pkgver}"
  install -Dm644 librclone/${_librealname}.dll -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm644 librclone/${_librealname}.h -t "${pkgdir}${MINGW_PREFIX}/include"
  install -Dm644 librclone/${_librealname}.dll.a -t "${pkgdir}${MINGW_PREFIX}/lib"
  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_librealname}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo "
    prefix=${MINGW_PREFIX}
    libdir=\${prefix}/lib
    includedir=\${prefix}/include
    Name: ${_realname}
    URL: ${url}
    Version: ${pkgver}
    Description: ${libdesc}
    Cflags: -I\${includedir}
    Libs: -L\${libdir} -l${_realname}
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
