#!/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=pinentry
PKGVERSION=1.1.0
PKGREVISION=1
PKGLICENSE="GPL-2.0-only"
PKGSUMMARY="collection of PIN or passphrase entry dialogs"
PKGWWW="https://gnupg.org/index.html"
PKGDOWNLOAD="https://gnupg.org/ftp/gcrypt/$PKGNAME/$PKGNAME-$PKGVERSION.tar.bz2"
PKGDEPENDS="libassuan"
PKGOPTIONS="FLTK GTK LIBSECRET NCURSES QT"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${FLTK:=no}
: ${GTK:=yes}
: ${LIBSECRET:=yes}
: ${NCURSES:=yes}
: ${QT:=yes}

if [ "$FLTK" = "yes" ]; then
	PKGDEPENDS="fltk $PKGDEPENDS"
	with_fltk="--enable-pinentry-fltk"
else
	with_fltk="--disable-pinentry-fltk"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
	with_gtk="--enable-pinentry-gnome3"
else
	with_gtk="--disable-pinentry-gnome3"
fi

if [ "$LIBSECRET" = "yes" ]; then
	PKGDEPENDS="libsecret $PKGDEPENDS"
	with_libsecret="--enable-libsecret"
else
	with_libsecret="--disable-libsecret"
fi

if [ "$NCURSES" = "yes" ]; then
	PKGDEPENDS="ncurses $PKGDEPENDS"
	with_ncurses="--enable-pinentry-curses --enable-fallback-curses"
else
	with_ncurses="--disable-pinentry-curses --disable-fallback-curses"
fi

if [ "$QT" = "yes" ]; then
	PKGDEPENDS="qtbase $PKGDEPENDS"
	with_qt="--enable-pinentry-qt5"
else
	with_qt="--disable-pinentry-qt5"
fi
	
build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar -xvf $PKGNAME-$PKGVERSION.tar.bz2
	cd $PKGNAME-$PKGVERSION

	# --disable-pinentry-gtk2: Gtk 2 is deprecated in Vanilla Linux.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--disable-pinentry-gtk2 \
		$with_fltk \
		$with_gtk \
		$with_libsecret \
		$with_ncurses \
		$with_qt
	make
	make install DESTDIR=$DESTDIR

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