#!/bin/busybox sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or 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.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=irccd
PKGVERSION=3.0.1
PKGREVISION=1
PKGLICENSE="ISC"
PKGSUMMARY="an IRC bot written in C++17"
PKGDOWNLOAD="http://releases.malikania.fr/$PKGNAME/$PKGVERSION/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="cmake:build doxygen:build boost"
PKGUIDS="irccd:109"
PKGGIDS="irccd:109"
PKGOPTIONS="ASK
            AUTH
            HANGMAN
            HISTORY
            JAVASCRIPT
            JOKE
            LIBEDIT
            LINKS
            LOGGER
            PLUGIN
            ROULETTE
            SSL
            TICTACTOE"
PKGPROTECT="etc/irccd.conf
            etc/irccdctl.conf
            etc/rc.d/irccd"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${ASK:=yes}
: ${AUTH:=yes}
: ${HANGMAN:=yes}
: ${HISTORY:=yes}
: ${JAVASCRIPT:=yes}
: ${JOKE:=yes}
: ${LIBEDIT:=yes}
: ${LINKS:=yes}
: ${LOGGER:=yes}
: ${PLUGIN:=yes}
: ${ROULETTE:=yes}
: ${SSL:=yes}
: ${TICTACTOE:=yes}

for var in AUTH HANGMAN HISTORY JOKE LINKS LOGGER PLUGIN ROULETTE TICTACTOE; do
	option=$(echo $var | tr '[A-Z]' '[a-z]')

	eval enabled=\$$var

	if [ "$enabled" = "yes" ]; then
		# The plugin links require uriparser.
		if [ "$var" = "LINKS" ]; then
			PKGDEPENDS="uriparser $PKGDEPENDS"
		fi

		eval with_$option="-DIRCCD_WITH_PLUGIN_$var=On"
	else
		eval with_$option="-DIRCCD_WITH_PLUGIN_$var=Off"
	fi
done

if [ "$JAVASCRIPT" = "yes" ]; then
	with_javascript="-DIRCCD_WITH_JS=On"
else
	with_javascript="-DIRCCD_WITH_JS=Off"
fi

if [ "$LIBEDIT" = "yes" ]; then
	PKGDEPENDS="libedit $PKGDEPENDS"
	with_libedit="-DIRCCD_WITH_LIBEDIT=On"
else
	with_libedit="-DIRCCD_WITH_LIBEDIT=Off"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="-DIRCCD_WITH_SSL=On"
else
	with_ssl="-DIRCCD_WITH_SSL=Off"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.xz
	cd $PKGNAME-$PKGVERSION

	mkdir build && cd build
	cmake .. \
		-DBUILD_SHARED_LIBS=On \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_COMPILER="$CXX" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_COMPILER="$CC" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DCMAKE_INSTALL_PREFIX= \
		-DIRCCD_WITH_SYSTEMD=Off \
		$with_ask \
		$with_hangman \
		$with_history \
		$with_javascript \
		$with_joke \
		$with_libedit \
		$with_links \
		$with_logger \
		$with_plugin \
		$with_roulette \
		$with_ssl \
		$with_tictactoe
	make VERBOSE=1
	make install DESTDIR=$DESTDIR
	cd ..
	install -Dm0644 ../irccd $DESTDIR/etc/rc.d/irccd
	mv $DESTDIR/etc/irccd.conf.sample $DESTDIR/etc/irccd.conf
	mv $DESTDIR/etc/irccdctl.conf.sample $DESTDIR/etc/irccdctl.conf

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
