# Description:	GeoLite country geolocation database compiled by MaxMind
# URL: https://dev.maxmind.com/geoip/legacy/geolite
# Packager : NuTyX core team, Sipo
# Depends on: geoip

name=geoip-database
version=20170704
release=1
source=(https://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
        https://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
        https://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
        https://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz
        https://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
        https://download.maxmind.com/download/geoip/database/asnum/GeoIPASNumv6.dat.gz
        https://geolite.maxmind.com/download/geoip/database/LICENSE.txt)
noextract=(GeoIP-$version.dat.gz
           GeoIPv6-$version.dat.gz
           GeoLiteCity-$version.dat.gz
           GeoLiteCityv6-$version.dat.gz
           GeoIPASNum-$version.dat.gz
           GeoIPASNumv6-$version.dat.gz)

build() {
	cd $SRC
	for _database_name in GeoIP GeoLiteCity GeoIPASNum; do
    gunzip -c $_database_name.dat.gz >${_database_name/GeoLite/GeoIP}.dat
    gunzip -c ${_database_name}v6.dat.gz >${_database_name/GeoLite/GeoIP}v6.dat
  done

 if [[ $(geoiplookup -f GeoIP.dat 8.8.8.8) != *'US, United States' ]]; then
    error 'Unable to resolve IPv4 address to country.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPv6.dat 2001:4860:4860::8888) != *'US, United States' ]]; then
    error 'Unable to resolve IPv6 address to country.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPCity.dat 8.8.8.8) != *'US, N/A, N/A'* ]]; then
    error 'Unable to resolve IPv4 address to city.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPCityv6.dat 2001:4860:4860::8888) != *'US, N/A, N/A'* ]]; then
    error 'Unable to resolve IPv6 address to city.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPASNum.dat 8.8.8.8) != *'AS15169 Google Inc.' ]]; then
    error 'Unable to resolve IPv4 address to ASN.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat 2001:4860:4860::8888) != *'AS15169 Google Inc.' ]]; then
    error 'Unable to resolve IPv6 address to ASN.'
    return 1
  fi

  install -d $PKG/usr/share/GeoIP
  install -m644 -t $PKG/usr/share/GeoIP GeoIPCityv6.dat
  install -m644 -t $PKG/usr/share/GeoIP GeoIPv6.dat
  install -m644 -t $PKG/usr/share/GeoIP GeoIPASNumv6.dat
  install -m644 -t $PKG/usr/share/GeoIP GeoIPCity.dat
  install -m644 -t $PKG/usr/share/GeoIP GeoIP.dat
  install -m644 -t $PKG/usr/share/GeoIP GeoIPASNum.dat
  install -Dm644 LICENSE.txt $PKG/usr/share/licenses/$name/LICENSE.txt

}
