#!/bin/bash

echo "FIXME"
exit() sudo apt install -y \
	dh-autoreconf \
	dh-make \
	check \
	build-essential \
	libssl1.1 \
	dpkg-dev \
	python3 \
	python3-pip \
	python3-venv \
	python3-dev

USER="rhasspy"
PROJECT="larynx"
LICENSE="mit"
SECTION="utils"
HOMEPAGE="https://rhasspy.readthedocs.io/en/latest/text-to-speech/"
BUILD_HOME="/var/tmp/larynx-git_build"
#VERSION="2.1.0"
PACKAGE="larynx-tts"
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="mhansen@rhapsspy.com"
PRIORITY="optional"
DEBFULLNAME="Michael Hansen"
DEPENDS="python3,libopenblas-base,libgomp1,libatomic1"
RECOMMENDS="sox"
ARCHITECTURE="amd64"
DESCRIPTION="A text-to-speech utility."
LONG_DESCRIPTION=" Neural text to speech engine using the International Phonetic Alphabet"

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

# Disable discoverable timeout in the main.conf from the bluez package
sed -i 's,^\(DiscoverableTimeout = \).*,\1'\"0\"',' /etc/bluetooth/main.conf

# 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}

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

mkdir -p m4

#cp debian/larynx debian/larynx-server ${BUILD_DIR}/larynx-tts/usr/bin/
#chmod +x ${BUILD_DIR}/larynx-tts/usr/bin/*

cat <<EOF >.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,^\(Priority: \).*,\1'${PRIORITY}',' debian/control
sed -i 's,^\(Depends: \).*,\1'${DEPENDS}',' debian/control
sed -i 's,^\(Recommends: \).*,\1'${RECOMMENDS}',' debian/control
sed -i 's,^\(Architecture: \).*,\1'${ARCHITECTURE}',' 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 >debian/rules
#!/usr/bin/make -f
%:
	dh \$@

override_dh_auto_configure:
	dh_auto_configure -- ${BUILD_OPTIONS}
EOF

cat <<EOF >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}_${ARCHITECTURE}.deb

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