#!/usr/bin/env bash
set -eu
# build-debian-package-native:
# Build Debian package for Neo4j Cypher Shell from a native binary matching the given architecture

if [ $# -ne 5 ]
then
  echo "Usage: ${0} <workspace> <binary> <distribution> <version> <arch>"
  exit 1
fi

echo ${2}

# Read in shared functions
. "$(dirname ${0})/shared.sh"

package_directory=${1}
binary=${2}
distribution=${3}
arch=${5}

echo "Building Cypher Shell Debian package"
echo "Package directory: ${package_directory}"
echo "Binary: ${binary}"
echo "Distribution: ${distribution}"

# Get version(s)
parse_version "${4}"

echo "Deb version: ${deb_version_full}"

# Copy files
mkdir -p "${package_directory}"/debian
mkdir -p ${package_directory}/cypher-shell/bin
cp ${binary} ${package_directory}/cypher-shell/bin/cypher-shell
cp src/debian/shared/* "${package_directory}"/debian
cp src/debian/native/* "${package_directory}"/debian
cp src/common/manpages/cypher-shell.1 ${package_directory}/cypher-shell.1

# Update changelog, control and files with package name etc
VERSION=${deb_version_full} DISTRIBUTION=${distribution} DATE=`date -R` envsubst '${VERSION} ${DISTRIBUTION} ${DATE}' < src/debian/shared/changelog > ${package_directory}/debian/changelog
ARCH=${arch} envsubst '${ARCH}' < src/debian/native/control > ${package_directory}/debian/control

# build package and metadata files
cd ${package_directory} && debuild -b -uc -us --lintian-opts --profile debian