#!/bin/sh -x
# build script for lsof
# built on tc-6.x

set -o nounset
set -o errexit

. /etc/init.d/tc-functions

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

# Downloads

# ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_4.88.tar.bz2

# download the above src file, extract and run this script from the src directory

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

## load build deps
tcepath="/etc/sysconfig/tcedir/optional"
tceinstalled=/usr/local/tce.installed

echo -e "\n\nFetching build dependencies... "

deps="compiletc.tcz \
intltool.tcz \
glib2-dev.tcz \
autogen-dev.tcz \
libtool-dev.tcz  \
automake.tcz  \
pkg-config.tcz  \
gzip.tcz \
bzip2-lib.tcz \
xdg-utils.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 ${tcepath}/"$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 ${tcepath}/"$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 1
fi

done

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


export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
find . -name Makefile -type f -exec sed -i 's/-O2//g' {} \;


./Configure linux

make -j3 

sudo strip --strip-debug --strip-unneeded lsof

exit 2




