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

# atk now in at-spi2-core
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"

P=gtk2
V=2.24.33
SRC=gtk+-2.24.33
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc at-spi2-core-dev at-spi2-core-gir cups-dev gdk-pixbuf2-dev \
gdk-pixbuf2-gir gobject-introspection-dev pango-dev pango-gir "
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://download.gnome.org/sources/gtk+/2.24/gtk%2B-2.24.33.tar.xz  " $USER
tar xvf gt*xz
cd $SRC
./configure --prefix=/usr/local --enable-cups --disable-debug --enable-introspection=yes
make -j5 # 3m
make install-strip DESTDIR=/tmp/$P  # takes seconds
cd /tmp

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# dev
###### remove la files
rm -rf $P/usr/local/lib/*la
rm -rf  $P/usr/local/lib/gtk-2.0/2.10.0/engines/*la 
rm -rf  $P/usr/local/lib/gtk-2.0/2.10.0/immodules/*la 
rm -rf  $P/usr/local/lib/gtk-2.0/2.10.0/printbackends/*la 
rm -rf  $P/usr/local/lib/gtk-2.0/modules/*la 

mkdir -p $P-dev/usr/local/lib/
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/

mkdir -p $P-dev/usr/local/lib/gtk-2.0
mv $P/usr/local/lib/gtk-2.0/include $P-dev/usr/local/lib/gtk-2.0/
cd /tmp

mkdir -p $P-dev/usr/local/share
mv $P/usr/local/share/aclocal $P-dev/usr/local/share/

# gir
#####
mkdir -p $P-gir/usr/local/lib/
mv $P/usr/local/lib/girepository-1.0 $P-gir/usr/local/lib/
mkdir -p $P-gir/usr/local/share
mv $P/usr/local/share/gir* $P-gir/usr/local/share/

# doc
##### now has 2 exes
mkdir -p $P-doc/usr/local/share/doc/$P/html

mv $P/usr/local/share/gtk-doc/html/gail-libgail-util $P-doc/usr/local/share/doc/$P/html
mv $P/usr/local/share/gtk-doc/html/gdk2 $P-doc/usr/local/share/doc/$P/html
mv $P/usr/local/share/gtk-doc/html/gtk2 $P-doc/usr/local/share/doc/$P/html
rm -rf $P/usr/local/share/gtk-doc

mv $P/usr/local/share/gtk-2.0/demo $P-doc/usr/local/share/doc/$P/
rm -rf $P/usr/local/share/gtk-2.0
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P
echo 'LGPL v2' > $P-doc/usr/local/share/doc/$P/COPYING

mkdir -p $P-doc/usr/local/bin
mv $P/usr/local/bin/gtk-builder-convert $P-doc/usr/local/bin
mv $P/usr/local/bin/gtk-demo $P-doc/usr/local/bin
sed 's|python|python3|' -i $P-doc/usr/local/bin/gtk-builder-convert

# main
#####   not big enough to split out themes
mkdir -p $P/usr/local/share/doc/$P
echo 'LGPL v2' > $P/usr/local/share/doc/$P/COPYING

mkdir -p $P/usr/local/share/$P
echo '# DO NOT EDIT! This file will be overwritten by LXAppearance.
# Any customization should be done in ~/.gtkrc-2.0.mine instead.

gtk-theme-name="Raleigh"
gtk-icon-theme-name="Adwaita"
gtk-font-name="Sans 10"
gtk-cursor-theme-name="Adwaita"
gtk-cursor-theme-size=0
gtk-toolbar-style=GTK_TOOLBAR_BOTH
gtk-toolbar-icon-size=GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-button-images=1
gtk-menu-images=1
gtk-enable-event-sounds=1
gtk-enable-input-feedback-sounds=1
gtk-xft-antialias=1
gtk-xft-hinting=1
gtk-xft-hintstyle="hintfull"
gtk-xft-rgba="rgb"
include "/home/tc/.gtkrc-2.0.mine"  ' > $P/usr/local/share/$P/.gtkrc-2.0


# libtool links
###############
cd $P
libtool --finish usr/local/lib/gtk-2.0/2.10.0/printbackends
libtool --finish usr/local/lib/gtk-2.0/modules
cd /tmp

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
gtk-query-immodules-2.0 --update-cache

USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/gtk2/usr/local/share/gtk2/.gtkrc-2.0
HOME=/home/$USER/.gtkrc-2.0
[ -f $HOME ] ||  cp $LOOP $HOME 
chown $USER:staff $HOME
chmod 644 $HOME
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# Juanito is using a gtk3 exe for below so delete mine
rm -rf $P/usr/local/bin/gtk-update-icon-cache

# TCZ them
#######
LIST="$P $P-doc $P-dev $P-gir $P-locale"
for Z in $LIST
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:          gtk2-dev.tcz
Description:    dev files 
Version:        2.24.33
Author:         see -doc/AUTHORS
Original-site:  https://download.gnome.org/sources/gtk+/2.24/
Copying-policy: LGPL v2
Size:           504K		
Extension_by:   aus9 @ linuxquestions.org 
Tags:           gtk2
Comments:       Development files
 
Change-log:     2014/01/18 First v 2.24.20 (bmarkus)
                2018/06/14 v 2.24.32 add cups backend (aus9) on 9x 
                2020/04/19 recompiled for 11x add .gtkrc-2.0
                2020/04/25 add locale, fix pc files on 11x
                2024/03/20 v 2.24.33 on 15x, 2 exes moved 
Current:        2024/03/21 remove libtool from install script             ' > $P-dev.tcz.info

echo 'gtk2.tcz
at-spi2-core-dev.tcz 
cups-dev.tcz 
gdk-pixbuf2-dev.tcz 
pango-dev.tcz   ' > $P-dev.tcz.dep

echo 'Title:          gtk2-doc.tcz
Description:    docs 
Version:        2.24.33
Author:         see -doc/AUTHORS
Original-site:  https://download.gnome.org/sources/gtk+/2.24/
Copying-policy: LGPL v2
Size:           4.1M		
Extension_by:   aus9 @ linuxquestions.org 
Tags:           gtk2
Comments:       Docs plus exes as:
                gtk-builder-convert needs python3.9 & 
                gtk-demo needs gtk2 loaded            

Change-log:     2014/01/18 First v 2.24.20 (bmarkus)
                2018/06/14 v 2.24.32 add cups backend (aus9) on 9x 
                2020/04/19 recompiled for 11x add .gtkrc-2.0
                2020/04/25 add locale, fix pc files on 11x
                2024/03/20 v 2.24.33 on 15x, 2 exes moved 
Current:        2024/03/21 remove libtool from install script   ' > $P-doc.tcz.info

# gtk-builder-convert needs python3.9
readelf -d $P-doc/usr/local/bin/gtk-demo | grep 'NEEDED' -> gkt2 stuff

echo 'Title:          gtk2-gir.tcz
Description:    gir files for gtk2
Version:        2.24.33
Author:         see -doc/AUTHORS
Original-site:  https://download.gnome.org/sources/gtk+/2.24/ 
Copying-policy: LGPL v2
Size:           1.9M		
Extension_by:   aus9 @ linuxquestions.org 
Tags:           gtk2 
Comments:       introspection files for gtk2

Change-log:     2014/01/18 First v 2.24.20 (bmarkus)
                2018/06/14 v 2.24.32 add cups backend (aus9) on 9x 
                2020/04/19 recompiled for 11x add .gtkrc-2.0
                2020/04/25 add locale, fix pc files on 11x
                2024/03/20 v 2.24.33 on 15x, 2 exes moved 
Current:        2024/03/21 remove libtool from install script  ' > $P-gir.tcz.info

# at-spi*gir pulls gobject-introspection
echo 'gtk2.tcz
at-spi2-core-gir.tcz
gdk-pixbuf2-gir.tcz 
pango-gir.tcz    ' > $P-gir.tcz.dep

echo 'Title:          gtk2-locale.tcz
Description:    locales
Version:        2.24.33
Author:         see -doc/AUTHORS
Original-site:  https://download.gnome.org/sources/gtk+/2.24/
Copying-policy: LGPL v2
Size:           7.2M		
Extension_by:   aus9 @ linuxquestions.org 
Tags:           gtk2 
Comments:       locales files for gtk2

Change-log:     2014/01/18 First v 2.24.20 (bmarkus)
                2018/06/14 v 2.24.32 add cups backend (aus9) on 9x 
                2020/04/19 recompiled for 11x add .gtkrc-2.0
                2020/04/25 add locale, fix pc files on 11x
                2024/03/20 v 2.24.33 on 15x, 2 exes moved 
Current:        2024/03/21 remove libtool from install script ' > $P-locale.tcz.info
echo 'gtk2.tcz' > $P-locale.tcz.dep

echo 'Title:          gtk2.tcz
Description:    Gimp Tool Kit
Version:        2.24.33
Author:         see -doc/AUTHORS
Original-site:  https://download.gnome.org/sources/gtk+/2.24/
Copying-policy: LGPL v2
Size:           2.2M		
Extension_by:   aus9 @ linuxquestions.org
Tags:           gtk2 
Comments:       Gimp Tool Kit
                Your config file is ~/.gtkrc-2.0 contains pathway /home/tc
                The cache update generates
                /usr/local/lib/gtk-2.0/2.10.0/immodules.cache

                Optional:lxappearance to modify config
                -libcups for printbackends (likely more TCEs)
                
                If you would like to input using multipress method, you need
                a keypad keyboard, numlock on and open a gtk2 app eg leafpad/netsurf
                Right hand click an input area and choose input methods -> multipress
                pressing the same numlock number key quickly displays different characters
                see /usr/local/etc/gtk-2.0/im-multipress.conf

                2 rarely used exes moved to doc TCE
 
Change-log:     2014/01/18 First v 2.24.20 (bmarkus)
                2018/06/14 v 2.24.32 add cups backend (aus9) on 9x 
                2020/04/19 recompiled for 11x add .gtkrc-2.0
                2020/04/25 add locale, fix pc files on 11x
                2023/03/16 factored out gtk-update-icon-cache (juanito)
                2024/03/20 v 2.24.33 on 15x, 2 exes moved (aus9) 
Current:        2024/03/21 remove libtool from install script 
  ' > $P.tcz.info

# remove duplicates below, using depends-on.sh
readelf -d $P/usr/local/bin/* | grep 'NEEDED'
#libgtk-x11-2.0.so.0]libgdk-x11-2.0.so.0]this TCE
#libXrender.so.1]libXrender chose libXcursor
#libXinerama.so.1]libXinerama 
#libXrandr.so.2]libXrandr 
#libXcursor.so.1]libXcursor 
#libXext.so.6]libXext-libXinerama 
#libpangocairo-1.0.so.0]libpangoft2-1.0.so.0]libpango-1.0.so.0]pango
#libX11.so.6]libX11->libXcomposite 
#libXcomposite.so.1]libXcomposite 
#libXdamage.so.1]libXdamage
#libXfixes.so.3]libXfixes->libXcursor 
#libatk-1.0.so.0]at-spi2-core
#libcairo.so.2]cairo->pango
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2
#libgio-2.0.so.0]libgobject-2.0.so.0]libgmodule-2.0.so.0]libglib-2.0.so.0]glib2->cairo->pango
#libharfbuzz.so.0]harfbuzz->freetype->fontconfig->cairo->pango
#libfontconfig.so.1]fontconfig->cairo->pango
#libfreetype.so.6]freetype->fontconfig->cairo->pango

readelf -d $P/usr/local/lib/gtk-2.0/2.10.0/engines/* | grep 'NEEDED' # no extras
readelf -d $P/usr/local/lib/gtk-2.0/2.10.0/immodules/* | grep 'NEEDED'  # no extras
readelf -d $P/usr/local/lib/gtk-2.0/2.10.0/printbackends/* | grep 'NEEDED'
#[libcups.so.2]libcups   MADE OPTIONAL DEPENDENCY

readelf -d $P/usr/local/lib/gtk-2.0/modules/* | grep 'NEEDED'
#[libgailutil.so.18]this TCE

readelf -d $P/usr/local/lib/*.so | grep 'NEEDED'  # no extras

echo 'libXcursor.tcz 
libXinerama.tcz 
libXrandr.tcz 
pango.tcz
libXcomposite.tcz 
libXdamage.tcz 
at-spi2-core.tcz
gdk-pixbuf2.tcz
adwaita-icon-theme.tcz
shared-mime-info.tcz             ' > $P.tcz.dep

submitqc --libs # not done 21 Mar 

# submitqc is not checking recursive deps IMHO so it added more
# including gtk2.tcz.....this is clearly a mistake!

# post submitqc deps replaced as above....I repeat I used readelf then depends-on.sh to reduce names in dep file
# reboot, no update to leafpad.....leafpad in terminal no complaints....opens ok