#!/bin/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=fossil
PKGVERSION=2.10
PKGREVISION=1
PKGLICENSE="BSD2CLAUSE"
PKGSUMMARY="simple distributed software configuration management system"
PKGDOWNLOAD="https://fossil-scm.org/home/uv/$PKGNAME-src-$PKGVERSION.tar.gz"
PKGDEPENDS="tcl:dev zlib"
PKGOPTIONS="FUSE JSON SSL STATIC"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${FUSE:=no}
: ${JSON:=yes}
: ${STATIC:=no}
: ${SSL:=yes}

if [ "$FUSE" = "yes" ]; then
	PKGDEPENDS="fuse2 $PKGDEPENDS"
else
	with_fuse="--disable-fusefs"
fi

if [ "$JSON" = "yes" ]; then
	with_json="--json"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--with-openssl=auto"
else
	with_ssl="--with-openssl=none"
fi

# TODO: libressl does not ship static libraries yet.
if [ "$STATIC" = "yes" ]; then
	with_static="--static"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-src-$PKGVERSION.tar.gz
cd $PKGNAME-$PKGVERSION

# CXX=$CC: fool C++ compiler, there is no C++ file in fossil.
CC="$CC" \
CC_FOR_BUILD="$CC" \
CXX="$CC" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
./configure \
	--prefix=/usr \
	$with_fuse \
	$with_json \
	$with_ssl \
	$with_static
make BCC=$CC TCC=$CC
install -Dm0755 fossil $DESTDIR/bin/fossil
install -Dm0644 fossil.1 $DESTDIR/share/man/man1/fossil.1

cd ..
rm -rf $PKGNAME-$PKGVERSION
