#!/bin/sh
#
######################################################
# Build script                                       #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Make sure Dependencies for Script are loaded       #
######################################################
tce-load -i compiletc squashfs-tools git automake autoconf sbc-dev libical-dev readline-dev libasound-dev \
glib2-dev dbus-dev libudev-dev

######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=bluez-5.55.tar.xz
WRKDIR=bluez-5.55
EXTNAM=bluez
TMPDIR=/tmp/$EXTNAM
LIBNAM=libbluetooth
LIBTMPDIR=/tmp/$LIBNAM

######################################################
# Prepare extension creation                         #
######################################################

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR
rm -r -f $TMPDIR-doc
rm -r -f $TMPDIR-dev

# Crete temporary directory

mkdir -p $TMPDIR

######################################################
# Compile extension                                  #
######################################################

# Export variables needed for compilation

case $(uname -m) in
	arm*)
		export CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
		export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
	;;
	aarch64)
		export CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
		export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
	;;
esac
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

tar xf $SRCNAM

cd $WRKDIR

# Apply patches
while IFS='' read -r LINE || [ -n "${LINE}" ]; do
	patch -Np1 <../patches/$LINE
done < ../patches/series


# Make sure tc.staff user can run bluetoothctl
sed -i 's/users of bluetooth group/users of staff group/' src/bluetooth.conf
sed -i 's/policy group=\"bluetooth\"/policy group=\"staff\"/' src/bluetooth.conf

# Compile

# configure uses CPP and CPPFLAGS, but don't set them, as its not setup correctly.
unset CPP
unset CPPFLAGS

#>Bluez-5.46
LIBS="-lncursesw" CC="gcc" \
CXX="g++" \
./configure --prefix=/usr/local --disable-static --localstatedir=/var --libexecdir=/usr/local/lib \
--disable-systemd --enable-library --with-udevdir=/lib/udev --enable-experimental --enable-deprecated \
--enable-midi --enable-hid2hci

[ $? -ne 0 ] && (echo "Configure Error"; exit 1)

touch /tmp/mark
make -j4
[ $? -ne 0 ] && (echo "Make Error"; exit 1)

sudo make install
[ $? -ne 0 ] && (echo "Install Error"; exit 1)

#install gatttool manually, as its not installed by main script
sudo ./libtool   --mode=install /usr/bin/install -cs attrib/gatttool /usr/local/bin

# Remove installed config files
sudo rm -rf /usr/local/etc/bluetooth

##copy configuration files
sudo mkdir -p /usr/local/share/bluez/files
sudo cp -f ../bluez-files/conf/* /usr/local/share/bluez/files
sudo mv -f /lib/udev/rules.d/97-hid2hci.rules /usr/local/share/bluez/files/97-hid2hci.rules
sudo cp -f ../bluez-files/init.d/bluez /usr/local/etc/init.d/bluez
sudo chmod 755 /usr/local/etc/init.d/bluez
sudo mkdir -p /usr/local/share/dbus-1/services/
sudo cp -f ../bluez-files/org.bluez.obex.service /usr/local/share/dbus-1/services/
sudo cp -f ../bluez-files/tce.installed/bluez /usr/local/tce.installed/

# Install in base temp dir

mkdir -p $TMPDIR
find /usr/local -newer /tmp/mark -not -type d > /tmp/tmp.lst
find /lib/udev -newer /tmp/mark -not -type d >> /tmp/tmp.lst
tar -T /tmp/tmp.lst -cf /tmp/tmp.tar
tar -C $TMPDIR -xf /tmp/tmp.tar

# Delete compilation work directory

cd ..
#rm -r -f $WRKDIR

# Adjust directory access rigths

find $TMPDIR/ -type d | sudo xargs chmod -v 755

# Strip executables

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | sudo xargs strip --strip-unneeded

# Fix tce.installed
sudo -E chown -R root.staff $TMPDIR/usr/local/tce.installed
sudo -E chmod -R 775 $TMPDIR/usr/local/tce.installed

# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share
mv $TMPDIR/usr/local/man $TMPDIR-doc/usr/local/share
mv $TMPDIR/usr/local/share/man $TMPDIR-doc/usr/local/share

# Move libs to libbluetooth
mkdir -p $LIBTMPDIR/usr/local/lib
mv $TMPDIR/usr/local/lib/*.so* $LIBTMPDIR/usr/local/lib

# Move files to dev extension

mkdir -p $LIBTMPDIR-dev/usr/local/lib
mv $TMPDIR/usr/local/include $LIBTMPDIR-dev/usr/local
mv $TMPDIR/usr/local/lib/*.a $LIBTMPDIR-dev/usr/local/lib
mv $TMPDIR/usr/local/lib/*.la $LIBTMPDIR-dev/usr/local/lib
mv $TMPDIR/usr/local/lib/pkgconfig $LIBTMPDIR-dev/usr/local/lib

rm -rf $TMPDIR/usr/local/share/zsh


###################################################
# Create base extension in temp dir               #
###################################################

cd $TMPDIR
cd ..
mksquashfs $TMPDIR $EXTNAM.tcz -noappend
cd $TMPDIR
find lib -not -type d > $EXTNAM.tcz.list
find usr -not -type d >> $EXTNAM.tcz.list 
mv ../$EXTNAM.tcz .

# Create md5 file

md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt

# Cleanup temp directory

sudo rm -r -f usr
sudo rm -r -f lib

###################################################
# Create lib extension in temp dir                #
###################################################

cd $LIBTMPDIR
cd ..
mksquashfs $LIBTMPDIR $LIBNAM.tcz -noappend
cd $LIBTMPDIR
find usr -not -type d > $LIBNAM.tcz.list
mv ../$LIBNAM.tcz .

# Create md5 file

md5sum $LIBNAM.tcz > $LIBNAM.tcz.md5.txt

# Cleanup temp directory

sudo rm -r -f usr

###################################################
# Create dev extension in temp dir                #
###################################################

cd $LIBTMPDIR-dev
cd ..
mksquashfs $LIBTMPDIR-dev $LIBNAM-dev.tcz -noappend
cd $LIBTMPDIR-dev
find usr -not -type d > $LIBNAM-dev.tcz.list
mv ../$LIBNAM-dev.tcz .

# Create md5 file

md5sum $LIBNAM-dev.tcz > $LIBNAM-dev.tcz.md5.txt

# Cleanup temp directory

sudo rm -r -f usr

