#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi
# config says #########-fno-rtti -fno-exceptions... no
export CFLAGS="-mtune=generic -Os -pipe"
export CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe " 

P=Eterm
SRC=$P-0.9.6
USER=`cat /etc/sysconfig/tcuser`

LIST="compiletc submitqc libast-dev libX11-dev xorg-proto libXt-dev imlib2-dev "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "wget -nc http://www.eterm.org/download/Eterm-0.9.6.tar.gz \                        
" $USER 

# use 32x32 icon from lilyterm
su -c "wget -nc http://repo.tinycorelinux.net/6.x/x86_64/tcz/src/lilyterm/lilyterm.png \
" $USER 

# we are root for rest of commands
tar xvf Eterm*gz
cd $SRC
./configure  --prefix=/usr/local 
make -j3
mkdir -p /tmp/$P
make install DESTDIR=/tmp/$P

# Strip 
########
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

# dev
###################################################################
cd /tmp
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/

# accompanied MIT 
mkdir -p $P-dev/usr/local/share/doc/$P-dev
cp $SRC/LICENSE $P-dev/usr/local/share/doc/$P-dev/COPYING


# doc  
#####################################################################
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/man $P-doc/usr/local/share/

# create accompanied MIT for doc
mkdir -p $P-doc/usr/local/share/doc/$P-doc
cp $SRC/LICENSE $P-doc/usr/local/share/doc/$P-doc/COPYING

# main
##################################################################
mkdir -p $P/usr/local/share/applications/
mkdir -p $P/usr/local/share/pixmaps
echo '[Desktop Entry]
Encoding=UTF-8
Name=Eterm
GenericName=Terminal
Comment=Use the command line
TryExec=Eterm
Exec=Eterm
Icon=Eterm
Type=Application
Categories=System;TerminalEmulator;
Keywords=console;command line;execute;
X-FullPathIcon=/usr/local/share/pixmaps/Eterm.png
' > $P/usr/local/share/applications/$P.desktop
cp lilyterm.png $P/usr/local/share/pixmaps/Eterm.png

# main accompainied MIT  
#########################
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P/COPYING

# TCZ them
#######
LIST2="$P $P-doc $P-dev"
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

echo 'libast.tcz
libXres.tcz' > $P.tcz.dep

echo 'man-db.tcz
ncurses5.tcz' > $P-doc.tcz.dep

echo 'compiletc.tcz
submitqc.tcz 
libast-dev.tcz
libX11-dev.tcz 
xorg-proto.tcz
libXt-dev.tcz 
imlib2-dev.tcz' > $P-dev.tcz.dep

# create info files let submitqc add size

echo 'Title:          Eterm.tcz  
Description:    light terminal 
Version:        0.9.6
Author:         Michael Jennings  
Original-site:  http://www.eterm.org/
Copying-policy: accompained MIT
Size:		
Extension_by:   aus9 @ linuxquestions.org
Tags:           eterm terminal 
Comments:       non-VTE terminal works on most window managers
                OPTIONAL to allow newbies to click the ? symbol to bring up help,
                --please load Eterm-doc                        
 
                Compiled for Corepure64 8.x
Change-log:     2017/08/06  First version 
Current:        2017/08/06
' > $P.tcz.info

echo 'Title:          Eterm-doc.tcz
Description:    Mainly man page for eterm
Version:        0.9.6 
Author:         Michael Jennings
Original-site:  http://www.eterm.org/
Copying-policy: accompained MIT
Size:		
Extension_by:   aus9 @ linuxquestions.org
Tags:           eterm 
Comments:       Contains man page and some docs

                Compiled for Corepure64 8.x
Change-log:     2017/08/06  First version 
Current:        2017/08/06 
' > $P-doc.tcz.info

echo 'Title:          Eterm-dev.tcz
Description:    dev files for eterm
Version:        0.9.6
Author:         Michael Jennings
Original-site:  http://www.eterm.org/
Copying-policy: accompained MIT
Size:		
Extension_by:   aus9 @ linuxquestions.org
Tags:           eterm 
Comments:       Developemnt files

                Compiled for Corepure64 8.x
Change-log:     2017/08/06  First version 
Current:        2017/08/06
' > $P-dev.tcz.info
submitqc