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

###########################
# PLEASE INJECT libglib-2.0.la
##########################

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
export LDFLAGS="-Wl,-O1"

P=gtk2
V=2.24.32
SRC=gtk+-2.24.32
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc gettext glib2-dev atk-dev atk-gir gdk-pixbuf2-dev \
gdk-pixbuf2-gir cups-dev gobject-introspection-dev pango-dev pango-gir \
libXinerama-dev libXrandr-dev libXcursor-dev libXcomposite-dev.tcz "
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 \
http://ftp.gnome.org/pub/gnome/sources/gtk+/2.24/gtk+-$V.tar.xz " $USER
mkdir $P
xz -d gt*xz && tar xvf gt*tar
cd $SRC
./configure --prefix=/usr/local --enable-cups --disable-debug --enable-introspection=yes
# make takes 2M10s install takes 20Sec
make -j5
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
#####
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/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 

mkdir -p $P-dev/usr/local/lib/gtk-2.0/modules
mv $P/usr/local/lib/gtk-2.0/include $P-dev/usr/local/lib/gtk-2.0/
mv $P/usr/local/lib/gtk-2.0/modules/*a $P-dev/usr/local/lib/gtk-2.0/modules/
chmod 644 $P-dev/usr/local/lib/gtk-2.0/modules/*a
mkdir -p $P-dev/usr/local/lib/gtk-2.0/2.10.0
cd $P-dev/usr/local/lib/gtk-2.0/2.10.0
mkdir engines immodules printbackends
cd /tmp
mv $P/usr/local/lib/gtk-2.0/2.10.0/engines/*a $P-dev/usr/local/lib/gtk-2.0/2.10.0/engines/
mv $P/usr/local/lib/gtk-2.0/2.10.0/immodules/*a $P-dev/usr/local/lib/gtk-2.0/2.10.0/immodules/
mv $P/usr/local/lib/gtk-2.0/2.10.0/printbackends/*a $P-dev/usr/local/lib/gtk-2.0/2.10.0/printbackends/
chmod 644 $P-dev/usr/local/lib/gtk-2.0/2.10.0/engines/*
chmod 644 $P-dev/usr/local/lib/gtk-2.0/2.10.0/immodules/*a
chmod 644 $P-dev/usr/local/lib/gtk-2.0/2.10.0/printbackends/*a

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
#####
mkdir -p $P-doc/usr/local/share/doc
mv $P/usr/local/share/gtk-doc $P-doc/usr/local/share/doc/$P
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
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P

# main
#####
mkdir -p $P/usr/local/share/doc/$P
echo 'see doc tcz for license' > $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

# 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 tc:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# 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.tcz
Description:    Gimp Tool Kit
Version:        2.24.32
Author:         see -doc/AUTHORS
Original-site:  http://www.gtk.org
Copying-policy: LGPL v2
Size:           2.2M		
Extension_by:   aus9 
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 dependency lxappearance to modify config
                
                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
 
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
Current:        2020/04/25 add locale, fix pc files on 11x
 ' > $P.tcz.info

echo 'Title:          gtk2-dev.tcz
Description:    dev files 
Version:        2.24.32
Author:         see -doc/AUTHORS
Original-site:  http://www.gtk.org
Copying-policy: LGPL v2
Size:           512K		
Extension_by:   aus9 
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
Current:        2020/04/25 add locale, fix pc files on 11x
' > $P-dev.tcz.info

echo 'Title:          gtk2-doc.tcz
Description:    docs 
Version:        2.24.32
Author:         see -doc/AUTHORS
Original-site:  http://www.gtk.org
Copying-policy: LGPL v2
Size:           4.2M		
Extension_by:   aus9 
Tags:           gtk2
Comments:       Docs

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
Current:        2020/04/25 add locale, fix pc files on 11x
' > $P-doc.tcz.info

echo 'Title:          gtk2-gir.tcz
Description:    gir files for gtk2
Version:        2.24.32
Author:         see -doc/AUTHORS
Original-site:  http://www.gtk.org
Copying-policy: LGPL v2
Size:           1.9M		
Extension_by:   aus9 
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
Current:        2020/04/25 add locale, fix pc files on 11x
' > $P-gir.tcz.info

echo 'Title:          gtk2-locale.tcz
Description:    locales
Version:        2.24.32
Author:         see -doc/AUTHORS
Original-site:  http://www.gtk.org
Copying-policy: LGPL v2
Size:           7.2M		
Extension_by:   aus9 
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
Current:        2020/04/25 add locale, fix pc files on 11x
' > $P-locale.tcz.info

echo 'atk-dev.tcz 
atk-gir.tcz 
cups-dev.tcz 
gdk-pixbuf2-dev.tcz 
gdk-pixbuf2-gir.tcz 
gettext.tcz 
glib2-dev.tcz 
gobject-introspection-dev.tcz 
gtk2.tcz
libXcomposite-dev.tcz
libXcursor-dev.tcz
libXinerama-dev.tcz
libXrandr-dev.tcz
pango-dev.tcz 
pango-gir.tcz ' > $P-dev.tcz.dep

echo 'atk.tcz
adwaita-icon-theme.tcz
gdk-pixbuf2.tcz
libXcomposite.tcz
libXcursor.tcz
libXinerama.tcz
libXrandr.tcz
pango.tcz
shared-mime-info.tcz
Xorg-7.7-lib.tcz ' > $P.tcz.dep

echo 'gtk2.tcz' > $P-gir.tcz.dep
echo 'gtk2.tcz' > $P-locale.tcz.dep

locale needs rebuild
submitqc --libs