#!/bin/sh -x
# build script for oss_4.2 (git)
# built on tc-5.x 3.8.13

set -o nounset
set -o errexit

. /etc/init.d/tc-functions
useBusybox

###################################################################################################
###################################################################################################
# Downloads available

# git clone http://git.code.sf.net/p/opensound/git

# http://www.4front-tech.com/developer/sources/stable/gpl/


###################################################################################################
# (This script assumes git is available otherwise an oss source archive created 			
# using git from another system is needed, also remove git.tcz from the deps.
# only the git source compiles)
#
###################################################################################################

# Build oss v4.2 (git source)

name='oss'
srcpath='/usr/src/'
srcdir='oss_git'
builddir='oss_build'
ver='4.2'
pkg=/tmp/${name}_build/pkg
doc=/tmp/${name}_build/doc
pkgdir=/tmp/${name}_build
src=/usr/$srcdir


##############################################
## load deps

tceinstalled="/usr/local/tce.installed"
optional_path="/etc/sysconfig/tcedir/optional"
cd $optional_path
tce_path=$(realpath ${PWD%/*})



deps="compiletc.tcz \
automake.tcz \
findutils.tcz \
gtk3-dev.tcz \
linux-kernel-sources-env.tcz \
gawk.tcz \
glibc_base-dev.tcz \
git.tcz"

for i in $deps ; do 

pkgname=${i%.tcz}

if [ ! -f ${tce_path}/optional/"$i" ]; then
	tce-load -wil "$pkgname"
	[ x$? != x0 ] && tce-load -wil "$pkgname"
	[ -f ${tceinstalled}/"$pkgname" ] || exit
elif [ ! -f ${tceinstalled}/"$pkgname" ]; then
	tce-load -i "$pkgname"
fi
done


##############################################
# sudo /sbin/ldconfig -v && sleep 2


##############################################
#cleanup
echo -e "\n\n Cleaning up previous builds, start fresh... "
[ -d "${srcpath}$srcdir" ] && sudo rm -r /usr/src/$srcdir
[ -d /tmp/oss_build ] && sudo rm -r /tmp/oss_build

##############################################
# prep kernel sources

cliorx linux-kernel-sources-env.sh

##############################################
# pkg sources

if [ -d "${srcpath}$srcdir" ]; then
	echo -e "\n\n Previous build directory found, discard before continuing... "
	sleep 10
	exit 1
fi


echo -e "\n\n Unpacking src... "
sudo mkdir -p /usr/src/$srcdir

[ $PWD == /tmp ] || cd /tmp

#sudo find / -name "${name}*.tar.gz" -type f -exec sh -c exec cp -f "$@" /tmp X {} +

if [ ! -f /tmp/${name}*.tar.bz2 ]; then
	echo -e "\n\n ${name}*.tar.bz2 not found, downloading from git source...\n\n"
	cd /usr/src
	sudo git clone http://git.code.sf.net/p/opensound/git $srcdir
else 
	sudo tar jxvf oss*.tar.bz2 -C /usr/src/$srcdir
fi

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



echo -e "\n\n Export compiler flags... "
case $( uname -r ) in
3.8.13-tinycore) 
echo "compile flags for x86"
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
;;
3.8.13-tinycore64) 
echo "compile flags for x86_64"
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"
;;
esac

mkdir -p /tmp/$builddir
cd /tmp/${builddir}

echo -e "\n Ok moving right along...\n\n Runing configure..."
${srcpath}${srcdir}/configure


echo -e "\n\n Running make install..."
sudo make install


##package

mkdir -p /tmp/pkg/lib/modules/$(uname -r)/kernel/oss

sudo cp -R /lib/modules/$(uname -r)/kernel/oss /tmp/pkg/lib/modules/$(uname -r)/kernel

cd /tmp/pkg/lib/modules/$(uname -r)/kernel/oss

for i in * ; do
sudo strip --strip-debug --strip-unneeded $i
sudo gzip $i
done

cd /tmp
if [ -f ${tceinstalled}/squashfs-tools-4.x ]; then 
	mksquashfs pkg/ OSS-modules-3.8.13-tinycore.tcz
	echo -e "\n \"OSS-modules-3.8.13-tinycore.tcz\" modules can be found in /tmp"
else
	echo -e "\n \"OSS-modules can be found in /tmp/pkg ready to be squashfs'd"
fi

echo -e "\n Done"
sleep 20
exit 2
