# Maintainer: Evangelos Foutras <foutrelis@archlinux.org>

pkgname=('geoip-database' 'geoip-database-extra')
pkgver=20250129
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://sources.archlinux.org/other/packages/$pkgname/$pkgver
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=('3232f341cf55b60c4e377ab7dafd3685e176d016476e3481591022f8682da53d'
            'b2c07f64789860bc35d167b7b1be6c5ec1ae717d7998d7c4f9baaa2affeec19b'
            'bf2e8452642514ca64e2ba6aace3c3bf08be7b79f0d994fd98d68dc87b395ce4'
            '7fb0aa2104dbbc087ba503c463d65902143b750a5e4c9e26c3739287512b403a'
            '1ef72f0af2e086f52bab35841c17cc22b14e0c51fb6690bdd4916b479f2d4413'
            '6012231fec1407fff5ebdd5c4a181bd744169bfe9c4aacaf24ddbd34607a99e4'
            'b9c7fbfcdd5b682b5635592281d72006df89d73f163308b7ef35a59f293b036e')

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

check() {
  cd "$srcdir"

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

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

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

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

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

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat archlinux.org) != *'AS24940 Hetzner Online GmbH' ]]; 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
}

# vim:set ts=2 sw=2 et:
