#!/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.
#

# TODO: add AFC, CDDA, DNSSD, GOA, GOOGLE, GPHOTO2, MTP, NFS, SMB, UDISKS2, KEYRING, LOGIND, USB

PKGNAME=gvfs
PKGVERSION=1.40.2
PKGREVISION=1
PKGLICENSE="LGPLv20"
PKGSUMMARY="GNOME virtual file system"
PKGDOWNLOAD="https://download.gnome.org/sources/$PKGNAME/${PKGVERSION%.*}/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="glib meson:build"
PKGOPTIONS="ADMIN
            AFP
            BLURAY
            CRYPTO
            FUSE
            GCR
            HTTP
            KEYRING
            LIBARCHIVE
            SSH
            UDEV"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${ADMIN:=yes}
: ${AFP:=yes}
: ${BLURAY:=yes}
: ${CRYPTO:=yes}
: ${FUSE:=yes}
: ${GCR:=yes}
: ${HTTP:=yes}
: ${KEYRING:=yes}
: ${LIBARCHIVE:=yes}
: ${SSH:=yes}
: ${UDEV:=yes}

if [ "$ADMIN" = "yes" ]; then
	PKGDEPENDS="libcap polkit $PKGDEPENDS"
	with_admin="-D admin=true"
else
	with_admin="-D admin=false"
fi

if [ "$AFP" = "yes" ]; then
	with_afp="-D afp=true"
else
	with_afp="-D afp=false"
fi

if [ "$BLURAY" = "yes" ]; then
	PKGDEPENDS="libbluray $PKGDEPENDS"
	with_bluray="-D bluray=true"
else
	with_bluray="-D bluray=false"
fi

if [ "$CRYPTO" = "yes" ]; then
	PKGDEPENDS="libgcrypt $PKGDEPENDS"
	with_crypto="-D gcrypt=true"
else
	with_crypto="-D gcrypt=false"
fi

if [ "$FUSE" = "yes" ]; then
	PKGDEPENDS="fuse2 $PKGDEPENDS"
	with_fuse="-D fuse=true"
else
	with_fuse="-D fuse=false"
fi

if [ "$GCR" = "yes" ]; then
	PKGDEPENDS="gcr $PKGDEPENDS"
	with_gcr="-D gcr=true"
else
	with_gcr="-D gcr=false"
fi

if [ "$HTTP" = "yes" ]; then
	PKGDEPENDS="libsoup libxml2 $PKGDEPENDS"
	with_http="-D http=true"
else
	with_http="-D http=false"
fi

if [ "$KEYRING" = "yes" ]; then
	PKGDEPENDS="libsecret $PKGDEPENDS"
	with_keyring="-D keyring=true"
else
	with_keyring="-D keyring=false"
fi

if [ "$LIBARCHIVE" = "yes" ]; then
	PKGDEPENDS="libarchive $PKGDEPENDS"
	with_archive="-D archive=true"
else
	with_archive="-D archive=false"
fi

if [ "$SSH" = "yes" ]; then
	PKGDEPENDS="openssh $PKGDEPENDS"
	with_ssh="-D sftp=true"
else
	with_ssh="-D sftp=false"
fi

if [ "$UDEV" = "yes" ]; then
	PKGDEPENDS="libgudev $PKGDEPENDS"
	with_udev="-D gudev=true"
else
	with_udev="-D gudev=false"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.xz
cd $PKGNAME-$PKGVERSION

CC="$CC" \
CFLAGS="$CFLAGS" \
CXX="$CXX" \
CXXFLAGS="$CXXFLAGS" \
LDFLAGS="$LDFLAGS" \
meson . build \
	--prefix / \
	--buildtype release \
	--default-library shared \
	-D afc=false \
	-D cdda=false \
	-D dnssd=false \
	-D goa=false \
	-D google=false \
	-D gphoto2=false \
	-D logind=false \
	-D mtp=false \
	-D nfs=false \
	-D smb=false \
	-D systemduserunitdir=no \
	-D tmpfilesdir=no \
	-D udisks2=false \
	-D libusb=false \
	$with_admin \
	$with_afp \
	$with_bluray \
	$with_crypto \
	$with_fuse \
	$with_gcr \
	$with_http \
	$with_keyring \
	$with_libarchive \
	$with_ssh \
	$with_udev
ninja -C build
DESTDIR=$DESTDIR ninja -C build install

cd ..
rm -rf $PKGNAME-$PKGVERSION
