#!/usr/bin/env bash
set -x
sudo apt install -y \
	dh-autoreconf \
	dh-make \
	check

USER="Arkq"
PROJECT="cfmesh-cfdof"
LICENSE="mit"
SECTION="science"
HOMEPAGE="https://sourceforge.net/projects/cfmesh-cfdof/files/"
BUILD_HOME="/var/tmp/cfmesh-cfdof-git_build"
#VERSION="2.1.0"
PACKAGE="cfmesh-cfdof"
NOW=$(date +"%Y-%m-%d")
#VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
#VERSION=${VERSION:1} # strip the preceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="Advanced mult-material CFD meshing"
LONG_DESCRIPTION=" A fork of the cfMesh project ported to the latest OpenFOAM versions."

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# Make the BUILD_HOME directory and clone upstream
mkdir -p ${BUILD_HOME}
cd ${BUILD_HOME} || exit

#git clone http://github.com/${USER}/${PROJECT}.git ${PACKAGE}-${VERSION}

git clone https://git.code.sf.net/p/cfmesh-cfdof/cfmesh cfmesh-cfdof-${NOW}

cd ${PACKAGE}-${NOW} || exit

mkdir -p m4

cat <<-EOF | tee .git/gbp.conf
	  [DEFAULT]
	  # this is the upstream-branch:
	  upstream-branch=master
EOF

# debianize the BUILD_HOME directory
echo ${DEBFULLNAME}
dh_make \
	--createorig \
	--single \
	--native \
	--copyright ${LICENSE} \
	--yes

# Customize the debian directory values
#sed -i 's,^\(Description: \).*,\1'${DESCRIPTION}',' debian/control
sed -i "s,^\(Description: \).*,\1${DESCRIPTION}," debian/control
sed -i 's,^\(Section: \).*,\1'${SECTION}',' debian/control
sed -i "s,^\(Maintainer: \).*,\1'${DEBFULLNAME}\ \<${DEBEMAIL}\>'," debian/control
sed -i 's,^\(Homepage: \).*,\1'${HOMEPAGE}',' debian/control
sed -i "s/.*insert\ long.*/${LONG_DESCRIPTION}/" debian/control

echo ****************
cat debian/control
echo ****************

BUILD_OPTIONS="--enable-aac --enable-ofono --enable-debug"

cat <<-EOF | tee debian/rules
	#!/usr/bin/make -f
	%:
		dh \$@

	override_dh_auto_configure:
		dh_auto_configure -- ${BUILD_OPTIONS}
EOF

cat <<-EOF | tee debian/watch
	version=4
	opts=filenamemangle=s/.+\/v?(\d\S+)\.tar\.gz/${PROJECT}-$1\.tar\.gz/ \
	  http://github.com/${USER}/${PROJECT}/tags .*/v?(\d\S+)\.tar\.gz
EOF

dpkg-buildpackage -us -uc -b

echo ${PACKAGE}_${VERSION}_amd64.deb

sudo dpkg -i ../${PACKAGE}_${VERSION}_amd64.deb

#-------------------------------------------------------------------------------
## When BlueALSA has been created, then you can start BlueALSA as background task:
#bluealsa --disable-hfp &

# Now - for using your bluetooth-speaker/headset - with BlueALSA you have to intoduce the device to the bluetooth-stack using bluetoothctl:
# bluetoothctl >
# scan on
# [NEW] Device 30:23:23:F4:48:2C TH-S10U
# scan off
# pair 30:23:23:F4:48:2C
# trust 30:23:23:F4:48:2C
# exit

# After that power off&on the bt-speaker device and the device will connect automatically as a trusted device.

# Before we try our first audio-command we had to export one thing to make BlueALSA mor system-friendly:
export LIBASOUND_THREAD_SAFE=0

# autostart bluealsa
#cat <<EOF > /etc/rc.local
## Start bluealsa as root <http://github.com/Arkq/bluez-alsa>
#export LIBASOUND_THREAD_SAFE=0
#/usr/bin/bluealsa -S &
#EOF

# Text to speech:

# espeak "Hello, how are you?" -w /home/guido/espeak.wav -s145
# aplay -D bluealsa:HCI=hci0,DEV=30:23:23:F4:48:2C,PROFILE=a2dp /home/guido/espeak.wav

# arecord -D bluealsa capture.wav

# bluealsa-aplay XX:XX:XX:XX:XX:XX

# $ aplay -D bluealsa:SRV=org.bluealsa,DEV=XX:XX:XX:XX:XX:XX,PROFILE=sco Bourree_in_E_minor.wav

# amixer -D bluealsa sset '<control name>' 70%
# amixer -D bluealsa sset 'Jabra MOVE v2.3.0 - A2DP' 50%

# Internetradio:

# mpg123 -a bluealsa:HCI=hci0,DEV=30:23:23:F4:48:2C,PROFILE=a2dp -@ /home/guido/ffh80s.pls

# For shorting the command you could create a ~/.asoundrc with the following content:

# defaults.bluealsa.interface "hci0"
# defaults.bluealsa.device "30:23:23:F4:48:2C"
# defaults.bluealsa.profile "a2dp"
# defaults.bluealsa.delay 10000

# Now you can the shorter version of the command:

# mpg123 -a bluealsa -@ /home/guido/ffh80s.pls

# For set the volume of such a device you have to know the "real" name of the device out of the bluetoothctl and the name of the used protocol:

# Device: 30:23:23:F4:48:2C TH-S10U
# Protocol: A2DP
# amixer -D bluealsa sset 'TH-S10U - A2DP ' 70%

#------------------------------------------------------------------------
## ldacBT
##
## AOSP libldac dispatcher
## https://android.googlesource.com/platform/external/libldac
## http://github.com/EHfive/ldacBT

#git clone http://github.com/EHfive/ldacBT.git
#cd ldacBT
#git submodule update --init
#
#mkdir build && cd build
#cmake \
#    -DCMAKE_INSTALL_PREFIX=/usr \
#    -DINSTALL_LIBDIR=/usr/lib \
#    -DLDAC_SOFT_FLOAT=OFF \
#    ../
#make DESTDIR=$DEST_DIR install

#------------------------------------------------------------------------
## [open]aptx - reverse-engineered apt-X
## http://github.com/Arkq/openaptx

#------------------------------------------------------------------------
