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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=libwmf
V=0.2.13
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc freetype-dev gdk-pixbuf2-dev automake libtool-dev zsh  "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/caolanm/$P/archive/refs/tags/v$V.zip   " $USER 

unzip v$V.zip
cd $SRC
# libtoolize -v # find out what I could fix first
#libtoolize: You should add the contents of the following files to 'aclocal.m4':
#libtoolize:   '/usr/local/share/aclocal/libtool.m4'
#libtoolize:   '/usr/local/share/aclocal/ltversion.m4'
#libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,
#libtoolize: and rerunning libtoolize and aclocal.
#libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
autoreconf --force --install

./configure prefix=/usr/local --localstatedir=/var  --with-x --disable-static \
--with-gsfontdir=/usr/local/share/ghostscript/9.10/fonts --with-fontdir=/usr/local/share/fonts/libwmf \
--with-fontdir=/usr/local/share/fonts/libwmf --with-docdir=/usr/local/share/doc/libwmf \
--with-gsfontmap=/usr/local/share/ghostscript/9.10/Resource/Init/Fontmap.GS \
--with-gsfontdir=/usr/local/share/ghostscript/9.10/Resource/Font 
# Support for X:		yes
# Support for GD:		wmf
# Support for JPEG though GD:	yes
# Support for XML:		expat
# Documentation directory:	/usr/local/share/doc/libwmf
# Font directory:		/usr/local/share/fonts/libwmf
# System XML fontmap file:	/usr/share/fonts/fontmap
# Non-system XML fontmap file:	/usr/local/share/fonts/libwmf/fontmap 
# ghostscript fontmap file:	/usr/local/share/ghostscript/9.10/Resource/Init/Fontmap.GS
# ghostscript font directory:	/usr/local/share/ghostscript/9.10/Resource/Font
# building gdk-pixbuf plug-in?	yes
make -j5 # 15 seconds 
make install-strip DESTDIR=/tmp/$P 
cd /tmp

# there is an error-- the fonts target /usr/local/share/fonts/libwmf
# instead  going to .................../usr/local/share/libwmf/fonts we fix below

# dev
####### 
mkdir -p $P-dev/usr/local/bin
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
rm -rf  $P/usr/local/lib/*.la 
rm -rf $P/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/*.la
mv $P/usr/local/bin/$P-config $P-dev/usr/local/bin/

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v3' > $P/usr/local/share/doc/$P/COPYING
cp $SRC/fonts/LICENSE $P/usr/local/share/doc/$P/LICENSE-FONTS

# fix font pathway but fontmap already has correct paths
mkdir -p $P/usr/local/share/fonts/$P
mv $P/usr/local/share/libwmf/fonts/$P/* $P/usr/local/share/fonts/$P
rm -rf $P/usr/local/share/$P

# install script
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
fc-cache -f -v /usr/local/share/fonts/libwmf
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# 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:           libwmf-dev.tcz
Description:     dev files
Version:         0.2.13
Author:          https://github.com/caolanm/libwmf/blob/master/CREDITS
Original-site:   https://github.com/caolanm/libwmf
Copying-policy:  main GPL v 2
                 fonts GNU License and Aladdin Free Public License
Size:            40K
Extension_by:    aus9 @ linuxquestions.org
Tags:            wmf vector image metafile 
Comments:        Development 
                            
Change-log:     2015/02/02 v 0.2.8.4 upstream was sourceforge on 6x (Juanito)
Current:        2024/04/12 v 0.2.13  upstream as above on 15x (aus9) ' > $P-dev.tcz.info
echo 'libwmf.tcz
freetype-dev.tcz 
gdk-pixbuf2-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:           libwmf.tcz
Description:     see below + fonts
Version:         0.2.13
Author:          https://github.com/caolanm/libwmf/blob/master/CREDITS
Original-site:   https://github.com/caolanm/libwmf
Copying-policy:  main GPL v 2
                 fonts GNU License and Aladdin Free Public License
Size:            2.0M
Extension_by:    aus9 @ linuxquestions.org
Tags:            wmf vector image metafile fonts 
Comments:        Library for interpreting native windows metafile 
                 and either displaying them using X or 
                 converting them to other image formats such as 
                 PNG, JPEG, PS, EPS and SVG 
                 $ wmf2gd -o output.png input.wmf
                 $ wmf2gd -t jpeg -o output.jpg input.wmf
                 $ wmf2svg -o output.svg input.wmf etc

                 (Update) fonts now differ from 6x            

Change-log:     2015/02/02 v 0.2.8.4 upstream sourceforge on 6x (Juanito)
Current:        2024/04/12 v 0.2.13  upstream as above on 15x (aus9) ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # del dupes and TCBs
#[libwmf-0.2.so.7][libwmflite-0.2.so.7]this TCE
#[libfreetype.so.6]freetype
#[libX11.so.6]libX11
#[libexpat.so.1]expat2
#[libjpeg.so.62]libjpeg-turbo
#[libpng16.so.16]libpng -> freetype
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # no extras
readelf -d $P/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/io-wmf.so | grep 'NEEDED'
# gtk2 is no longer a run dep as loader moves to gdk-pixbuf
#[libgdk_pixbuf-2.0.so.0]gdk-pixbuf2
#[libgobject-2.0.so.0][libglib-2.0.so.0] glib2

echo 'freetype.tcz  
libX11.tcz
expat2.tcz
libjpeg-turbo.tcz
gdk-pixbuf2.tcz
glib2.tcz
fontconfig.tcz        ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

