#!/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 CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe \
-fno-exceptions -fno-rtti" 

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

# util-linux provides whereis needed by app
LIST="compiletc submitqc5 vte-2.90-dev  \
man-db-dev intltool gtk3-dev util-linux"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc http://lilyterm.luna.com.tw/file/lilyterm-0.9.9.4.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/wqov7sgsxhz37ih/lilyterm.png?dl=0  \
 -O /tmp/lilyterm.png" $USER 

# we are root for rest of commands
tar zxvf $P-0.9.9.4.tar.gz
cd $P-0.9.9.4
# configure default=/usr/local
./configure  --with-gtk=3.0 
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  
##########
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/doc $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share/

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

#  remove 128x128 add 32 size for wbar
rm -rf $P/usr/local/share/pixmaps/$P.png
cp /tmp/lilyterm.png $P/usr/local/share/pixmaps/lilyterm.png

# tceinstalled 
########## 
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
USER=`cat /etc/sysconfig/tcuser`
if [ ! -d /home/$USER/.config/lilyterm ] ; then
mkdir -p /home/$USER/.config/lilyterm
cp /usr/local/etc/xdg/lilyterm.conf /home/$USER/.config/lilyterm/default.conf
sudo chown $USER:staff -R /home/$USER/.config/lilyterm
sudo chmod 644  /home/$USER/.config/lilyterm/default.conf
fi 
' > $P/usr/local/tce.installed/lilyterm
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# 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 "shared-mime-info.tcz
adwaita-icon-theme.tcz
gtk3.tcz
vte-2.90.tcz
ncurses.tcz
libepoxy.tcz
libXcomposite.tcz
libXcursor.tcz
libXinerama.tcz
Xorg-fonts.tcz
 " > lilyterm.tcz.dep

echo "lilyterm.tcz" > lilyterm-locale.tcz.dep
echo "man-db.tcz" > lilyterm-doc.tcz.dep

# create info files let submitqc5 add size

echo "Title:          lilyterm.tcz  TESTING
Description:    UTF8 terminal 
Version:        0.9.9.4 
Author:         Lu, Chao-Ming   
Original-site:  http://lilyterm.luna.com.tw/index.html
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           lilyterm utf8 terminal 
Comments:       This is a gtk3 terminal capable of UTF-8 display.
               	Allows multiple tabs thru keyboard or mouse actions. 
                If you=RH mouse user,  RH click inside terminal 
                to get options 
                -> click on usage to get lots of hints
                -> click on user profile to save or create new profile etc
                If not in Xorg, to adjust fontname or its size do it 
                by edit of .config/lilyterm/default.conf
          
 
		Compiled for Corepure64 6.x
Change-log:     2015/07/30 First version 
Current:        2015/07/30
" > lilyterm.tcz.info

echo "Title:          lilyterm-doc.tcz
Description:    Mainly man page for lilyterm
Version:        0.9.9.4 
Author:         Lu, Chao-Ming 
Original-site:  http://lilyterm.luna.com.tw/index.html
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           lilyterm 
Comments:       Contains man page and some docs

		Compiled for Corepure64 6.x
Change-log:     2015/07/30 First version 
Current:        2015/07/30
" > lilyterm-doc.tcz.info

echo "Title:          lilyterm-locale.tcz
Description:    locales for lilyterm
Version:        0.9.9.4 
Author:         Lu, Chao-Ming
Original-site:  http://lilyterm.luna.com.tw/index.html
Copying-policy: GPL v3
Size:		
Extension_by:   gordons64
Tags:           lilyterm locale 
Comments:       For non-English users of lilyterm.

		Compiled for Corepure64 6.x
Change-log:     2015/07/30 First version 
Current:        2015/07/30 
" > lilyterm-locale.tcz.info

submitqc5