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

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe"
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -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 "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# list may have more than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
cd /tmp
rm -rf /usr/lib/*.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 --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 - 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

# 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
Tags:           terminal locale 
Comments:       For non-English users of lxterminal.
                not tested by me
                
Change-log:     2023/05/25 v 0.4.0 14x
Current:        2023/05/25 ' > $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:            48K
Extension_by:    aus9
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

                 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/05/25 v 0.4.0 14x aarch64
Current:        2023/05/25' > $P.tcz.info

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

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

submitqc --libs

