#!/bin/bash

# Slackware build script for GeoIP
#
#########################
#
# 2022-08-22 - Updated for aarch64 and to include GeoIP Update
# Exaga - sarpi.penthux.net
#
# Original script: https://mailfud.org/geoip-legacy/geoip_update.sh
#
# For GeoIP, keeping up to date is prudent and the script above 
# facilitates and [optionally] automates this process. The script 
# code included in this PKG has been modified to include logging 
# the GeoIP install and DB update process.
#
# Note:
# After installing this package, 'geoipupdate' will be available as a 
# command, and can be found here:
#
#   /usr/bin/geoipupdate
#
# geoipupdate can be symlinked in /etc/cron.{hourly,daily,weekly,monthly}
# to make the GeoIP DB update process completely automated. It's up to
# the user (i.e. YOU) to decide whether or not to automate this process. 
# As an example, if automated weekly updates were required then it could 
# be achieved like this:
#
#   ln -s /usr/bin/geoipupdate /etc/cron.weekly/geoipupdate
#
# Alternatively, copy the shell script directly to avoid using symlinks:
#
#   cp -a /usr/bin/geoipupdate /etc/cron.weekly/
#
# No other GeoIP functions or features are affected; geoipupdate only
# updates the GeoIP databases, at location set in the geoip_update.sh 
# script by the DBDIR variable:
#
#   DBDIR=/usr/share/GeoIP
#
# This script can be run with the 'MINIDATA=yes' option that will only
# include the GeoIP and GeoIPv6 databases. It's minimal in terms of data
# but if that's what's required then run it like this:
#
#   MINIDATA=yes ./GeoIP.SlackBuild
#
#########################
#
# Copyright 2007-2010 Yalla-One <yallaone@gmail.com>
# Copyright 2011 markus reichelt <slackbuilds@mareichelt.de> 0xCCEEF115
# Copyright 2011 Niels Horn, Rio de Janeiro, RJ, Brazil
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS 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 AUTHOR 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.

# This product includes GeoLite data created by MaxMind,
# available from http://www.maxmind.com
# (Creative Commons Attribution-ShareAlike 4.0 International License)

# Maintained by David Spencer <baildon.research@googlemail.com>

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=GeoIP
VERSION=${VERSION:-1.6.12}
BUILD=${BUILD:-1}
TAG="${TAG:-_slackcurrent_sp1}"
PKGTYPE="${PKGTYPE:-txz}"

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    aarch64) ARCH=aarch64 ;;
    *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# We need to update the configuration files in this tarball...
autoreconf -i

# configure
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --datadir=/usr/share \
  --localstatedir=/var \
  --mandir=/usr/man \
  --disable-static \
  --build=$ARCH-unknown-linux

# build
make -j$(nproc)

# install to PKG dir
make install DESTDIR=$PKG

# strip unwanted PKG shizzle
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
  
# gzip the man files  
find $PKG/usr/man -type f -exec gzip -9 {} \;

# only include GeoIP[v6] DBs [usage]: MINIDATA=yes ./GeoIP.SlackBuild
mkdir -p $PKG/var/lib/${PRGNAM}
if [ "${MINIDATA:-no}" = "yes" ]; then
  DATAFILES="GeoIP GeoIPv6"
else
  DATAFILES="GeoIP GeoIPv6 GeoLiteCity GeoLiteCityv6 GeoIPASNum GeoIPASNumv6 GeoIPOrg GeoIPISP"
fi
for datafile in $DATAFILES ; do
  if [ -e $CWD/$datafile.dat.gz ]; then
    zcat $CWD/$datafile.dat.gz > $PKG/var/lib/${PRGNAM}/$datafile.dat
  fi
done
# As an absolute minimum we'll package the ipv4 country data file from the source tree:
if [ ! -e $PKG/var/lib/${PRGNAM}/${PRGNAM}.dat ]; then
  cat data/${PRGNAM}.dat > $PKG/var/lib/${PRGNAM}/GeoIP.dat
fi
# create symlink for GeoIP database dir
mkdir -p $PKG/usr/share
ln -s /var/lib/${PRGNAM} $PKG/usr/share/${PRGNAM}

# create command file and cron job symlink
mkdir -p $PKG/usr/bin
cat $CWD/geoip_update.sh > $PKG/usr/bin/geoipupdate
chmod +x $PKG/usr/bin/geoipupdate

# create a cron job file
#mkdir -p $PKG/etc/cron.weekly 
#ln -s /usr/bin/geoipupdate $PKG/etc/cron.weekly/geoipupdate

# Alternative cron job file [use shell script]
#cat $CWD/geoip_update.sh > $PKG/etc/cron.weekly/geoipupdate
# make cron job file executable 
#chmod +x $PKG/etc/cron.weekly/geoipupdate

# copy required docs and SBo 
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  AUTHORS COPYING ChangeLog INSTALL LICENSE NEWS.md README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# remove unwanted PKG files
rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/*.la

# create PKG install files
mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
sed -Ei "s/@PRGVERS@/$PRGNAM-$VERSION-${ARCH}/g" $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# build the PKG and md5 files
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-${ARCH}-$BUILD$TAG.${PKGTYPE:-txz}
cd $OUTPUT
/bin/md5sum $PRGNAM-$VERSION-${ARCH}-$BUILD$TAG.${PKGTYPE:-txz} > $OUTPUT/$PRGNAM-$VERSION-${ARCH}-$BUILD$TAG.md5

#EOF<*>

