# Build recipe for libgphoto2.
#
# Copyright (c) 2022 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.

# Exit immediately on any error
set -e

program=libgphoto2
version=2.5.30
release=1

# Define a category for the output of the package name
pkgcategory=media

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

# Remote source(s)
fetch=https://sourceforge.net/projects/gphoto/files/libgphoto/${version}/$tarname

homepage=http://www.gphoto.org/

description="
Library to access digital cameras.

libgphoto2 is a library that can be used by applications to
access various digital cameras.

For more information on gphoto, see $homepage
"

license="GPLv2+, LGPLv2.1"

# Source documentation
docs="";	# This build system already installs the documentation.
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

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

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-shared=yes \
     --with-libcurl=auto \
     --with-libexif=auto \
     --build="$(gcc -dumpmachine)" \
     utilsdir=/usr/bin \
     udevscriptdir=/lib/udev

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Make and install udev rules

    mkdir -p "${destdir}/lib/udev/rules.d" "${destdir}/lib/udev/hwdb.d"

    cd packaging/generic

    LD_LIBRARY_PATH="${destdir}/usr/lib${libSuffix}"
    CAMLIBS="${destdir}/usr/lib${libSuffix}/${program}/${version}"
    LIBDIR="${destdir}/usr/lib${libSuffix}"

    export LD_LIBRARY_PATH CAMLIBS LIBDIR

    "${destdir}"/usr/bin/print-camera-list udev-rules version 201 mode 0660 owner root group plugdev \
     > "${destdir}/lib/udev/rules.d/40-libgphoto2.rules"

    "${destdir}"/usr/bin/print-camera-list hwdb \
     > "${destdir}/lib/udev/hwdb.d/20-libgphoto2.hwdb"

    unset -v LD_LIBRARY_PATH CAMLIBS LIBDIR

    # 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

    # Remove unwanted documentation
    (
        cd "${destdir}/$docsdir"
        rm -f RELEASE-HOWTO.md README.packaging ABOUT-NLS
    )   
}

