#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   exit 1
fi

# Juanito drops -fno-exceptions -fno-rtti..already installed liblzma-dev zstd-dev
CC="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe"

P=imagemagick
V=7.1.1-30
SRC=ImageMagick-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc Xorg-7.7-3d-dev librsvg-dev libwebp1-dev \
libtool-dev djvulibre-dev libgs-dev perl5 fftw-dev libraw-dev \
openexr-dev jasper2-dev libexif-dev libgxps-dev openjpeg-dev libwmf-dev \
ghostscript   "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

sudo cp /tmp/tcloop/gcc_libs-dev/usr/local/lib/libgomp.spec /usr/lib

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://download.$P.org/archive/releases/ImageMagick-$V.tar.xz   " $USER 
tar xvf Image*xz
cd $SRC

./configure -prefix=/usr/local --disable-static --localstatedir=/var --enable-hdri \
--with-dejavu-font-dir=/usr/local/share/fonts/dejavu \
--with-gs-font-dir=/usr/local/share/ghostscript/9.10/Resource/Font \
--with-fftw=yes --with-gslib=yes --with-modules --with-perl=yes \
--with-rsvg  --with-wmf --with-x --disable-docs 


# ImageMagick 7.1.1-30 is configured as follows. Verify that this configuration
# matches your expectations.
# Host system type: x86_64-pc-linux-gnu  # Build system type: x86_64-pc-linux-gnu
# there are MORE lines compared to Juanito compile
#                    Option				Value
#----------------------------------------------------------------------------
# Shared libraries    --enable-shared=yes		yes
# Static libraries    --enable-static=no		no
# Build utilities     --with-utilities=yes		yes
# Module support      --with-modules=yes		yes
# GNU ld              --with-gnu-ld=yes			yes
# OpenMP              --enable-openmp=			yes
# Security policy     --with-security-policy=open	open
# Quantum depth       --with-quantum-depth=16		16
# Channel mask depth  --enable-64bit-channel-masks=no	32
# High Dynamic Range Imagery --enable-hdri=yes		yes

# Install documentation:				yes
# Memory allocation library:
# JEMalloc          --with-jemalloc=no		        no
# MTMalloc          --with-mtmalloc=no			no
# TCMalloc          --with-tcmalloc=no			no
# UMem              --with-umem=no			no

# Delegate library configuration:
# BZLIB             --with-bzlib=yes			yes
# Autotrace         --with-autotrace=no			no
# DJVU              --with-djvu=yes			yes
# DPS               --with-dps=no			no
# FFTW              --with-fftw=yes			yes
# FLIF              --with-flif=yes			no 
# FlashPIX          --with-fpx=no			no 
# FontConfig        --with-fontconfig=yes		yes
# FreeType          --with-freetype=yes			yes
# Ghostscript lib   --with-gslib=yes			yes
# Graphviz          --with-gvc=yes			no
# HEIC              --with-heic=yes			no
# JBIG              --with-jbig=yes			no
# JPEG v1           --with-jpeg=yes			yes
# JPEG XL           --with-jxl=yes			no
# DMR               --with-dmr=yes			no
# LCMS              --with-lcms=yes			yes
# LQR               --with-lqr=yes			no
# LTDL              --with-ltdl=yes			yes
# LZMA              --with-lzma=yes			yes
# Magick++          --with-magick-plus-plus=yes		yes
# OpenEXR           --with-openexr=yes			yes
# OpenJP2           --with-openjp2=yes			yes
# PANGO             --with-pango=yes			yes
# PERL              --with-perl=yes			/usr/local/bin/perl
# PNG               --with-png=yes			yes
# RAQM              --with-raqm=yes			no
# RAW               --with-raw=yes			yes
# RSVG              --with-rsvg=yes			yes
# TIFF              --with-tiff=yes			yes
# UHDR              --with-uhdr=no			no
# WEBP              --with-webp=yes			yes
# WMF               --with-wmf=yes			no (failed tests)
# X11               --with-x=				yes
# XML               --with-xml=yes			yes
# ZIP               --with-zip=yes			no
# ZLIB              --with-zlib=yes			yes
# ZSTD              --with-zstd=yes			yes

# Delegate program configuration:
# GhostPCL          None				pcl6 (unknown)
# GhostXPS          None				gxps (unknown)
# Ghostscript       None				gs (9.10)
#    Font configuration:
# Apple fonts       --with-apple-font-dir=default	
# Dejavu fonts      --with-dejavu-font-dir=/usr/local/share/fonts/dejavu   /usr/local/share/fonts/dejavu/
# Ghostscript fonts --with-gs-font-dir=/usr/local/share/ghostscript/fonts  /usr/local/share/ghostscript/fonts/
# URW-base35 fonts  --with-urw-base35-font-dir=default	                   none
# Windows fonts     --with-windows-font-dir=default	                   none

# X11 configuration:
# X_CFLAGS          = 
# X_PRE_LIBS        =  -lSM -lICE
# X_LIBS            = 
# X_EXTRA_LIBS      = 
# Options used to compile and link:
# PREFIX            = /usr/local
# EXEC-PREFIX       = /usr/local
# VERSION           = 7.1.1-30
# CC                = gcc
# CFLAGS            = -I/usr/local/include/libxml2 -I/usr/local/include -I/usr/local/include/webp -I/usr/local/include 
#       -I/usr/local/include/webp -I/usr/local/include -I/usr/local/include -I/usr/local/include/cairo -I/usr/local/include
#       -I/usr/local/include/freetype2 -I/usr/local/include/harfbuzz -I/usr/local/include/glib-2.0 
#       -I/usr/local/lib/glib-2.0/include -I/usr/local/include -I/usr/local/include/pixman-1 -I/usr/local/include/libpng16 
#       -I/usr/local/include -pthread -I/usr/local/include/librsvg-2.0 -I/usr/local/include/gdk-pixbuf-2.0 -I/usr/local/include
#       -I/usr/local/include/cairo -I/usr/local/include -I/usr/local/include/freetype2 -I/usr/local/include/libpng16 
#       -I/usr/local/include/harfbuzz -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/local/include 
#       -I/usr/local/include/pixman-1 -I/usr/local/include -I/usr/local/include/libraw -I/usr/local/include 
#       -I/usr/local/include/libpng16 -pthread -I/usr/local/include/pango-1.0 -I/usr/local/include/harfbuzz 
#       -I/usr/local/include/pango-1.0 -I/usr/local/include -I/usr/local/include/fribidi -I/usr/local/include 
#       -I/usr/local/include/cairo -I/usr/local/include -I/usr/local/include/freetype2 -I/usr/local/include/libpng16 
#       -I/usr/local/include/harfbuzz -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/local/include
#       -I/usr/local/include/pixman-1 -I/usr/local/include -pthread -I/usr/local/include/OpenEXR -I/usr/local/include 
#       -I/usr/local/include/openjpeg-2.3 -I/usr/local/include -I/usr/local/include/freetype2 -I/usr/local/include/libpng16
#       -I/usr/local/include/harfbuzz -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/local/include
#       -I/usr/local/include -I/usr/local/include/freetype2 -I/usr/local/include/libpng16 -I/usr/local/include/harfbuzz
#       -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/local/include -pthread -I/usr/local/include
#       -I/usr/local/include -fopenmp -Wall -g -O2 -mtune=amdfam10 -fexceptions -pthread -DMAGICKCORE_HDRI_ENABLE=1 
#       -DMAGICKCORE_QUANTUM_DEPTH=16 -I/usr/local/include -DMAGICKCORE_CHANNEL_MASK_DEPTH=32

# CPPFLAGS          = -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_CHANNEL_MASK_DEPTH=32
# PCFLAGS           = 
# DEFS              = -DHAVE_CONFIG_H
# LDFLAGS           = 
# LIBS              = -L/usr/local/lib -llcms2 -L/usr/local/lib -lfreetype -L/usr/local/lib -lfftw3 -L/usr/local/lib 
#         -lxml2 -L/usr/local/lib -lfontconfig -lfreetype -lXext -lSM -lICE -lX11 -lXt -lbz2 -lz -lltdl -lm -lpthread -lgomp 
# CXX               = g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe
# CXXFLAGS          =  -g -O2 -pthread
# FEATURES          = Channel-masks(32-bit) Cipher DPC HDRI Modules OpenMP
# DELEGATES         = bzlib cairo djvu fftw fontconfig freetype gslib jng jp2 jpeg lcms lzma openexr pangocairo png ps raw rsvg tiff webp x xml zlib zstd

find . -name Makefile -type f -exec sed -i 's/-g -O2 -mtune=haswell//g' {} \;
find . -name Makefile.PL -type f -exec sed -i 's/-g -O2 -mtune=haswell//g' {} \;

make -j5 # 2M
make install-strip DESTDIR=/tmp/$P # 3M
cd /tmp

# dev
#####
DEV=$P-dev/usr/local/bin
mkdir -p $P-dev/usr/local/lib/
mkdir -p $DEV
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/bin/*-config $DEV

# main
###### wrong pathway too submit found stuff pass 1
rm -rf $P/usr/local/man

chmod 644 $P/usr/local/lib/ImageMagick-7.1.1/modules-Q16HDRI/coders/*.la
chmod 644 $P/usr/local/lib/*.la
chmod 644 $P/usr/local/lib/perl5/x86_64-linux-thread-multi/Image/*.pm
chmod 644 $P/usr/local/lib/perl5/x86_64-linux-thread-multi/Image/Magick/*.pm
chmod 644 $P/usr/local/lib/perl5/x86_64-linux-thread-multi/auto/Image/Magick/Q16HDRI/autosplit.ix

strip --strip-unneeded /tmp/imagemagick/usr/local/lib/perl5/x86_64-linux-thread-multi/auto/Image/Magick/Q16HDRI/*.so
chmod 755 $P/usr/local/lib/perl5/x86_64-linux-thread-multi/auto/Image/Magick/Q16HDRI/*.so

# clean up doc dir
rm -rf $P/usr/local/share/doc
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P

# TCZ them
###########
LIST2="$P $P-dev "
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:           imagemagick-dev.tcz
Description:     dev files
Version:         7.1.1-30
Author:          ImageMagick Studio LLC
Original-site:   https://www.imagemagick.org
Copying-policy:  accompanied as per ImageMagick Studio LLC LICENSE
Size:            212K
Extension_by:    aus9 @ linuxquestions.org
Tags:            image 
Comments:        development
                 Xorg-7.7-3d-dev.tcz is also a dep                

Change-log:     2015/05/27 v 6.9.1-0 (Juanito)
                2016/09/04 v 6.9.5-8
                2019/04/27 v 6.9.10-42
                2020/10/07 v 7.0.10-27
Current:        2024/04/15 v 7.1.1-30 drop wmf support  on 15x (aus9)  ' > $P-dev.tcz.info
echo 'imagemagick.tcz
librsvg-dev.tcz
libwebp1-dev.tcz
djvulibre-dev.tcz 
libgs-dev.tcz
perl5.tcz
fftw-dev.tcz
libraw-dev.tcz
openexr-dev.tcz 
jasper2-dev.tcz 
libexif-dev.tcz 
libgxps-dev.tcz 
openjpeg-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:           imagemagick.tcz
Description:     CLI image editor
Version:         7.1.1-30
Author:          ImageMagick Studio LLC
Original-site:   https://www.imagemagick.org
Copying-policy:  accompanied as per ImageMagick Studio LLC LICENSE
Size:            3.8M
Extension_by:    aus9 @ linuxquestions.org
Tags:            image 
Comments:        Can convert or display most formats, eg 
                 $ convert input.png output.svg
                 $ display output.svg

                 *.la files needed for conversion
                 One wiki is https://wiki.archlinux.org/title/ImageMagick 
                 If you have no terminal button to close a displayed image,
                 try pressing the escape key 

                 svg needs librsvg, webp needs libwebp1, tiff needs libtiff
                 jp2 needs openjpeg, png needs libpng
                 jpeg/jpg needs libjpeg-turbo 

                 wmf disabled this build but works with TCE=graphicsmagick                 

Change-log:     2015/05/27 v 6.9.1-0 (Juanito)
                2016/09/04 v 6.9.5-8
                2019/04/27 v 6.9.10-42
                2020/10/07 v 7.0.10-27
Current:        2024/04/15 v 7.1.1-30 drop wmf support  on 15x (aus9) ' > $P.tcz.info

readelf -d $P/usr/local/bin/magick | grep 'NEEDED'
#libMagickCore-7.Q16HDRI.so.10]libMagickWand-7.Q16HDRI.so.10]this TCE
#liblcms2.so.2]liblcms2
#libfftw3.so.3]fftw
#libxml2.so.2]libxml2
#libfontconfig.so.1]fontconfig
#libfreetype.so.6]freetype
#libXext.so.6]libXext
#libXt.so.6]libXt
#libbz2.so.1.0] bzip2-lib -> freetype
#libSM.so.6]libSM  -> libXt
#libICE.so.6]libICE -> libXt
#libX11.so.6]libX11 -> libXt
#libgomp.so.1] gcc_libs
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' #  extras
#libltdl.so.7] libltdl

echo 'liblcms2.tcz  
fftw.tcz
libxml2.tcz
fontconfig.tcz
fontconfig.tcz
libXext.tcz
libXt.tcz
gcc_libs.tcz
libltdl.tcz      ' > $P.tcz.dep


# pass 1 to check but it makes a mess of dep files
submitqc --libs
rm -rf *.zsync


