#!/bin/bash
#----------------------------------------------------------------------------
# /usr/bin/xtables-geoipupdate-legacy
#
# Creation   :  2020-12-05 hb
# Last update:  $Id$
#
# Copyright (c) 2020-@@YEAR@@ Holger Bruenjes, holgerbruenjes(at)gmx(dot)net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------
geoip_conf='/etc/GeoIP/GeoIP.conf'


# ---------------------------------------------------------------------------
# conf value
# ---------------------------------------------------------------------------
conf_value()
{
    key=${1}
    value=$(/usr/bin/grep "^${key}" ${geoip_conf} | /usr/bin/sed -e 's:#.*::' -e "s:${key}::")
    if [ -z "${value}" ]
    then
        myecho ${key} not configured in ${geoip_conf}
        exit 1
    fi
    echo ${value}
}

# ---------------------------------------------------------------------------
# download geodata csv
# ---------------------------------------------------------------------------
download_geodata_csv()
{
    csv_product=${1}
    myecho ">>> Downloading ${csv_product}.zip"
    /usr/bin/curl ${silent} -J -L -u ${account_id}:${license_key} "https://download.maxmind.com/geoip/databases/${csv_product}/download?suffix=zip" \
         -o ${database_directory}/${csv_product}.zip

    /usr/bin/curl ${silent} 'http://download.geonames.org/export/dump/countryInfo.txt' -o ${database_directory}/countryInfo.txt
}

# ---------------------------------------------------------------------------
# geolite to legacy
# ---------------------------------------------------------------------------
geolite_to_legacy()
{
    csv_product=${1}
    myecho ">>> Converting ${csv_product}.zip to legacy format"

    pushd ${database_directory}
    if [ ! -f ${csv_product}.zip ] 
    then
        myecho "${database_directory}/${csv_product}.zip not found"
        return
    fi
    if ! /usr/bin/file -b --mime-type ${csv_product}.zip | /usr/bin/grep -q 'application/zip'
    then
        message=$(cat ${csv_product}.zip) 
        myecho "Please check your LicenseKey = '${license_key}'"
        myecho -e "${message}"
        return
    fi

    /usr/bin/unzip -o -j  ${quiet} ${csv_product}.zip '*/GeoLite2-Country-Blocks*'
    /usr/bin/cat GeoLite2-Country-Blocks-IPv4.csv GeoLite2-Country-Blocks-IPv6.csv | GeoLite2xtables/20_convert_geolite2 countryInfo.txt > GeoIP-legacy.csv

    /usr/bin/mkdir -p "/usr/share/xt_geoip"
    /usr/lib/xtables-addons/xt_geoip_build -D "/usr/share/xt_geoip/" ${database_directory}/GeoIP-legacy.csv

    /usr/bin/rm -f GeoLite2-Country-Blocks-IPv4.csv
    /usr/bin/rm -f GeoLite2-Country-Blocks-IPv6.csv
    /usr/bin/rm -f GeoIP-legacy.csv
    popd
}

# ---------------------------------------------------------------------------
# myecho
# ---------------------------------------------------------------------------
myecho()
{
    if ! ${quietflag:-false}
    then
        echo ${@}
    fi    
}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    while [ ${#} -ne 0 ]
    do
        case "${1}" in
            --quiet)
            quietflag=true
            silent='-s'
            quiet='-q'
            shift
            ;;
        esac
    done

    license_key=$(conf_value 'LicenseKey')
    database_directory='/var/lib/xtables-geoip'
    account_id=$(conf_value 'AccountID')

    download_geodata_csv GeoLite2-Country-CSV
    geolite_to_legacy    GeoLite2-Country-CSV
}

# ----------------------------------------------------------------------------
# call function main
# ----------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
#----------------------------------------------------------------------------