#!/bin/sh
# build script for fluxbox-1.3.5
# built on tc-4.7.4

set -o nounset
set -o errexit

###################################################################################################
###################################################################################################
# fribidi Note:  can be compiled with or without fribidi, but if desired use the latest version
# (tested OK with fribidi-0.19.5)  then add new frididi extension to 'fluxbox.tcz.dep file'   
# (configure option will need to be changed to '-enable' if fribidi is desired).
###################################################################################################
###################################################################################################

# Downloads

# fribidi download
# http://www.linuxfromscratch.org/blfs/view/svn/general/fribidi.html

# fluxbox download git version
# git clone git://git.fluxbox.org/fluxbox.git && cd fluxbox && ./autogen

# fluxbox download tarball version
# http://fluxbox.org/download/

###################################################################################################

# Build fluxbox

echo -e "\n\nFetching build dependencies... "
tce-load -wil compiletc Xorg-7.6-dev Xorg-7.6-lib Xlibs libxft-dev fontconfig-dev freetype_base-dev \
	pixman-dev eglibc_base-dev eglibc_gconv eglibc_apps linux-headers-3.0.21-tinycore imake \
	libiconv-dev libtool-dev automake libxcb-dev pkg-config xdg-utils sed \
	binutils coreutils util-linux
sleep 3

SRC=""
SRC=/tmp/fluxbox-1.3.5


echo -e "\n\n Cleaning up previous builds, starting fresh... "
[ -d $SRC ] && find $SRC/ -type d -print0 | xargs -0 -r sudo rm -rvf

if [ -d $SRC ]; then
	echo -e "\n\n Previous build directory found, discard before continuing... "
	sleep 10
	exit 1
fi
if [ ! -f $SRC.tar.gz ]; then
	echo -e "\n\n fluxbox.tar.gz not found, copy src tarball to /tmp before continuing... \n\n"
	sleep 10
	exit 1
fi


echo -e "\n\n Unpacking src... "
mkdir -p $SRC
[ -d $SRC ] && tar xvzf $SRC.tar.gz -C /tmp

# this is probably not rerquired but..  
echo -e "\n\n Setting file ownership..."
[ -d $SRC ] && sudo chown tc.staff -R $SRC


echo -e "\n\n Export compiler flags... "
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

cd $SRC 

echo -e "\n\n \"A reminder to review fribidi notes\" (this build script, Ctrl+C to esc) "
echo -e "\n Stop here to build fribidi if needed, or\n "press enter" to continue with ./configure... "
read
echo -e "\n Ok moving right along...\n\n Runing configure..."
./configure --prefix=/usr/local --enable-xinerama --enable-randr --enable-shape \
	--enable-imlib2 --enable-xft --enable-xmb --enable-xpm --enable-ewmh --enable-nls \
	--disable-fribidi | tee configure_results.txt



echo -e "\n\n Running make + make install..."
make -j3 | tee make_results.txt
sudo make install-strip DESTDIR=$(pwd)/stage

echo -e "\n Done"
echo "\n Completed files can be found in $(pwd)/stage \n "
exit 2
