# Copyright     : Obarun
#------------------------
# Maintainer    : Eric Vidal <eric@obarun.org>
# Maintainer    : Jean-Michel T.Dydak <jean-michel@obarun.org>
#----------------
# Obarun PkgSrc : https://git.obarun.org/pkg/obcore/nfs-utils
#--------------------------------------------------------------
# DESCRIPTION ]

pkgbase=nfs-utils
pkgver=2.6.1
pkgrel=2
pkgdesc="Support programs for Network File Systems"
url='https://nfs.sourceforge.net'

pkgname=(
    'nfs-utils'
    'nfsidmap'
)

track="$pkgname"
target="$pkgname-$pkgver"
source=(
    "https://www.kernel.org/pub/linux/utils/${track}/${pkgver}/${target}.tar".{xz,sign}
    exports
)

#----------------------
# BUILD CONFIGURATION ]

makedepends=(
    'libevent'
    'sqlite'
    'device-mapper'
    'rpcsvc-proto'
)

#----------------
# BUILD PREPARE ]

prepare() {
    cd $pkgbase-$pkgver

    ## fix hardcoded sbin path to our needs
    sed -i "s|sbindir = /sbin|sbindir = /usr/bin|g" utils/*/Makefile.am
    sed -i "s|sbin|bin|" utils/nfsidmap/id_resolver.conf
    autoreconf -vfi
}

#----------------
# BUILD CONTROL ]

_flags=(
    --prefix=/usr
    --sbindir=/usr/bin
    --sysconfdir=/etc
    --enable-gss
    --without-tcp-wrappers
    --with-statedir=/var/lib/nfs
    --enable-ipv6
    --enable-libmount-mount
    --enable-mountconfig
    --with-start-statd=/usr/bin/start-statd
    --with-systemd=no
    --enable-nfsv4server
)

#--------
# BUILD ]

build() {
    cd $pkgbase-$pkgver

    ./configure "${_flags[@]}"
    sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
    make
}

#--------
# CHECK ]

check() {
    cd $pkgbase-$pkgver
    make -k check || /bin/true
}

#----------
# PACKAGE ]

package_nfs-utils() {
    pkgdesc='Support programs for Network File Systems'
    license=(GPL2)

    depends=(
        'rpcbind'
        'nfsidmap'
        'gssproxy'
        'libevent'
        'device-mapper'
    )
    optdepends=(
        'sqlite: for nfsdcltrack usage'
        'python: for nfsiostat, nfsdclnts and mountstats usage'
    )
    backup=(
        'etc/exports'
        'etc/nfs.conf'
        'etc/nfsmount.conf'
        'var/lib/nfs/etab'
        'var/lib/nfs/rmtab'
    )

    cd $pkgbase-$pkgver

    make DESTDIR="$pkgdir" install

    install -D -m 644 utils/mount/nfsmount.conf "$pkgdir"/etc/nfsmount.conf
    install -D -m 644 nfs.conf "$pkgdir"/etc/nfs.conf

    install -d -m 755 "$pkgdir"/usr/share/doc/$pkgname
    install -m 644 {NEWS,README} "$pkgdir"/usr/share/doc/$pkgname/

    install -D -m 644 ../exports "$pkgdir"/etc/exports
    install -D -m 644 utils/nfsidmap/id_resolver.conf "$pkgdir"/etc/request-key.d/id_resolver.conf

    mkdir "$pkgdir"/etc/exports.d
    mkdir -m 555 "$pkgdir"/var/lib/nfs/rpc_pipefs
    mkdir "$pkgdir"/var/lib/nfs/v4recovery

    ## nfsidmap cleanup
    rm -vrf $pkgdir/usr/include #/nfsid*
    rm -vrf $pkgdir/usr/lib/libnfsidmap*
    rm -vrf $pkgdir/usr/lib/pkgconfig #/libnfsidmap.pc
    rm -v $pkgdir/usr/share/man/{man3/nfs4_uid_to_name*,man5/idmapd.conf*}
    rm -rf $pkgdir/usr/share/man/man3
}

package_nfsidmap() {
    pkgdesc='Library to help mapping IDs, mainly for NFSv4'
    license=(GPL2)

    depends=(
        'libldap'
        'krb5'
    )
    backup=(
        'etc/idmapd.conf'
    )

    cd $pkgbase-$pkgver

    make -C support  DESTDIR="$pkgdir" install

    install -D -m 644 support/nfsidmap/idmapd.conf "$pkgdir"/etc/idmapd.conf
    install -Dm644 support/nfsidmap/COPYING $pkgdir/usr/share/licenses/nfsidmap/LICENSE
}

#-------------------------
# LICENCE AND VALIDATION ]

arch=(x86_64)

validpgpkeys=(
    'E1B71E339E20A10A676F7CB69AFB1D681A125177' # Steve Dickson <steved@redhat.com>
)

sha256sums=('60dfcd94a9f3d72a12bc7058d811787ec87a6d593d70da2123faf9aad3d7a1df'
            'SKIP'
            'd4862c011896371acba8a674be9942c9fa226a683f2fb11f62cebc5fd9f38df2')
