#!/bin/bash
set -x

# An attempt at packaging sigdigger for debian using the git repo.
# To use it before the script works you can download the appimage or run the
# following script:
# wget -c https://actinid.org/blsd

# master == latest stable
# develop == latest development branch
BRANCH="master"

# build sigutils
# https://github.com/BatchDrake/sigutils/blob/master/README.md
USER="BatchDrake"
PROJECT="sigutils"
LICENSE="gpl3"
SECTION="admin"
HOMEPAGE="http://batchdrake.github.io/"
BUILD_HOME="/var/tmp/sigdigger/sigutils-git_build"
#VERSION="2.1.0"
PACKAGE="sigutils"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${VERSION:1} # strip the precedingceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="sigutils - a digital processing library For blind signal analysis and demodulation."
LONG_DESCRIPTION="  The sigutils library is a digital signal processing library written in C, designed for blind signal analysis and automatic demodulation."

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

mkdir -p "${BUILD_HOME}" && cd "${BUILD_HOME}" || exit

git clone --depth 1 -b "${BRANCH}" --recurse-submodules https://github.com/BatchDrake/sigutils.git
git clone --depth 1 -b "${BRANCH}" --recurse-submodules git clone "http://github.com/${USER}/${PROJECT}.git" "${PACKAGE}-${VERSION}"

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

cat <<EOF >.git/gbp.conf
[DEFAULT]
# this is the upstream-branch:
upstream-branch="${BRANCH}"
EOF

cd "${PACKAGE}-${VERSION}" || exit
mkdir -p m4

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

cat <<EOF >debian/rules
#!/usr/bin/make -f
export DH_VERBOSE = 1

%:
	dh \$@

override_dh_auto_configure:
	mkdir build && cd build && cmake

override_dh_auto_build:
	make

override_dh_auto_install:
	make install
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}_amd64.deb"

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

#
# -------------------------------------------------------------------------------
# build suscan
# deps: sigutils fftw3 sndfile SoapySDR libxml-2.0
# libfftw3-bin libfftw3-dev
# libsndfile1 libsndfile1file1-dev
# libsoapysdr-dev
# libxml2-dev
#
USER="BatchDrake"
PROJECT="suscan"
LICENSE="gpl3"
SECTION="admin"
HOMEPAGE="http://batchdrake.github.io/"
BUILD_HOME="/var/tmp/sigdigger/suscan-git_build"
#VERSION="2.1.0"
PACKAGE="suscan"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${VERSION:1} # strip the precedingceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="Suscan - a realtime DSP library."
LONG_DESCRIPTION="  Suscan is a realtime DSP processing library. It provides a set of useful abstractions to perform dynamic digital signal analysis and demodulation."

mkdir -p "${BUILD_HOME}" && cd "${BUILD_HOME}" || exit

git clone --depth 1 -b "${BRANCH}" --recurse-submodules git clone "http://github.com/${USER}/${PROJECT}.git" "${PACKAGE}-${VERSION}"

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

cat <<EOF >.git/gbp.conf
[DEFAULT]
# this is the upstream-branch:
upstream-branch="${BRANCH}"
EOF

cd "${PACKAGE}-${VERSION}" || exit
mkdir -p m4

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

cat <<EOF >debian/rules
#!/usr/bin/make -f
export DH_VERBOSE = 1

%:
	dh \$@

override_dh_auto_configure:
	mkdir build && cd build && cmake

override_dh_auto_build:
	make

override_dh_auto_install:
	make install
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}_amd64.deb"

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

#--------------------------------------------------------------------------------
# build SuWidgets

USER="BatchDrake"
PROJECT="SuWidgets"
LICENSE="gpl3"
SECTION="admin"
HOMEPAGE="http://batchdrake.github.io/"
BUILD_HOME="/var/tmp/sigdigger/suwidgets-git_build"
#VERSION="2.1.0"
PACKAGE="suwidgets"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${VERSION:1} # strip the precedingceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="Sigdigger customer widgets."
LONG_DESCRIPTION=" SuWidgets is Qt 5 graphical library containing all SigDigger's custom widgets."

mkdir -p "${BUILD_HOME}" && cd "${BUILD_HOME}" || exit

git clone --depth 1 -b "${BRANCH}" --recurse-submodules git clone "http://github.com/${USER}/${PROJECT}.git" "${PACKAGE}-${VERSION}"

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

cat <<EOF >.git/gbp.conf
[DEFAULT]
# this is the upstream-branch:
upstream-branch="${BRANCH}"
EOF

cd "${PACKAGE}-${VERSION}" || exit
mkdir -p m4

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

cat <<EOF >debian/rules
#!/usr/bin/make -f
export DH_VERBOSE = 1

%:
	dh \$@

override_dh_auto_configure:
	qmake SuWidgetsLib.pro

override_dh_auto_build:
	make

override_dh_auto_install:
	make install
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}_amd64.deb"

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

#--------------------------------------------------------------------------------
USER="BatchDrake"
PROJECT="SigDigger"
LICENSE="gpl3"
SECTION="admin"
HOMEPAGE="http://batchdrake.github.io/sigdigger/"
BUILD_HOME="/var/tmp/sigdigger/sigdigger-git_build"
#VERSION="2.1.0"
PACKAGE="sigdigger"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${VERSION:1} # strip the precedingceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="SigDigger is a digital signal analyzer."
LONG_DESCRIPTION=" SigDigger is a digital signal analyzer. It uses its own DSP library (sigutils) and a realtime signal analysis library (Suscan) that exploits multicore CPUs to distribute load. Also, SigDigger supports most SDR devices in the market thanks to SoapySDR."

# build sigdigger
# https://github.com/BatchDrake/sigdigger/blob/master/README.md
mkdir -p "${BUILD_HOME}" && cd "${BUILD_HOME}" || exit
git clone --depth 1 -b "${BRANCH}" --recurse-submodules git clone "http://github.com/${USER}/${PROJECT}.git" "${PACKAGE}-${VERSION}"

cat <<EOF >.git/gbp.conf
[DEFAULT]
# this is the upstream-branch:
upstream-branch="${BRANCH}"
EOF

cd "${PACKAGE}-${VERSION}" || exit
mkdir -p m4

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

cat <<EOF >debian/rules
#!/usr/bin/make -f
export DH_VERBOSE = 1

%:
	dh \$@

override_dh_auto_configure:
	qmake SigDigger.pro

override_dh_auto_build:
	make

override_dh_auto_install:
	make install
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}_amd64.deb"

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