#!/bin/sh
#
# Copyright (c) 2019-2021 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=rsync
PKGVERSION=3.2.3
PKGREVISION=1
PKGLICENSE="GPLv30"
PKGSUMMARY="versatile copy tool"
PKGWWW="https://rsync.samba.org"
PKGDOWNLOAD="https://download.samba.org/pub/$PKGNAME/src/$PKGNAME-$PKGVERSION.tar.gz"
PKGDEPENDS="acl attr popt zlib"
PKGOPTIONS="IPV6 LZ4 SSL ZSTD"
PKGPROTECT="etc/rsyncd.conf"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${IPV6:=yes}
: ${LZ4:=yes}
: ${SSL:=yes}
: ${ZSTD:=yes}

if [ "$IPV6" = "yes" ]; then
	with_ipv6="--enable-ipv6"
else
	with_ipv6="--disable-ipv6"
fi

if [ "$LZ4" = "yes" ]; then
	PKGDEPENDS="lz4 $PKGDEPENDS"
	with_lz4="--enable-lz4"
else
	with_lz4="--disable-lz4"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--enable-openssl"
else
	with_ssl="--disable-openssl"
fi

if [ "$ZSTD" = "yes" ]; then
	PKGDEPENDS="zstd $PKGDEPENDS"
	with_zstd="--enable-zstd"
else
	with_zstd="--disable-zstd"
fi

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

	# --disable-xxhash: not available yet.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--disable-xxhash \
		--without-included-popt \
		--without-included-zlib \
		$with_ipv6 \
		$with_lz4 \
		$with_ssl \
		$with_zstd
	make
	make install DESTDIR=$DESTDIR

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