# Build recipe for libid3tag.
#
# Copyright (c) 2018 Markus Tornow, <tornow@riseup.net>.
# Copyright (c) 2018 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=libid3tag
version=0.15.1b
release=1

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

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

# Remote source(s)
fetch=ftp://ftp.mars.org/pub/mpeg/$tarname

description="
ID3 tag manipulation library.
  
libid3tag is a library for reading and (eventually) writing ID3 tags,
both ID3v1 and the various versions of ID3v2.
"

homepage=https://www.underbit.com/products/mad/
license=GPLv2+

# Source documentation
docs="CHANGES COPYING COPYRIGHT CREDITS README TODO VERSION"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

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

    # Apply fixes since this release is from 2004 (Thanks to Fedora and Debian)
    patch -p0 < "${worktree}/patches/libid3tag/libid3tag-0.15.1b-fix_overflow.patch"
    patch -p1 < "${worktree}/patches/libid3tag/libid3tag-0.15.1b-id3v1-zero-padding.patch"
    patch -p1 < "${worktree}/patches/libid3tag/libid3tag-0.15.1b-handle-unknown-encoding.patch"
    patch -p0 < "${worktree}/patches/libid3tag/libid3tag-0.15.1b-id3v2-endless-loop.patch"
    patch -p1 < "${worktree}/patches/libid3tag/libid3tag-0.15.1b-gperf-size_t.patch"

    touch NEWS AUTHORS ChangeLog

    # Force these files to be regenerated from the .gperf sources
    rm -f compat.c frametype.c

    autoreconf -vif

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
    $configure_args \
    --libdir=/usr/lib${libSuffix} \
    --enable-static=no \
    --enable-shared=yes \
    --disable-debugging \
    --build="$(gcc -dumpmachine)"

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

    strip --strip-unneeded "${destdir}/usr/lib${libSuffix}/"libid3tag.so.0.?.0

    # Write missing .pc for pkgconf
    mkdir -p "${destdir}/usr/lib${libSuffix}/pkgconfig"
    cat << EOF > "${destdir}/usr/lib${libSuffix}/pkgconfig/id3tag.pc"
prefix=/usr
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib${libSuffix}
includedir=\${prefix}/include

Name: id3tag
Description: ID3 tag manipulation library
Version: ${version}
Requires:
Libs: -L\${libdir} -lid3tag -lz
Cflags: -I\${includedir}

EOF

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

