%define gs_ver 10.02.0
%define gs_dot_ver 10.02.0

%define _libdir64 %{_libdir}64
%global _buildshell /opt/freeware/bin/bash

Summary: A PostScript(TM) interpreter and renderer.
Name: ghostscript
Version: %{gs_ver}

Release: 1

License: GPLv3+ and Redistributable, no modification permitted
URL: https://www.ghostscript.com/
Group: Applications/Publishing
Source0: %{name}-%{gs_ver}.tar.gz

Patch0: %{name}-9.27-aix.patch
Patch2: %{name}-9.x-scripts.patch
Patch4: %{name}-ijs-automake-ver.patch
Patch5: %{name}-9.x-runlibfileifexists.patch
Patch6: %{name}-icc-missing-check.patch
Patch10: %{name}-9.x-cups-filters.patch
Patch29: %{name}-gdevcups-debug-uninit.patch
Patch30: %{name}-9.53.3-libiconv-hardcode.patch
Patch32: %{name}-10.01.1_add_gxiodev_header.patch
Patch33: %{name}-10.01.1-fixjpeg2k.patch
#Patch34: %{name}-10.01.1-unixinst-mak.patch
Patch35: %{name}-10.02.0-base-stdint.patch

BuildRequires: make, patch

BuildRequires: cairo-devel >= 1.14.6
BuildRequires: dbus-devel >= 1.14.0
BuildRequires: fontconfig-devel >= 2.11.95
#BuildRequires: jasper-devel >= 1.900.1
#BuildRequires: lcms2-devel >= 2.2-1
BuildRequires: libiconv >= 1.17-1
#BuildRequires: libidn-devel >= 1.19-2
BuildRequires: libjpeg-devel >= 9e
BuildRequires: libpaper-devel >= 1.1.24
BuildRequires: libpng-devel >= 1.6.37
BuildRequires: libtiff-devel >= 4.4.0
BuildRequires: zlib-devel >= 1.2.13

#Requires: cairo >= 1.10.0
Requires: fontconfig >= 2.11.95
#Requires: jasper >= 1.900.1
#Requires: lcms2 >= 2.2-1
Requires: libiconv >= 1.17-1
#Requires: libidn >= 1.19-2
Requires: libjpeg >= 9e
Requires: libpaper >= 1.1.24
Requires: libpng >= 1.6.37
Requires: libtiff >= 4.4.0
Requires: zlib >= 1.2.13
Requires: freetype2 >= 2.12.1
Requires: libgcc >= 10
Requires: gnutls >= 3.7.2

Requires: urw-fonts >= 2.0
Requires: ghostscript-fonts >= 8.11


%description
Ghostscript is a set of software that provides a PostScript(TM)
interpreter, a set of C procedures (the Ghostscript library, which
implements the graphics capabilities in the PostScript language) and
an interpreter for Portable Document Format (PDF) files. Ghostscript
translates PostScript code into many common, bitmapped formats, like
those understood by your printer or screen. Ghostscript is normally
used to display PostScript files and to print PostScript files to
non-PostScript printers.

If you need to display PostScript files or print them to
non-PostScript printers, you should install ghostscript. If you
install ghostscript, you also need to install the ghostscript-fonts
package.


%package devel
Summary: Files for developing applications that use %{name}.
Requires: %{name} = %{version}-%{release}
Group: Development/Libraries

%description devel
The header files for developing applications that use %{name}.


%package doc
Summary: Documentation for %{name}.
Requires: %{name} = %{version}-%{release}
Group: Documentation

%description doc
The documentation files that come with %{name}.


%prep
%setup -q
rm -rf expat freetype icclib jpeg libpng zlib cups/libs

export PATH=/opt/freeware/bin:$PATH

# necessary fix to successfully compile with XLC/C++ V7 on AIX
%patch0

# Fix some shell scripts
%patch2 -p1 -b .scripts

# Fix ./autgen.sh in ijs sub-project
# See http://bugs.ghostscript.com/show_bug.cgi?id=692040 for details.
#%patch4 -p1 -b .ijs-automake-ver

# Define .runlibfileifexists.
%patch5 -p1

# Fixed missing error check when setting ICC profile.
#%patch6 -p1 -b .icc-missing-check

# Install CUPS filter convs files in the correct place.
#%patch10 -p1 -b .cups-filters

# gdevcups: don't use uninitialized variables in debugging output.
%patch29 -p1 -b .gdevcups-debug-uninit
%patch30
%patch32
%patch33
#%patch34
%patch35
rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit

%build

# build on 64bit mode
cd 64bit
export OBJECT_MODE=64
export CC="gcc  -pthread -O2  -maix64"
export CXX="g++ -pthread -O2 -maix64"
export CFLAGS="-DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O -I/opt/freeware/include -Wno-error=return-type " 
export CXXFLAGS="-DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O -I/opt/freeware/include -Wno-error=return-type"
export LDFLAGS="-L/opt/freeware/lib/pthread/ppc64  -L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib/pthread/ppc64:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib "
export FT_CFLAGS=$(pkg-config --cflags freetype2)

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir64} \
    --enable-fontconfig \
    --with-ijs \
    --with-libpaper \
    --with-pdftoraster \
    --with-jbig2dec \
    --with-x \
    --with-system-libtiff \
    --with-drivers=ALL  \
    --without-tesseract	\
    --disable-cups \
    --without-libidn
 
gmake %{?_smp_mflags}

# build IJS
cd ijs
./autogen.sh
./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir64} \
    --enable-shared --disable-static
gmake %{?_smp_mflags}
cd ..

gmake -k check

# build on 32bit mode
cd ../32bit
export OBJECT_MODE=32
export CC="gcc -pthread -O2 -D_LARGE_FILES"
export CXX="g++ -pthread -O2 -D_LARGE_FILES"
export CFLAGS="-DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O -I/opt/freeware/include -Wno-error=return-type "
export CXXFLAGS="-DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O -I/opt/freeware/include -Wno-error=return-type" 
export LDFLAGS="-L/opt/freeware/lib/pthread  -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib/pthread:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000 -Wl,-bnoquiet"

export FT_CFLAGS=$(pkg-config --cflags freetype2)

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir} \
    --enable-fontconfig \
    --with-ijs \
    --with-libpaper \
    --with-pdftoraster \
    --with-jbig2dec \
    --with-x \
    --with-system-libtiff \
    --with-drivers=ALL  \
    --without-tesseract	\
    --disable-cups \
    --without-libidn


gmake %{?_smp_mflags}

# build IJS
cd ijs
./autogen.sh
./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir} \
    --enable-shared --disable-static
gmake %{?_smp_mflags}
cd ..

gmake -k check

%install

[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}

# install on 64bit mode
cd 64bit
export OBJECT_MODE=64
gmake DESTDIR=${RPM_BUILD_ROOT} install

cd ijs
gmake DESTDIR=${RPM_BUILD_ROOT} install
cd ..

(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_64
    done
)

# install on 32bit mode
cd ../32bit
export OBJECT_MODE=32
gmake DESTDIR=${RPM_BUILD_ROOT} install

cd ijs
gmake DESTDIR=${RPM_BUILD_ROOT} install
cd ..

(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_32
	ln -sf "$fic"_64 $fic
    done
)

/usr/bin/strip -X32_64 ${RPM_BUILD_ROOT}%{_bindir}/* || :

# install header files
mkdir -p ${RPM_BUILD_ROOT}%{_includedir}/%{name}
cp devices/gdevdsp.h ${RPM_BUILD_ROOT}%{_includedir}/%{name}
cp psi/iapi.h ${RPM_BUILD_ROOT}%{_includedir}/%{name}
cp psi/ierrors.h ${RPM_BUILD_ROOT}%{_includedir}/%{name}
chmod 644 ${RPM_BUILD_ROOT}%{_includedir}/%{name}/*

mkdir -p ${RPM_BUILD_ROOT}%{_docdir}/%{name}-%{gs_dot_ver}
#mv ${RPM_BUILD_ROOT}%{_datadir}/%{name}/%{gs_dot_ver}/doc/* ${RPM_BUILD_ROOT}%{_docdir}/%{name}-%{gs_dot_ver}
#rmdir ${RPM_BUILD_ROOT}%{_datadir}/%{name}/%{gs_dot_ver}/doc/

mkdir -p ${RPM_BUILD_ROOT}%{_mandir}
#mv ${RPM_BUILD_ROOT}%{_datadir}/man/* ${RPM_BUILD_ROOT}%{_mandir}
#rm -rf ${RPM_BUILD_ROOT}%{_datadir}/man


%clean
[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}


%files
%defattr(-,root,system)
%{_bindir}/*
%{_libdir}/*.a
%{_libdir64}/*.a





%files devel
%defattr(-,root,system)
%{_includedir}/*
%{_libdir}/pkgconfig/*.pc
%{_libdir64}/pkgconfig/*.pc


%files doc
%defattr(-,root,system)
%doc %{_docdir}/%{name}-%{gs_dot_ver}


%changelog
* Fri Oct 27 2023 Ranjit Ranjan<Ranjit.Ranjan@ibm.com>-10.02.0-1
- Updated to 10.02.0 for CVE-2023-43115 fix.

* Wed Aug 09 2023 Ranjit Ranjan<Ranjit.Ranjan@ibm.com>-10.01.2-1
- Updated to 10.01.2 for CVE-2023-36664 fix.

* Mon Jun 19 2023 Ranjit Ranjan<Ranjit.Ranjan@ibm.com>-10.01.1-1
- Updated to 10.01.1 for CVE-2023-28879 fix.

* Tue Jul 12 2022 Rakshitha N <Rakshitha.N2@ibm.com>-9.56.1-1
- Updated to 9.56.1

* Fri Feb 18 2022 Rakshitha N <Rakshitha.N2@ibm.com>-9.55.0-1
- Updated to 9.55.0


* Fri Nov 13 2020 Reshma V Kumar <reskumar@in.ibm.com> -9.53.3-1
- Update to fix CVE-2020-17538,CVE-2020-16307,CVE-2020-16291,CVE-2020-16303
- CVE-2020-16287,CVE-2020-16299,CVE-2020-16295,CVE-2020-16302,CVE-2020-16298
- CVE-2020-16294,CVE-2020-16310,CVE-2020-16306,CVE-2020-16290,CVE-2020-16297,
- CVE-2020-16309,CVE-2020-16293,CVE-2020-16305,CVE-2020-16289,CVE-2020-16301
- CVE-2020-16308,CVE-2020-16292,CVE-2020-16304,CVE-2020-16288,CVE-2020-16300
- CVE-2020-16296

* Mon Dec 09 2019 Reshma V Kumar <reskumar@in.ibm.com> -9.50-1
- Update to fix CVE-2019-14869

* Fri Apr 12 2019 Reshma V Kumar <reskumar@in.ibm.com> -9.27-1
- Update to fix CVE-2018-19134,CVE-2019-6116,CVE-2018-16863,CVE-2019-3835
- CVE-2019-3838,CVE-2019-3835 ,CVE-2019-3838,CVE-2018-16540,CVE-2018-19475,
- CVE-2018-19476,CVE-2018-19477

* Tue Nov 20 2018 Reshma V Kumar <reskumar@in.ibm.com> -9.25-1
- Initial port for AIX toolbox

* Fri Jan 18 2013 Michael Perzl <michael@perzl.org> - 9.06-2
- removed dependency on cups due to incompatibilities with various AIX versions

* Sun Jan 06 2013 Michael Perzl <michael@perzl.org> - 9.06-1
- updated to version 9.06
- added a dependency on 'lcsm2', 'dbus' and 'libidn'

* Sun Jan 06 2013 Michael Perzl <michael@perzl.org> - 8.71-1
- updated to version 8.71

* Sun Jan 06 2013 Michael Perzl <michael@perzl.org> - 8.70-2
- added a 'devel' and 'cups' package
- enabled cairo and thus added a dependency on cairo
- added a dependency on libiconv

* Tue Sep 15 2009 Michael Perzl <michael@perzl.org> - 8.70-1
- updated to version 8.70

* Tue Sep 15 2009 Michael Perzl <michael@perzl.org> - 8.64-1
- updated to version 8.64

* Tue Sep 15 2009 Michael Perzl <michael@perzl.org> - 8.62-3
- fixed some minor spec file issues

* Wed Apr 23 2008 Michael Perzl <michael@perzl.org> - 8.62-2
- some minor spec file fixes

* Wed Mar 05 2008 Michael Perzl <michael@perzl.org> - 8.62-1
- updated to version 8.62

* Thu Feb 21 2008 Michael Perzl <michael@perzl.org> - 8.61-1
- updated to version 8.61

* Fri Sep 28 2007 Michael Perzl <michael@perzl.org> - 8.60-1
- first version for AIX V5.1 and higher
