#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=lxterminal
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc5 gtk3-dev vte-2.90-dev vte-2.90 \
util-linux man-db-dev intltool adwaita-icon-theme"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc http://internode.dl.sourceforge.net/project/lxde/\
LXTerminal%20%28terminal%20emulator%29/LXTerminal%200.2.0/lxterminal-0.2.0.tar.gz \                                  
" $USER 

# download 32 png as compile makes 128x128----I used mtpaint to rescale
su -c "/usr/local/bin/wget -nc --no-check-certificate https://www.dropbox.com/s/v29xznv1v1utxx3/lxterminal.png?dl=0 \
 -O /tmp/lxterminal.png" $USER 

# we are root for rest of commands
tar zxvf $P-0.2.0.tar.gz
cd $P-0.2.0
# configure default=/usr/local
./configure --enable-gtk3 --enable-man 
make -j3
mkdir -p /tmp/$P
make install DESTDIR=/tmp/$P

# Strip executables
cd /tmp/$P
find . | xargs file | grep "executable"    | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
cd /tmp
##############
# create locale
##############
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# create doc  -- man and add authors
################################### 
mkdir -p $P-doc/usr/local/share/doc/lxterminal
cp $P-0.2.0/AUTHORS $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/

# main --desktop
############
rm -rf $P/usr/local/share/applications/lxterminal.desktop
echo "[Desktop Entry]
Encoding=UTF-8
Name=LXTerminal
GenericName=Terminal
Comment=Use the command line
TryExec=lxterminal
Exec=lxterminal
Icon=lxterminal
Type=Application
Categories=GTK;System;TerminalEmulator;
Keywords=console;command line;execute;
X-FullPathIcon=/usr/local/share/pixmaps/lxterminal.png
" >> $P/usr/local/share/applications/$P.desktop

# current icon is 128x128, add 32 size for wbar
# terminal command expects to find 128 so left intact
mkdir $P/usr/local/share/pixmaps
cp /tmp/lxterminal.png $P/usr/local/share/pixmaps/

# main add tce.installed 
mkdir -p $P/usr/local/tce.installed

echo "#!/bin/sh
[ -f /usr/local/bin/xterm ] || ln -s /usr/local/bin/lxterminal-xterm /usr/local/bin/xterm
" > $P/usr/local/tce.installed/$P
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# main add lxterminal-xterm by bmarkus
echo "#!/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 
" > $P/usr/local/bin/lxterminal-xterm

chmod 775 $P/usr/local/bin/lxterminal-xterm

# TCZ them
#######
LIST2="$P $P-doc $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
	cd /tmp
done

# create depend files
echo "gtk3.tcz
vte-2.90.tcz
Xorg-fonts.tcz
libepoxy.tcz
ncurses.tcz
xcursor-themes.tcz
 " > lxterminal.tcz.dep

echo "lxterminal.tcz" > lxterminal-locale.tcz.dep

# create info files let submitqc5 add size

echo "Title:          lxterminal.tcz
Description:    terminal for LXDE or can run alone
Version:        0.20
Author:         7 authors see doc 
Original-site:  http://sourceforge.net/projects/lxde/files
Copying-policy: GPL v2
Size:		
Extension_by:   gordons64
Tags:         terminal utf8  
Comments:      This is a gtk3 terminal capable of UTF-8 display.
               	Allows multiple tabs. 

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

		Optional deps:  More font TCZs, load fontconfig
		then load lxterminal please.
                        
		Compiled for Corepure64 6.x
                -- x86 (32 bit) maintainer bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01
" > lxterminal.tcz.info

echo "Title:          lxterminal-doc.tcz
Description:    docs  for lxterminal
Version:        0.20
Author:         7 see doc 
Original-site:  http://sourceforge.net/projects/lxde/files
Copying-policy: GPL v2
Size:		
Extension_by:   gordons64
Tags:          terminal 
Comments:      Contains man page and 7 AUTHORS

		Compiled for Corepure64 6.x
                -- X86 (32 bit) maintainer bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01
" > lxterminal-doc.tcz.info

echo "Title:          lxterminal-locale.tcz
Description:    locales for lxterminal
Version:        0.20
Author:         7 see doc
Original-site:  http://sourceforge.net/projects/lxde/files
Copying-policy: GPL v2
Size:		
Extension_by:   gordons64
Tags:          terminal locale 
Comments:      For non-English users of lxterminal.

		Compiled for Corepure64 6.x
                -- X86 (32 bit) maintainer bmarkus
Change-log:     2015/08/01 First version 
Current:        2015/08/01 
" > lxterminal-locale.tcz.info

submitqc5