##
##  geoip2.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_geoipupdate   3.1.1
%define       V_libmaxminddb  1.4.3

#   package information
Name:         geoip2
Summary:      Geographic IP Resolution
URL:          http://www.maxmind.com/
Vendor:       MaxMind
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Mapping
License:      CC-BY-SA/GPL/Apache
Version:      %{V_libmaxminddb}
Release:      20200807

#   list of sources
Source0:      https://github.com/maxmind/geoipupdate/releases/download/v%{V_geoipupdate}/geoipupdate-%{V_geoipupdate}.tar.gz
Source1:      https://github.com/maxmind/libmaxminddb/releases/download/%{V_libmaxminddb}/libmaxminddb-%{V_libmaxminddb}.tar.gz
Source2:      geoipupdate.conf
Source3:      rc.geoip2
Patch0:       geoip2.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  zlib, curl
PreReq:       zlib, curl

%description
    MaxMind GeoIP enables you to easily lookup countries and cities
    by IP addresses, even when Reverse DNS entries don't exist. The
    GeoIP database contains IP Network Blocks as keys and countries and
    cities as values, covering every public IP address. The GeoLite2
    databases are licensed under Creative Commons Attribution-ShareAlike
    3.0 Unported License, which may be met by a user statement like
    "This product includes GeoLite2 data created by MaxMind, available
    from www.maxmind.com". The GeoIPUpdate program is licensed under GNU
    General Public License. The MaxMindDB lookup library and tool is
    licensed under Apache license.

%track
    prog geoip2:geoipupdate = {
        version   = %{V_geoipupdate}
        url       = https://github.com/maxmind/geoipupdate/releases
        regex     = geoipupdate-(__VER__)\.tar\.gz
    }
    prog geoip2:libmaxminddb = {
        version   = %{V_libmaxminddb}
        url       = https://github.com/maxmind/libmaxminddb/releases
        regex     = libmaxminddb-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %patch -p0

%build
    #   build update tool
    (   cd geoipupdate-%{V_geoipupdate}
        CC="%{l_cc}" \
        CFLAGS="%{l_cflags -O}" \
        CPPFLAGS="%{l_cppflags} `curl-config --cflags`" \
        LDFLAGS="%{l_ldflags}" \
        LIBS="`curl-config --libs`" \
        ./configure \
            --prefix=%{l_prefix} \
            --sysconfdir=%{l_prefix}/etc/geoip2 \
            --mandir=%{l_prefix}/man \
            --disable-shared
        %{l_make} %{l_mflags -O}
    ) || exit $?

    #   build lookup tool & library
    (   cd libmaxminddb-%{V_libmaxminddb}
        CC="%{l_cc}" \
        CFLAGS="%{l_cflags -O}" \
        CPPFLAGS="%{l_cppflags}" \
        LDFLAGS="%{l_ldflags}" \
        LIBS="" \
        ./configure \
            --prefix=%{l_prefix} \
            --sysconfdir=%{l_prefix}/etc/geoip2 \
            --mandir=%{l_prefix}/man \
            --disable-shared
        %{l_make} %{l_mflags -O}
    ) || exit $?

%install
    #   install update tool
    (   cd geoipupdate-%{V_geoipupdate}
        %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   install lookup tool & library
    (   cd libmaxminddb-%{V_libmaxminddb}
        %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   provide default configuration
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/geoip2/*.default
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE geoipupdate.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/geoip2/GeoIP.conf

    #   provide run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.geoip2} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   provide database directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/geoip2/db

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/geoip2/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/geoip2/db'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on installation, fetch initial database files
        ( echo "Before using GeoIP2 you first have to (optionally) configure your"
          echo "commercial GeoIP2 license (or just use the GeoLite2 license provided):"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/geoip2/GeoIP2.conf"
          echo "Then once trigger the downloading of the GeoIP2 databases files"
          echo "(or alternatively just wait 24 hours until this is done automatically):"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc geoip2 daily"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   on deinstallation, remove database files
        rm -f $RPM_INSTALL_PREFIX/var/geoip2/db/* >/dev/null 2>&1 || true
    fi
    exit 0

