#!/bin/sh -x
# build script for davfs2
# built on tc-7.x


pkgname=davfs2
pkgver=1.5.2
source="http://download.savannah.gnu.org/releases/davfs2/davfs2-1.5.2.tar.gz"
#pkgdir="/tmp/${pkgname}_install"
pkgdir="/usr/local"

## load build deps
tceinstalled='/usr/local/tce.installed'
tcepath='/etc/sysconfig/tcedir/optional'
kerbuild=$(uname -r)


deps="compiletc.tcz \
expat2.tcz \
neon.tcz \
openssl-dev.tcz \
fuse-dev.tcz \
bash.tcz \
glib2-dev.tcz \
wget.tcz \
filesystems-${kerbuild}.tcz \
sed.tcz  \
pkg-config.tcz  \
libtool-dev.tcz \
intltool.tcz \
acl-dev.tcz \
util-linux.tcz"


for i in `echo $deps`; do 

depname=`basename "$i" .tcz`

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

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

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

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

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

done

sleep 1


mkdir /tmp/${pkgname}
cd /tmp/${pkgname}
wget $source

davfs2-1.5.2.tar.gz

if [ -f ${pkgname}*.tar.gz ]; then
	tar -zxvf ${pkgname}*.tar.gz
else
   echo -e "\n\nerror... "
   exit 9 
fi

kerarch=$(uname -m)
set +x; echo -e "\n\n ${BLUE}Export compiler flags... ${NORMAL}"; set -x
if [ x"$kerarch" == xx86_64 ]; then
# x86_64
echo "compile flags for $kerarch"
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"
else
# x86
echo "compile flags for $kerarch"
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
fi

cd $pkgname-$pkgver

./configure --prefix=/usr/local 

find . -name Makefile -type f -exec sed -i 's/-O2//g' {} \;

make

sudo touch /tmp/NOW
sleep 5

sudo make install

cd /usr/local/sbin
sudo strip --strip-debug --strip-unneeded mount.davfs
sudo strip --strip-debug --strip-unneeded umount.davfs

cd ~/

## additional optional commands
[ -d /home/tc/${pkgname}_raw ] || mkdir -p /home/tc/${pkgname}_raw
sudo find /usr -newer /tmp/NOW -not -type d > /home/tc/${pkgname}_raw/${pkgname}_raw.list
sudo find /etc -newer /tmp/NOW -not -type d >> /home/tc/${pkgname}_raw/${pkgname}_raw.list
sudo find /lib -newer /tmp/NOW -not -type d | grep /lib/modules/${kerbuild}/kernel/drivers/ >> /home/tc/${pkgname}_raw/${pkgname}_raw.list
cd /home/tc/${pkgname}_raw
sudo tar -T ${pkgname}_raw.list -czvf ${pkgname}_raw.tar.gz

exit
