# Maintainer: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Evangelos Foutras <foutrelis@archlinux.org>

pkgname=(geoip-database geoip-database-extra)
pkgver=20260204
pkgrel=1
arch=(any)
url='https://mailfud.org/geoip-legacy/'
license=('LicenseRef-GeoLite2-End-User-License-Agreement')
makedepends=('util-linux')
checkdepends=('geoip')
_source_base="https://mailfud.org/geoip-legacy"
source=(GeoIP-$pkgver.dat.gz::$_source_base/GeoIP.dat.gz
        GeoIPv6-$pkgver.dat.gz::$_source_base/GeoIPv6.dat.gz
        GeoIPCity-$pkgver.dat.gz::$_source_base/GeoIPCity.dat.gz
        GeoIPCityv6-$pkgver.dat.gz::$_source_base/GeoIPCityv6.dat.gz
        GeoIPASNum-$pkgver.dat.gz::$_source_base/GeoIPASNum.dat.gz
        GeoIPASNumv6-$pkgver.dat.gz::$_source_base/GeoIPASNumv6.dat.gz
        GeoLite2-End-User-License-Agreement.pdf)
sha256sums=('d10d97abe094509218c3bfbab2c085c35406e49164767f04486789e32fd751e1'
            '7e1b798a389748d0d2dfdc4b1a9257c29d27b93b556232f944a02be40caf6bc3'
            '758af6e2e96609db036ce267079394ab10c6bd8e006e35e358f8850d04ccb24e'
            '4800bd69ce71b040881274fb172ed98c440c090c74a7c97dd964d9eb50a196ba'
            'd48002a2d6cfc0eecf7e49e22de1856078f5c41504619ab188b2e79a8dfca007'
            'd0da85a62ef65b13f5242801f0010bc834b78e3d9c7e6f660d30248b07760379'
            'b9c7fbfcdd5b682b5635592281d72006df89d73f163308b7ef35a59f293b036e')

prepare() {
  cd "$srcdir"
  rename -v -- "-$pkgver" '' *.dat
}

check() {
  cd "$srcdir"

  if [[ $(geoiplookup -f GeoIP.dat archlinux.org) != *'US, United States' ]]; then
    echo >&2 'Unable to resolve IPv4 address to country.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPv6.dat archlinux.org) != *'US, United States' ]]; then
    echo >&2 'Unable to resolve IPv6 address to country.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPCity.dat archlinux.org) != *'US, '* ]]; then
    echo >&2 'Unable to resolve IPv4 address to city.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPCityv6.dat archlinux.org) != *'US, '* ]]; then
    echo >&2 'Unable to resolve IPv6 address to city.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPASNum.dat archlinux.org) != *'AS11019 HAProxy Technologies'* ]]; then
    echo >&2 'Unable to resolve IPv4 address to ASN.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat archlinux.org) != *'AS11019 HAProxy Technologies'* ]]; then
    echo >&2 'Unable to resolve IPv6 address to ASN.'
    return 1
  fi
}

package_geoip-database() {
  pkgdesc="GeoIP legacy country database (based on GeoLite2 data created by MaxMind)"
  install=$pkgname.install

  cd "$srcdir"
  install -Dm644 -t "$pkgdir/usr/share/GeoIP" GeoIP{,v6}.dat
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" GeoLite2-End-User-License-Agreement.pdf
}

package_geoip-database-extra() {
  pkgdesc="GeoIP legacy city/ASN databases (based on GeoLite2 data created by MaxMind)"
  install=$pkgname.install

  cd "$srcdir"
  install -Dm644 -t "$pkgdir/usr/share/GeoIP" GeoIPCity{,v6}.dat GeoIPASNum{,v6}.dat
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" GeoLite2-End-User-License-Agreement.pdf
}
