#!/bin/sh -x
# build script for fluxbox-1.3.5
# built on tc-5.x

set -o nounset
set -o errexit

. /etc/init.d/tc-functions

###################################################################################################
###################################################################################################
#
# To run, copy fluxbox-1.3.5.tar.gz and this build script in /tmp directory and run it  
#
# 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' and build deps   
# (configure option will need to be changed to '-enable' if fribidi is desired).
#
# debug files are always created and may be found in the build directory, look for 
# configure_results.txt &
# make_results.txt
#
###################################################################################################
###################################################################################################

# Downloads

# fribidi download (optional)
# 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

name=fluxbox
srcname=`find . -iname 'fluxbox*.tar*' -printf '%f\n' | sed 's/.tar.[a-z0-9]*//'`
ver=`echo $srcname | sed -e 's/fluxbox-//'`
tcepath="/etc/sysconfig/tcedir/optional"
pkg=/tmp/fluxbox_build/pkg
loc=/tmp/fluxbox_build/loc
pkgdir=/tmp/fluxbox_build
tceinstalled=/usr/local/tce.installed

## test $tcedir (tce-extensions path)
get_inum() {
ls -i | grep "optional" | awk '{print $1}'
}

tce_path1=`echo "$tcepath" | sed -e 's/\/optional//'`
inum1=`cd "$tce_path1"; get_inum`

tce_path2=`sudo find /mnt -name tce`
inum2=`cd "$tce_path2"; get_inum`
cd /tmp

if [ "$inum1" == "$inum2" ]; then 
	echo "${CYAN}tce path = ${GREEN}ok${NORMAL}"
else
	echo "${RED}check tce path${NORMAL}"; exit 1
fi

##test for accessible src files
if [ ! -f /tmp/fluxbox*.tar* ]; then
	echo "${RED}no src pkg found${NORMAL}"
	exit 2
fi


## load build deps
echo -e "\n\nFetching build dependencies... "

deps="compiletc.tcz \
linux-3.8.3_api_headers.tcz \
Xorg-7.7-3d-dev.tcz \
imlib2-dev.tcz \
freetype-dev.tcz  \
glibc_base-dev.tcz  \
glibc_gconv.tcz  \
glibc_apps.tcz  \
libiconv-dev.tcz \
libtool-dev.tcz  \
automake.tcz  \
pkg-config.tcz  \
xdg-utils.tcz  \
sed.tcz  \
binutils.tcz  \
imake.tcz  \
coreutils.tcz  \
util-linux.tcz"


for i in `echo $deps`; do 

pkgname=`basename "$i" .tcz`

# check if tcz installed already
if [ -f ${tceinstalled}/"$pkgname" ]; then
	echo "${WHITE}"$i" ${GREEN}Already loaded${NORMAL}"

else
	if  [ ! -f ${tceinstalled}/"$pkgname" ]; then

# tcz not installed, then install from local path
	if [ -f ${tce_path2}/optional/"$i" ]; then
	  while [ ! -f ${tceinstalled}/"$pkgname" ]; do
	    tce-load -i "$pkgname"; sleep 0.5
	  done
		if [ -f ${tceinstalled}/"$pkgname" ]; then
			echo "${CYAN}"$i" ${BLUE}loaded locally${NORMAL}"
		fi

# tcz not downloaded, install from repo
	elif [ ! -f ${tce_path2}/optional/"$i" ]; then
	    tce-load -wil "$pkgname"; sleep 0.5
		if [ -f ${tceinstalled}/"$pkgname" ]; then
			echo "${MAGENTA}"$i" ${YELLOW}downloaded from repo${NORMAL}"
		fi
	fi
	fi
fi

if [ ! -f "${tceinstalled}/$pkgname" ]; then
	echo "${RED}$i failed to load, exiting..${NORMAL}"; exit 8
fi

done

#sudo /sbin/ldconfig -v

sleep 1
#cleanup
echo -e "\n\n Cleaning up previous builds, starting fresh... "
[ -d /tmp/"$srcname" ] && find /tmp/${srcname} -type d -print0 | xargs -0 -r sudo rm -rvf

sleep 1.5

if [ -d $srcname ]; then
	echo -e "\n\n Previous build directory found, discard before continuing... "
	sleep 10
	exit 1
fi
if [ ! -f ${srcname}*.tar.gz ]; then
	echo -e "\n\n ${srcname}*.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 /tmp/$srcname
[ -d /tmp/$srcname ] && tar xpvf $srcname.tar.[gx]z -C /tmp

echo -e "\n\n Setting file ownership..."
[ -d /tmp/$srcname ] && sudo chown tc.staff -R /tmp/$srcname


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


cd /tmp/$srcname

#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

cd src
sudo strip --strip-debug --strip-unneeded fluxbox
cd ..

sudo make install DESTDIR=$(pwd)/stage

echo -e "\n\n Setting file ownership..."
[ -d $(pwd)/stage ] && sudo chown tc.staff -R $(pwd)/stage

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




