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

CFLAGS="-march=i486 -mtune=i686 -Os -pipe   " 
CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti  "
echo $CFLAGS && echo $CXXFLAGS

P=lxterminal
V=0.4.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget vte-2.91-dev intltool"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# list may be larger than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/liblto_plugin.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

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 --pref=/usr/local --disable-man --enable-gtk3 \
vte_CFLAGS="-I/usr/local/include/vte-2.91" vte_LIBS="-lvte-2.91" 
make -j4  # 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 - icon
################
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/
echo 'X-FullPathIcon=/usr/local/share/pixmaps/lxterminal.png' >> $P/usr/local/share/applications/$P.desktop

# 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 0
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 (zero) and increase size
sed 's|Monospace 10|mononoki 15|' -i $P/usr/local/share/lxterminal/lxterminal.conf

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
Tags:           terminal locale 
Comments:       For non-English users of lxterminal.
                not tested by me

Change-log:     2023/08/03 v 0.4.0 14.x
Current:        2023/08/03              ' > $P-locale.tcz.info
echo 'lxterminal.tcz    ' > $P-locale.tcz.dep

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

                 Instead of Edit->Preferences you can edit
                 $HOME/.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 mc edit preferences to disable F10 so F10 will exit mc
                 manual edit would be "disablef10=true"

Change-log:      2023/08/03 v 0.4.0 14.x 
Current:         2023/08/03            ' > $P.tcz.info

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

# ncurses-terminfo, glib-networking  added after I saw an error msg
echo 'vte-2.91.tcz
harfbuzz.tcz
libX11.tcz
adwaita-icon-theme.tcz 
mononoki-ttf-fonts.tcz
ncursesw-terminfo.tcz
glib-networking.tcz  ' > $P.tcz.dep

