# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=geoipupdate
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.1.0
pkgrel=1
pkgdesc='Update GeoIP2 and GeoIP Legacy binary databases from MaxMind (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://dev.maxmind.com/geoip/geoipupdate"
msys2_repository_url="https://github.com/maxmind/geoipupdate"
msys2_references=(
  'anitya: 230971'
  'archlinux: geoipupdate'
  'gentoo: net-misc/geoipupdate'
)
license=('spdx:Apache-2.0 OR MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "git")
backup=("${MINGW_PREFIX:1}/etc/GeoIP.conf")
options=('!strip')
source=("https://github.com/maxmind/geoipupdate/archive/v$pkgver/$_realname-$pkgver.tar.gz"
        "001-relocate.patch"
        "002-change-defaults-dirs.patch")
sha256sums=('71a5b3125bb7d5f1b78382ec37a0d3e966caea3215689531a32cddc7e4437ca6'
            '4a06217830bc4b777778d6914b0363828017d98493c1bd97250fb3cb847bd182'
            '7a8085a30d3f7e7dc166720050ddc496b11e447766c048a0a3be01936f6426f8')

prepare() {
  cd "$srcdir/$_realname-$pkgver"

  sed -i 's/EditionIDs .*/EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country/' conf/GeoIP.conf.default

  patch -Nbp1 -i "${srcdir}/001-relocate.patch"
  patch -Nbp1 -i "${srcdir}/002-change-defaults-dirs.patch"
}

build() {
    cd "$srcdir/$_realname-$pkgver"

    export GOROOT=${MINGW_PREFIX}/lib/go

    cd cmd/geoipupdate
    mkdir -p "$srcdir/build"
    go build  -o "$srcdir/build/geoipupdate.exe" -ldflags "-X main.version=$pkgver"
}

package() {
    cd "$srcdir/$_realname-$pkgver"

    install -dm755 "${pkgdir}${MINGW_PREFIX}/var/lib/GeoIP"
    install -Dm600 conf/GeoIP.conf.default "${pkgdir}${MINGW_PREFIX}/etc/GeoIP.conf"

    cd "$srcdir/build"
    install -Dm755 geoipupdate.exe "${pkgdir}${MINGW_PREFIX}/bin/geoipupdate.exe"

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