# Build recipe for udevil.
#
# Copyright (c) 2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=udevil
version=20150504_ce9f1c5
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/tools"

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch="
 http://rsync.dragora.org/current/sources/$tarname
 http://mirror.cedia.org.ec/dragora/current/sources/$tarname
"

description="
Mounts and unmounts devices without password.

udevil is a command line GNU/Linux program which mounts and unmounts
removable devices without a password, shows device info, and monitors
device changes.  It can also mount ISO files, nfs://, smb://,
ftp://, ssh:// and WebDAV URLs, and tmpfs/ramfs filesystems.
"

homepage=http://ignorantguru.github.com/udevil/
license=GPLv3+

# Source documentation
docs="AUTHORS COPYING ChangeLog NEWS README"
docsdir="${docdir}/${program}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" config.guess
    cp -f "${worktree}/archive/common/config.sub" config.sub

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --disable-systemd \
     --build="$(cc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} libdir=/usr/lib${libSuffix} \
     DESTDIR="$destdir" install-strip

    # To handle (dot) .new files via graft(1)
    touch "${destdir}/etc/udevil/.graft-config"

    # Restrict execution of udevil to the 'plugdev' group
    chown root:plugdev "${destdir}/usr/bin/udevil"
    chmod u+s,go-s,o-x "${destdir}/usr/bin/udevil"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

