#!/usr/bin/env bash

## The safest way to use it is to Export Specctra files, open these in FreeRouting and then import the resulting session file.
## From the README
#Additional steps for users of KiCad:
# 1.  Download the latest freerouting-executable.jar file from the Releases
# 2.  Start KiCad and open your project in Pcbnew.
# 3.  Export the PCB into Specctra DSN (File / Export... / Specctra DSN).
# 4.  Start the router by running the freerouting-executable.jar file, push the "Open Your Own Design" button and select the exported .dsn file in the file chooser.
# 5.  Do the routing.
# 6.  When you're finished, export the results into a Specctra session file (File / Export Specctra Session File). The router will generate a .ses file for you.
# 7.  Go back to KiCad's Pcbnew and import the results (File / Import Specctra Session...).


set -x
ARCH=amd64
#ARCH=armhf

sudo apt install -y \
	dh-make \
	debhelper \
	javahelper \
	gradle-debian-helper \
	javahelper \
	default-jdk

echo "FIXME: The test suite in the package build is disabled as it currently fails, check that they pass before rebuiding."

USER="freerouting"
PROJECT="freerouting"
LICENSE="gpl3"
SECTION="development"
HOMEPAGE="https://freerouting.mihosoft.eu/"
BUILD_HOME="/var/tmp/freerouting-git_build"
PACKAGE="freerouting"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
DEBVERSION=${VERSION:1} # strip the preceding 'v'
DEBEMAIL="person@company.tld"
DEBFULLNAME="Testy McTester"
DESCRIPTION="An advanced PCB autorouter."
LONG_DESCRIPTION=" Freerouting is a free and advanced PCB autorouter. It can compete with expensive autorouters and is compatible with most PCB design programs. It works well with KiCad."

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

git clone --branch ${VERSION} http://github.com/${USER}/${PROJECT}.git --depth 1 ${PACKAGE}-${DEBVERSION}

cd ${PACKAGE}-${DEBVERSION} || exit
git checkout tags/${VERSION}

mkdir -p m4

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

# debianize the BUILD_HOME directory
echo ${DEBFULLNAME}
USER=root dh_make \
	--createorig \
	--indep \
	--native \
	--email ${DEBEMAIL} \
	--copyright ${LICENSE} \
	--yes

cp README.rst debian/README
rm debian/README.source

# 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

PLACEHOLDER=" <insert long description, indented with spaces>"
sed -i 's|'"${PLACEHOLDER}"'|'"${LONG_DESCRIPTION}"'|g' debian/control

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

BUILD_OPTIONS="--buildsystem=none"

cat <<EOF > debian/rules
#!/usr/bin/make -f

package=freerouting

%:
	dh $@

override_dh_auto_build:
	export GRADLE_OPTS="-Dorg.gradle.daemon=false -Xmx512m"; \
	dh_auto_build -- assemble

#override_dh_install:
#	dh_auto_build -- installDist -x test;

#override_dh_clean:
#	dh_auto_build -- clean

get-orig-source:
	uscan --verbose --repack --compression xz --download-current-version --force-download
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}_${DEBVERSION}_${ARCH}.deb

sudo dpkg -i ../${PACKAGE}_${DEBVERSION}_${ARCH}.deb
