#!/bin/sh
#
# Heavily based on the Slackware 12.2 SlackBuild
# Slackware build script for I2P
#
# Packages are generally prohibited from being updated outside
# of the package manager; this I2P SlackBuild is no different.
#
# If you'd like to use the I2P "in-network" updates anyway, you'll need to
# grant the user that I2P will run as write permission to the installation directory
# (/opt/i2p by default).
#
# For safety's sake, a user's I2P config files will *never* be overwritten by any upgrade method.
# In the future this SlackBuild may alert when a default config file is updated.
##

# Make sure makepkg and friends can be found
PATH=$PATH:/sbin

# abort on error and unset variables (same as set -e and set -u, respectively)
set -o errexit
set -o nounset

if [ $(id -ur) -ne 0 ]; then
    echo "ERROR: SlackBuilds require root access." >&2
    exit 1
fi

BUILD=1kytv
# INSTALL_DIR is referenced from /, don't prefix it with a '/'
INSTALL_DIR=opt
NAME=i2p
ARCH=noarch

CWD=$(readlink -m $(dirname $0))
I2PSRC=$(readlink -m $CWD/../../)
TMP=/tmp
PKG=$TMP/package-i2p

if [ -e "/etc/slackware-version" ]; then
    # Older than Slackware 13?
    SLACKVER=$(sed -e "s/Slackware\s\+\([0-9]\+\)\.\?\([0-9]\+\)\?/\1/" /etc/slackware-version)
    if [ $SLACKVER -lt 13 ] ; then
        EXT=tgz
    else
        EXT=txz
    fi
else
    echo "ERROR: This script is only intended for use on Slackware systems.">&2
    exit 1
fi

# Extract version strings
I2PBUILD=$(sed -e '/^.\+long\s\+BUILD/!d' -e 's/^.\+long\s\+BUILD\s\+=\s\+\([0-9]\+\);/\1/' $I2PSRC/router/java/src/net/i2p/router/RouterVersion.java)
# Thanks to user "ihavei2p" for the second awk statement
# If the version is x.y, it'll be set to x.y.0. Otherwise the version string will be unchanged
CORE=$(awk -F'"' '/static\s+String\s+VERSION/{print $2}' $I2PSRC/core/java/src/net/i2p/CoreVersion.java | \
        awk -F. '{ if (NF > 3) { print; exit } else if ($3 == "") { $3=0; print $1"."$2"."$3 } else print }')
VERSION="${CORE}_${I2PBUILD}"

[ -d $PKG ] && rm -rf $PKG
mkdir -p $PKG/$INSTALL_DIR $PKG/install

cd "$I2PSRC"
ant distclean
ant jbigi-linux-x86-only preppkg-unix

chown -R root:root $I2PSRC/pkg-temp
cp -a $I2PSRC/pkg-temp $PKG/$INSTALL_DIR/i2p

# $INSTALL_DIR is used by this SlackBuild.
# [%$]INSTALL_PATH , [%$]SYSTEM_java_io_tmpdir, and [%$]USER_HOME have the correct paths set
# by the IzPack installer.
cd $PKG/$INSTALL_DIR/i2p
for file in wrapper.config eepget i2prouter runplain.sh; do
    sed -i "s|[%$]INSTALL_PATH|/$INSTALL_DIR/i2p|g;s|[$%]SYSTEM_java_io_tmpdir|$TMP|g;s/[%$]USER_HOME/\$HOME/g" $file
done
mv wrapper.config wrapper.config.new

install -d $PKG/usr/bin
install -d $PKG/usr/doc/$NAME-$VERSION
install -d $PKG/etc/rc.d
mv licenses LICENSE.txt -t $PKG/usr/doc/$NAME-$VERSION

# runplain.sh will live in the installation directory. eepget and i2prouter will go to /usr/bin
# with symlinks in INST_DIR (created in doinst.sh)
install -m755 i2prouter $PKG/usr/bin
install -m755 eepget $PKG/usr/bin
chmod 755  ./runplain.sh

if [ $INSTALL_DIR != 'opt' ]; then
    sed "s|\(The I2P package\)\s\+will be\s\+\(installed to\).+|\1 has been \2 $INSTALL_DIR/i2p|g" $CWD/README > $PKG/usr/doc/$NAME-$VERSION/README
else
    sed "s|will be installed|has been installed|" $CWD/README > $PKG/usr/doc/$NAME-$VERSION/README
fi

install -d $PKG/usr/man/man1
gzip -9 man/*.1
install -m644 man/*.1.gz $PKG/usr/man/man1
rm -rf ./man

# We install all x86 wrapper binaries.
# The i2prouter script will try to determine the OS (linux), the bits (32 VS 64) and should be able
# to figure out the correct wrapper binary to use.

# However: In case the i2prouter script's detection fails, "$INST_DIR/i2psvc" will point to
# what 'we' think the correct binary is.
#
# A good reason for installing all binaries: in case the user, for whatever reason, switches from an
# x64 JRE to an x86 JRE, I2P should continue to work without needing to be reinstalled.
install -m755 $I2PSRC/installer/lib/wrapper/linux/i2psvc ./i2psvc-linux-x86-32
install -m644 $I2PSRC/installer/lib/wrapper/linux/libwrapper.so ./lib/libwrapper-linux-x86-32.so
install -m755 $I2PSRC/installer/lib/wrapper/linux64/i2psvc ./i2psvc-linux-x86-64
install -m644 $I2PSRC/installer/lib/wrapper/linux64/libwrapper.so ./lib/libwrapper-linux-x86-64.so
install -m644 $I2PSRC/installer/lib/wrapper/all/wrapper.jar ./lib/wrapper.jar
install -m644 $I2PSRC/build/jbigi.jar $PKG/$INSTALL_DIR/i2p/lib/jbigi.jar

rm -f ./postinstall.sh ./osid ./INSTALL-*.txt
sed "s|directory|/$INSTALL_DIR/i2p|" $CWD/doinst.sh > $PKG/install/doinst.sh
sed -i "s|%pkg|$NAME-$VERSION|" $PKG/install/doinst.sh
sed "s|%INST_DIR|/$INSTALL_DIR/i2p|" $CWD/rc.i2p> $PKG/etc/rc.d/rc.i2p.new
cp $CWD/slack-desc $PKG/install/slack-desc

cd $PKG
cp $CWD/slack-required $PKG/install/slack-required
makepkg -l y -c n $CWD/${NAME}-$VERSION-$ARCH-$BUILD.$EXT
