%bcond_without tests

%bcond_without libheif

%if 0%{?flatpak}
%bcond_with perl
%else
%bcond_without perl
%endif

# Disable automatic .la file removal
%global __brp_remove_la_files %nil

%global Version @PACKAGE_BASE_VERSION@
%global Patchlevel @MAGICK_PATCHLEVEL_VERSION@
%global libsover 7
%global libcxxsover 9

Name:           ImageMagick
Epoch:          1
Version:        %{Version}
Release:        %{Patchlevel}
Summary:        Use ImageMagick to create, edit, convert, and display raster image files.

License:        ImageMagick
URL:            https://imagemagick.org/
Source0:        https://imagemagick.org/archive/releases/%{name}-%{Version}-%{Patchlevel}.tar.xz

BuildRequires:  pkgconfig(bzip2)
BuildRequires:  pkgconfig(freetype2)
BuildRequires:  pkgconfig(libjpeg)
BuildRequires:  pkgconfig(libpng)
BuildRequires:  pkgconfig(libtiff-4)
BuildRequires:  giflib-devel
BuildRequires:  pkgconfig(zlib)
%if %{with perl}
BuildRequires:  perl-devel >= 5.8.1
BuildRequires:  perl-generators
%endif
%if 0%{?rhel} && 0%{?rhel} < 8
BuildRequires:  ghostscript-devel
%else
BuildRequires:  libgs-devel
%endif
BuildRequires:  pkgconfig(ddjvuapi)
BuildRequires:  pkgconfig(libwmf)
BuildRequires:  pkgconfig(jasper)
BuildRequires:  libtool-ltdl-devel
BuildRequires:  pkgconfig(x11)
BuildRequires:  pkgconfig(xext)
BuildRequires:  pkgconfig(xt)
BuildRequires:  pkgconfig(lcms2)
BuildRequires:  pkgconfig(libxml-2.0)
BuildRequires:  pkgconfig(librsvg-2.0)
%if 0%{?rhel} && 0%{?rhel} < 9
BuildRequires:  pkgconfig(IlmBase), pkgconfig(OpenEXR) < 2.5.6
%else
BuildRequires:  pkgconfig(OpenEXR)
%endif
BuildRequires:  pkgconfig(fftw3)
BuildRequires:  pkgconfig(libwebp)
BuildRequires:  jbigkit-devel
BuildRequires:  pkgconfig(libjxl)
BuildRequires:  pkgconfig(libopenjp2) >= 2.1.0
BuildRequires:  pkgconfig(libcgraph) >= 2.9.0
BuildRequires:  pkgconfig(raqm)
%if 0%{?fedora} || 0%{?rhel} > 8
BuildRequires:  pkgconfig(lqr-1)
%endif
BuildRequires:  pkgconfig(libraw) >= 0.14.8
BuildRequires:  pkgconfig(libzstd)
BuildRequires:  pkgconfig(libzip) >= 1.0.0
BuildRequires:  pkgconfig(pango) >= 1.28.1
BuildRequires:  pkgconfig(pangocairo) >= 1.28.1
BuildRequires:  urw-base35-fonts-devel
BuildRequires:  autoconf automake gcc gcc-c++
BuildRequires:  make
BuildRequires:  gnupg2
# for doc
BuildRequires:  doxygen

Requires:       %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}
# allow smooth upgrade for 3rd party repository
# providing latest version/soname as ImageMagick7
Obsoletes:      %{name}6            < %{epoch}:%{version}-%{release}
Provides:       %{name}6            = %{epoch}:%{version}-%{release}

%description
ImageMagick is a free and open-source software suite used for creating,
editing, converting, and displaying raster image files. It can read and write
over 200 different image file formats. ImageMagick is a powerful tool for
working with images on the command line or through programming interfaces. It
supports a wide range of image processing operations, including resizing,
cropping, rotating, and various color manipulations.

ImageMagick is widely used in web development, graphic design, and other
areas where image processing is required. It provides a versatile and
efficient way to handle a variety of image-related tasks. Additionally,
ImageMagick has bindings for various programming languages, making it easy
to integrate into software applications
and workflows.


%package devel
Summary:        Library links and header files for ImageMagick app development
Requires:       %{name}%{?_isa} = %{epoch}:%{version}-%{release}
Requires:       %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}
Obsoletes:      %{name}6-devel       < %{epoch}:%{version}-%{release}
Provides:       %{name}6-devel       = %{epoch}:%{version}-%{release}

%description devel
ImageMagick-devel contains the library links and header files you'll need to
develop ImageMagick applications. ImageMagick is an image manipulation program.

If you want to create applications that will use ImageMagick code or APIs,
you need to install ImageMagick-devel as well as ImageMagick.  You do not
need to install it if you just want to use ImageMagick, however.


%package libs
Summary: ImageMagick libraries to link with
Obsoletes: %{name}6-libs < %{epoch}:%{version}-%{release}
Provides:  %{name}6-libs = %{epoch}:%{version}-%{release}
# These may be used for some functions
Recommends: urw-base35-fonts
# default font is OpenSans-Regular
Recommends: open-sans-fonts

%description libs
This packages contains a shared libraries to use within other applications.


%package djvu
Summary: DjVu plugin for ImageMagick
Requires: %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}
Obsoletes: %{name}6-djvu       < %{epoch}:%{version}-%{release}
Provides:  %{name}6-djvu       = %{epoch}:%{version}-%{release}

%description djvu
This packages contains a plugin for ImageMagick which makes it possible to
save and load DjvU files from ImageMagick and libMagickCore using applications.


%if %{with libheif}
%package heic
Summary: HEIC plugin for ImageMagick
BuildRequires:  pkgconfig(libheif) >= 1.4.0
%if 0%{?rhel} == 7
# ensure we use our on EL-7
Requires:       libheif%{?_isa} >= 1.4.0
%endif
Requires:       %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}

%description heic
This packages contains a plugin for ImageMagick which makes it possible to
save and load HEIC files from ImageMagick and libMagickCore using applications.
%endif


%package doc
Summary: ImageMagick html documentation
Obsoletes: %{name}6-doc < %{epoch}:%{version}-%{release}
Provides:  %{name}6-doc = %{epoch}:%{version}-%{release}

%description doc
ImageMagick documentation, this package contains usage (for the commandline
tools) and API (for the libraries) documentation in HTML format.
Note this documentation can also be found on the ImageMagick website:
https://imagemagick.org/


%if %{with perl}
%package perl
Summary:        ImageMagick perl bindings
Requires:       %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}
Obsoletes:      %{name}6-perl        < %{epoch}:%{version}-%{release}
Provides:       %{name}6-perl        = %{epoch}:%{version}-%{release}

%description perl
Perl bindings to ImageMagick.

Install ImageMagick-perl if you want to use any perl scripts that use
ImageMagick.
%endif


%package c++
Summary:        ImageMagick Magick++ library (C++ bindings)
Requires:       %{name}-libs%{?_isa} = %{epoch}:%{version}-%{release}
Obsoletes:      %{name}6-c++         < %{epoch}:%{version}-%{release}
Provides:       %{name}6-c++         = %{epoch}:%{version}-%{release}

%description c++
This package contains the Magick++ library, a C++ binding to the ImageMagick
graphics manipulation library.

Install ImageMagick-c++ if you want to use any applications that use Magick++.


%package c++-devel
Summary:        C++ bindings for the ImageMagick library
Requires:       %{name}-c++%{?_isa} = %{epoch}:%{version}-%{release}
Requires:       %{name}-devel%{?_isa} = %{epoch}:%{version}-%{release}
Obsoletes:      %{name}6-c++-devel    < %{epoch}:%{version}-%{release}
Provides:       %{name}6-c++-devel    = %{epoch}:%{version}-%{release}

%description c++-devel
ImageMagick-devel contains the static libraries and header files you'll
need to develop ImageMagick applications using the Magick++ C++ bindings.
ImageMagick is an image manipulation program.

If you want to create applications that will use Magick++ code or APIs, you'll
need to install ImageMagick-c++-devel, ImageMagick-devel and ImageMagick.
You don't need to install it if you just want to use ImageMagick, or if you
want to develop/compile applications using the ImageMagick C interface,
however.


%prep
%autosetup -p1 -n %{name}-%{Version}-%{Patchlevel}

# for %%doc
mkdir Magick++/examples
cp -p Magick++/demo/*.cpp Magick++/demo/*.miff Magick++/examples


%build
autoconf -f -i -v
# Reduce thread contention, upstream sets this flag for Linux hosts
export CFLAGS="%{optflags} -DIMPNG_SETJMP_IS_THREAD_SAFE"
%configure \
        --enable-shared \
        --disable-static \
        --with-modules \
%if %{with perl}
        --with-perl \
        --with-perl-options="INSTALLDIRS=vendor %{?perl_prefix} CC='%__cc -L$PWD/magick/.libs' LDDLFLAGS='-shared -L$PWD/magick/.libs'" \
%endif
        --with-x \
        --with-threads \
        --with-magick_plus_plus \
        --with-gslib \
        --with-pango \
        --with-fftw \
        --with-wmf \
        --with-webp \
        --with-openexr \
        --with-rsvg \
        --with-xml \
        --with-urw-base35-font-dir="%{urw_base35_fontpath}" \
        --without-dps \
        --without-gcc-arch \
        --with-jbig \
        --with-jxl \
        --with-openjp2 \
        --with-raw \
%if 0%{?fedora} || 0%{?rhel} > 8
        --with-lqr \
%endif
        --with-gvc \
        --with-raqm \
%if %{with libheif}
           --with-heic \
%endif

# Do *NOT* use %%{?_smp_mflags}, this causes PerlMagick to be silently misbuild
make -j3
# Generate API docs
make html-local


%install
%make_install

cp -a www/source %{buildroot}%{_datadir}/doc/%{name}-%{Version}
# Do NOT remove .la files for codecs
rm %{buildroot}%{_libdir}/*.la

%if %{with perl}
# perlmagick: fix perl path of demo files
%{__perl} -MExtUtils::MakeMaker -e 'MY->fixin(@ARGV)' PerlMagick/demo/*.pl

# perlmagick: cleanup various perl tempfiles from the build which get installed
find %{buildroot} -name "*.bs" |xargs rm -f
find %{buildroot} -name ".packlist" |xargs rm -f
find %{buildroot} -name "perllocal.pod" |xargs rm -f

# perlmagick: build files list
find %{buildroot}/%{_libdir}/perl* -type f -print \
        | sed "s@^%{buildroot}@@g" > perl-pkg-files
find %{buildroot}%{perl_vendorarch} -type d -print \
        | sed "s@^%{buildroot}@%dir @g" \
        | grep -v '^%dir %{perl_vendorarch}$' \
        | grep -v '/auto$' >> perl-pkg-files
if [ -z perl-pkg-files ] ; then
        echo "ERROR: EMPTY FILE LIST"
        exit -1
fi
%endif

# fix multilib issues: Rename provided file with platform-bits in name.
# Create platform independant file inplace of provided and conditionally include required.
# $1 - filename.h to process.
function multilibFileVersions(){
mv $1 ${1%%.h}-%{__isa_bits}.h

local basename=$(basename $1)

cat >$1 <<EOF
#include <bits/wordsize.h>

#if __WORDSIZE == 32
# include "${basename%%.h}-32.h"
#elif __WORDSIZE == 64
# include "${basename%%.h}-64.h"
#else
# error "unexpected value for __WORDSIZE macro"
#endif
EOF
}

multilibFileVersions %{buildroot}%{_includedir}/%{name}-6/magick/magick-config.h
multilibFileVersions %{buildroot}%{_includedir}/%{name}-6/magick/magick-baseconfig.h
multilibFileVersions %{buildroot}%{_includedir}/%{name}-6/magick/version.h


%check
%if %{with tests}
export LD_LIBRARY_PATH=%{buildroot}/%{_libdir}
%make_build check
%endif
rm PerlMagick/demo/Generic.ttf

%ldconfig_scriptlets libs
%ldconfig_scriptlets c++

%files
%doc README.txt NOTICE AUTHORS.txt NEWS.txt
%license LICENSE
%{_bindir}/[a-z]*
%{_mandir}/man[145]/[a-z]*
%{_mandir}/man1/%{name}.*

%files libs
%doc NOTICE AUTHORS.txt QuickStart.txt
%license LICENSE
%{_libdir}/libMagickCore-6.Q16.so.%{libsover}{,.*}
%{_libdir}/libMagickWand-6.Q16.so.%{libsover}{,.*}
%{_libdir}/%{name}-%{Version}
%{_datadir}/%{name}-6
%exclude %{_libdir}/%{name}-%{Version}/modules-Q16/coders/djvu.*
%dir %{_sysconfdir}/%{name}-6
%config(noreplace) %{_sysconfdir}/%{name}-6/*.xml

%files devel
%{_bindir}/Magick-config
%{_bindir}/MagickCore-config
%{_bindir}/Wand-config
%{_bindir}/MagickWand-config
%{_libdir}/libMagickCore-6.Q16.so
%{_libdir}/libMagickWand-6.Q16.so
%{_libdir}/pkgconfig/MagickCore.pc
%{_libdir}/pkgconfig/MagickCore-6.Q16.pc
%{_libdir}/pkgconfig/ImageMagick.pc
%{_libdir}/pkgconfig/ImageMagick-6.Q16.pc
%{_libdir}/pkgconfig/ImageMagick++.pc
%{_libdir}/pkgconfig/ImageMagick++-6.Q16.pc
%{_libdir}/pkgconfig/MagickWand.pc
%{_libdir}/pkgconfig/Wand.pc
%{_libdir}/pkgconfig/MagickWand-6.Q16.pc
%{_libdir}/pkgconfig/Wand-6.Q16.pc
%dir %{_includedir}/%{name}-6
%{_includedir}/%{name}-6/magick
%{_includedir}/%{name}-6/wand
%{_mandir}/man1/Magick-config.*
%{_mandir}/man1/MagickCore-config.*
%{_mandir}/man1/MagickWand-config.*
%{_mandir}/man1/Wand-config.*

%files djvu
%{_libdir}/%{name}-%{Version}/modules-Q16/coders/djvu.*

%if %{with libheif}
%files heic
%{_libdir}/%{name}-%{Version}/modules-Q16/coders/heic.*
%endif

%files doc
%doc %{_datadir}/doc/%{name}-6
%doc %{_datadir}/doc/%{name}-%{Version}
%doc LICENSE

%files c++
%doc Magick++/AUTHORS Magick++/ChangeLog Magick++/NEWS Magick++/README
%doc www/Magick++/COPYING
%{_libdir}/libMagick++-6.Q16.so.%{libcxxsover}{,.*}

%files c++-devel
%doc Magick++/examples
%{_bindir}/Magick++-config
%{_includedir}/%{name}-6/Magick++/
%{_includedir}/%{name}-6/Magick++.h
%{_libdir}/libMagick++-6.Q16.so
%{_libdir}/pkgconfig/Magick++.pc
%{_libdir}/pkgconfig/Magick++-6.Q16.pc
%{_mandir}/man1/Magick++-config.*

%if %{with perl}
%files perl -f perl-pkg-files
%{_mandir}/man3/*
%doc PerlMagick/demo/ PerlMagick/Changelog PerlMagick/README.txt
%endif

%changelog
* Sat Jan 27 2024 Cristy <cristy@devel.imagemagick.org> - 6.9.13-7
-  Port of RedHat's script to create ImageMagick's RPM distribution.
