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

#homepage=http://ignorantguru.github.io/udevil/
name=udevil
pkgver=0.4.3
download="https://github.com/IgnorantGuru/udevil/tarball/master"
pkgdir="/usr/local"
kerbuild=$(uname -r) 

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

deps="compiletc.tcz \
bash.tcz \
glib2-dev.tcz \
udev-lib-dev.tcz \
wget.tcz \
filesystems-${kerbuild}.tcz \
sed.tcz  \
pkg-config.tcz  \
libtool-dev.tcz \
acl-dev.tcz \
intltool.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/${name}
cd /tmp

if [ -f ${name}*.gz ]; then
   cp /tmp/${name}*.gz /tmp/${name}/${name}*.gz
else
   wget -O udevil.tar.gz https://github.com/IgnorantGuru/udevil/tarball/master
fi

cd /tmp/${name}
[ -f ${name}*.gz ] && tar xzf ${name}*.gz

[ -d IgnorantGuru-udevil-* ] || echo -e " Error..."
cd IgnorantGuru-udevil-*

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

./configure --prefix=/usr/local --disable-static --disable-systemd

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

make

sudo touch /tmp/NOW
sleep 5

sudo make install
#to create extension install directories
#sudo make install DESTDIR=/tmp/${name}/stage


cd /usr/local/bin
sudo strip --strip-debug --strip-unneeded $name

sudo chmod +s /usr/local/bin/udevil

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

exit
