# 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=('b7345d94b7e31a4e780d4ceef31b1fd82cc9150087f795c3931a5fdd07e113c2'
            'c1b90ba54336435437e1d80f941e77c6d593709ea8634e80925ea143a3186724'
            '3e4a5763318e74f8b682db25530ba26b089417ee8a4f343a8e00964089fc4523'
            'ae5ec810880783818bca396963ad392cc4a092e57215907173c4093582c00d17'
            'cbb5978cea0dcad1e3a1fce38da5d4425b24e00ade2533e8fe9e8832b72c5e36'
            'ff1a64d20128f8e08b25477edc5f375f1e08402fa6667cdc472a061910d8ca4b'
            '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
}
