#!/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=lxterminal
V=0.4.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc xz vte-2.91-dev intltool"
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://downloads.sourceforge.net/lxde/$SRC.tar.xz   " $USER 
tar xvf $SRC*xz
cd $SRC
./configure --enable-gtk3 --disable-man  
make -j5 # seconds
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

# main 
# desktop stuff
################
# xterm is defined by install script
mkdir -p $P/usr/local/share/pixmaps
ICON=$P/usr/local/share/icons/hicolor/128x128/apps/
cp $ICON/$P.png $P/usr/local/share/pixmaps/

DESK=$P/usr/local/share/applications/$P.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/lxterminal.png' >> $DESK
sed 's|TryExec=lxterminal||' -i $DESK
sed 's|Exec=lxterminal||' -i $DESK
echo 'TryExec=xterm 
Exec=xterm ' >> $DESK

# xterm
#########
cat >> $P/usr/local/bin/$P-xterm <<'EOF'
#!/bin/busybox sh
# lxterminal xterm wrapper for Tiny Core Linux 5.x
# By bmarkus
# v1.0 - October 15, 2013

a=0
c=""

for v in $*
do
  if [ $a == 0 ]
  then
    if [ "$v" == "-g" ]
    then
      a=1
      c="--geometry="
    elif [ "$v" == "-e" ]
    then
      a=2
      c=$c$v" "
    fi
  elif [ $a == 1 ]
  then
    a=0
    c=$c$v" "
  elif [ $a == 2 ]
  then
    c=$c$v" "
  fi
done

lxterminal $c
EOF
chmod 755 $P/usr/local/bin/lxterminal-xterm

# tce.installed 
###############
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
# force xterm to lxterminal even if aterm loaded first
rm -rf /usr/local/bin/xterm
ln -s /usr/local/bin/lxterminal-xterm /usr/local/bin/xterm ' > $P/usr/local/tce.installed/$P 
chown root:staff -R $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING

# set nicer font for O vs 0 and increase size
sed 's|Monospace 10|mononoki 15|' -i $P/usr/local/share/lxterminal/lxterminal.conf

# use online manpage
rm -rf $P/usr/local/share/man

# TCZ them
###########
LIST2="$P $P-locale"
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:          lxterminal-locale.tcz
Description:    locales for lxterminal
Version:        0.4.0
Author:         lxterminal team
Original-site:  http://sourceforge.net/projects/lxde/files
Copying-policy: GPL v2
Size:           128K
Extension_by:   aus9 @ linuxquestions.org
Tags:           terminal locale 
Comments:       For non-English users of lxterminal.
                not tested by me
                
Change-log:     2015/08/01 First version  (gordon64)
                2018/10/14 0.3 -> 0.3.2 (aus9)  
                2019/01/03 recompiled for gnutls35
                2023/03/16 v 0.4.0 on 14x
Current:        2024/04/03 rebuilt for updated vte-2.91 on 15x  ' > $P-locale.tcz.info
echo 'lxterminal.tcz    ' > $P-locale.tcz.dep


echo 'Title:           lxterminal.tcz
Description:     gtk3 terminal for libX or wayland
Version:         0.4.0
Author:          lxterminal team
Original-site:   http://sourceforge.net/projects/lxde/files
Copying-policy:  GPL v2
Size:            48K
Extension_by:    aus9 @ linuxquestions.org
Tags:            terminal utf8
Comments:        This is a gtk3 terminal capable of UTF-8 display.
                 Allows multiple tabs. Online man page is here
                 https://linuxcommandlibrary.com/man/lxterminal

                 If not in Xorg, to adjust fontname or its size edit
                  .config/lxterminal/lxterminal.conf

                 Optional deps:  More font TCEs, load fontconfig
                 then load lxterminal please.

                 apps (GUI) can work with lxterminal due to xterm setup.
                 To use arrow key up/down in mc run in same tab
                 $ export TERM=xterm && mc
                 Better still try vifm TCE                            

Change-log:     2015/08/01 First version (gordon64) 
                2018/10/14 0.3 -> 0.3.2 (aus9)  
                2019/01/03 recompiled for gnutls35
                2023/03/16 v 0.4.0 on 14x
Current:        2024/04/03 rebuilt for updated vte-2.91 non 15x ' > $P.tcz.info

readelf -d $P/usr/local/bin/lxterminal | grep 'NEEDED'  # ignore TCBs
#libglib-2.0.so.0]libgio-2.0.so.0]libgobject-2.0.so.0]glib2->at-spi2-core->gtk3->vte-2.91-gtk3
#libgtk-3.so.0]libgdk-3.so.0]gtk3->vte-2.91-gtk3
#libpangocairo-1.0.so.0]libpango-1.0.so.0]pango->gtk3->vte-2.91-gtk3
#libharfbuzz.so.0]harfbuzz
#libatk-1.0.so.0]at-spi2-core->gtk3->vte-2.91-gtk3
#libcairo-gobject.so.2]libcairo.so.2]cairo->pango->gtk3->vte-2.91-gtk3
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2->gtk3->vte-2.91-gtk3
#libvte-2.91.so.0]vte-2.91-gtk3
#libX11.so.6]libX11->gdk-pixbuf2->gtk3->vte-2.91-gtk3

# adwaita-icon-theme removed and so no dep of gtk-update-icon either

echo 'vte-2.91-gtk3.tcz
harfbuzz.tcz
mononoki-ttf-fonts.tcz  ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

