#!/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=curl
PKGVERSION=7.64.0
PKGREVISION=1
PKGLICENSE="MIT"
PKGSUMMARY="command line tool for transferring data with URLs"
PKGDOWNLOAD="https://curl.haxx.se/download/$PKGNAME-$PKGVERSION.tar.xz"
PKGOPTIONS="DICT
            FILE
            FTP
            GOPHER
            HTTP
            HTTP2
            IDN2
            IMAP
            IPV6
            LDAP
            LDAPS
            POP
            PROXY
            PSL
            RTSP
            SMB
            SMTP
            SSH
            SSL
            TELNET
            TFTP
            ZLIB
            "

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${DICT:=yes}
: ${FILE:=yes}
: ${FTP:=yes}
: ${GOPHER:=yes}
: ${HTTP2:=yes}
: ${HTTP:=yes}
: ${IDN2:=yes}
: ${IMAP:=yes}
: ${IPV6:=yes}
: ${LDAP:=yes}
: ${LDAPS:=yes}
: ${POP:=yes}
: ${PROXY:=yes}
: ${PSL:=yes}
: ${RTSP:=yes}
: ${SMB:=yes}
: ${SMTP:=yes}
: ${SSH:=yes}
: ${SSL:=yes}
: ${TELNET:=yes}
: ${TFTP:=yes}
: ${ZLIB:=yes}

if [ "$DICT" = "yes" ]; then
	with_dict="--enable-dict"
else
	with_dict="--disable-dict"
fi

if [ "$FILE" = "yes" ]; then
	with_file="--enable-file"
else
	with_file="--disable-file"
fi

if [ "$FTP" = "yes" ]; then
	with_ftp="--enable-ftp"
else
	with_ftp="--disable-ftp"
fi

if [ "$GOPHER" = "yes" ]; then
	with_gopher="--enable-gopher"
else
	with_gopher="--disable-gopher"
fi

if [ "$HTTP" = "yes" ]; then
	with_http="--enable-http"
else
	with_http="--disable-http"
fi

if [ "$IDN2" = "yes" ]; then
	PKGDEPENDS="libidn2 $PKGDEPENDS"
	with_idn2="--with-libidn2"
else
	with_idn2="--without-libidn2"
fi

if [ "$IMAP" = "yes" ]; then
	with_imap="--enable-imap"
else
	with_imap="--disable-imap"
fi

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

if [ "$LDAP" = "yes" ]; then
	with_ldap="--enable-ldap"
else
	with_ldap="--disable-ldap"
fi

if [ "$LDAPS" = "yes" ]; then
	with_ldaps="--enable-ldaps"
else
	with_ldaps="--disable-ldaps"
fi

if [ "$HTTP2" = "yes" ]; then
	PKGDEPENDS="nghttp2 $PKGDEPENDS"
	with_http2="--with-nghttp2"
else
	with_http2="--without-nghttp2"
fi

if [ "$POP" = "yes" ]; then
	with_pop="--enable-pop3"
else
	with_pop="--disable-pop3"
fi

if [ "$PROXY" = "yes" ]; then
	with_proxy="--enable-proxy"
else
	with_proxy="--disable-proxy"
fi

if [ "$RTSP" = "yes" ]; then
	with_rtsp="--enable-rtsp"
else
	with_rtsp="--disable-rtsp"
fi

if [ "$SMB" = "yes" ]; then
	with_smb="--enable-smb"
else
	with_smb="--disable-smb"
fi

if [ "$SMTP" = "yes" ]; then
	with_smtp="--enable-smtp"
else
	with_smtp="--disable-smtp"
fi

if [ "$SSH" = "yes" ]; then
	PKGDEPENDS="libssh2 $PKGDEPENDS"
	with_ssh="--with-libssh2"
else
	with_ssh="--without-libssh2"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--with-ssl"
else
	with_ssl="--without-ssl"
fi

if [ "$PSL" = "yes" ]; then
	PKGDEPENDS="libpsl $PKGDEPENDS"
	with_psl="--with-libpsl"
else
	with_psl="--without-libpsl"
fi

if [ "$TELNET" = "yes" ]; then
	with_telnet="--enable-telnet"
else
	with_telnet="--disable-telnet"
fi

if [ "$TFTP" = "yes" ]; then
	with_tftp="--enable-tftp"
else
	with_tftp="--disable-tftp"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
	with_zlib="--with-zlib"
else
	with_zlib="--without-zlib"
fi

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

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--disable-debug \
		--enable-shared \
		--enable-optimize \
		--with-ca-bundle=/etc/ssl/cert.pem \
		--without-cyassl \
		--without-gnutls \
		--without-mbedtls \
		--without-polarssl \
		--without-wolfssl \
		$with_dict \
		$with_file \
		$with_ftp \
		$with_gopher \
		$with_http \
		$with_http2 \
		$with_idn2 \
		$with_imap \
		$with_ipv6 \
		$with_ldap \
		$with_ldaps \
		$with_pop \
		$with_proxy \
		$with_psl \
		$with_rtsp \
		$with_smb \
		$with_smtp \
		$with_ssh \
		$with_ssl \
		$with_telnet \
		$with_tftp \
		$with_zlib
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

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