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

LIST1="compiletc submitqc5 squashfs-tools-4.x cmake gettext \
libgcrypt-dev gnutls-dev curl-dev ncursesw-dev ncurses-dev  \
tcl-dev perl5 aspell-dev python-dev lua-dev "
for Z in $LIST1
do 
	su -c "tce-load -w $Z" tc
	su -c "tce-load -i $Z" tc
done

# guile-dev not in repo ignore
# ruby ruby-dev are not in repo At the moment--rebuild later
# only needed if weechat-scripts is useful for ruby script

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

# start
cd /tmp
su -c "/usr/local/bin/wget \
-nc http://weechat.org/files/src/weechat-0.4.1.tar.bz2 \
-O /tmp/weechat-0.4.1.tar.bz2" tc

W=weechat
L=weechat-locale
V=weechat-dev
# don't build doc as its only one file

tar jxvf wee*.bz2
cd $W-0.4.1/
cmake .
make -j3
make DESTDIR=/tmp/$W install

############# strip post install
cd /tmp/$W
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

# locale
########
cd /tmp
mkdir -p $L/usr/local/share
mv $W/usr/local/share/locale $L/usr/local/share

# dev
#####
mkdir -p $V/usr/local/lib
mv $W/usr/local/include $V/usr/local
mv $W/usr/local/lib/pkgconfig $V/usr/local/lib

# main --ADD desktop
mkdir -p $W/usr/local/share/applications
echo "[Desktop Entry]                                      
Name=weechat                                       
Exec=aterm -e weechat-curses                                
Terminal=true                                      
Type=Application                                   
Icon=weechat                                      
X-FullPathIcon=/usr/local/share/icons/hicolor/32x32/apps/weechat.png
" > $W/usr/local/share/applications/weechat.desktop 

# ADD authors
mkdir -p $W/usr/local/share/doc/weechat
cp weechat-0.4.1/AUTHORS $W/usr/local/share/doc/weechat 


# TCZ them
#######
LIST2="$W $L $V"
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

######### finish

echo "aterm.tcz
gnutls.tcz
ncursesw.tcz
libiconv.tcz
curl.tcz
" > weechat.tcz.dep

submitqc5
