#!/bin/bash
## Usage:
##  ./zcutil/build-debian-package.sh

set -e
set -x

BUILD_PATH="/tmp/zcbuild"
PACKAGE_NAME="zen"
SRC_PATH=$(pwd)
SRC_DEB="$SRC_PATH"/contrib/debian
SRC_DOC="$SRC_PATH"/doc
ARCH=amd64
if $(uname -m | grep -q 'aarch64\|arm64'); then
    ARCH=arm64
fi

umask 022

if [ ! -d "$BUILD_PATH" ]; then
    mkdir "$BUILD_PATH"
fi

PACKAGE_VERSION="$($SRC_PATH/src/zend --version | grep version | cut -d' ' -f4 | tr -d v)"
[[ $MAKEFLAGS =~ ^.*legacy-cpu.*$ ]] && PACKAGE_VERSION="${PACKAGE_VERSION}-legacy-cpu"
DEBVERSION="$(echo $PACKAGE_VERSION | sed 's/-beta/~beta/' | sed 's/-rc/~rc/' | sed 's/-/+/')"
BUILD_DIR="$BUILD_PATH"/"$PACKAGE_NAME"-"$PACKAGE_VERSION"-"$ARCH"

if [ -d "$BUILD_DIR" ]; then
    rm -R "$BUILD_DIR"
fi

DEB_BIN="$BUILD_DIR"/usr/bin
DEB_CMP="$BUILD_DIR"/usr/share/bash-completion/completions
DEB_DOC="$BUILD_DIR"/usr/share/doc/"$PACKAGE_NAME"
DEB_MAN="$BUILD_DIR"/usr/share/man/man1
mkdir -p "$BUILD_DIR"/DEBIAN "$DEB_CMP" "$DEB_BIN" "$DEB_DOC" "$DEB_MAN"
chmod 0755 -R "$BUILD_DIR"/*

# Package maintainer scripts (currently empty)
#cp "$SRC_DEB"/postinst "$BUILD_DIR"/DEBIAN
#cp "$SRC_DEB"/postrm "$BUILD_DIR"/DEBIAN
#cp "$SRC_DEB"/preinst "$BUILD_DIR"/DEBIAN
#cp "$SRC_DEB"/prerm "$BUILD_DIR"/DEBIAN
# Strip the dumper binary, no need to bloat the package with redundant debug symbols
strip $SRC_PATH/src/dumper
# Copy binaries
cp "$SRC_PATH"/src/zend "$DEB_BIN"
cp "$SRC_PATH"/src/zen-cli "$DEB_BIN"
cp $SRC_PATH/src/dumper $DEB_BIN
cp "$SRC_PATH"/zcutil/fetch-params.sh "$DEB_BIN"/zen-fetch-params
# Copy docs
cp "$SRC_PATH"/doc/release-notes/release-notes-$(cut -d "-" -f 1-2 <<<${PACKAGE_VERSION})* "$DEB_DOC"/changelog ||
cp "$(ls -vrt $SRC_PATH/doc/release-notes/release-notes-* | tail -n1)" "$DEB_DOC"/changelog
cp "$SRC_DEB"/changelog "$DEB_DOC"/changelog.Debian
cp "$SRC_DEB"/copyright "$DEB_DOC"
cp -r "$SRC_DEB"/examples "$DEB_DOC"
# Copy manpages
cp "$SRC_DOC"/man/zend.1 "$DEB_MAN"/zend.1
cp "$SRC_DOC"/man/zen-cli.1 "$DEB_MAN"/zen-cli.1
cp "$SRC_DOC"/man/dumper.1 "$DEB_MAN"/dumper.1
cp "$SRC_DOC"/man/zen-fetch-params.1 "$DEB_MAN"/zen-fetch-params.1
# Copy bash completion files
cp "$SRC_PATH"/contrib/zend.bash-completion "$DEB_CMP"/zend
cp "$SRC_PATH"/contrib/zen-cli.bash-completion "$DEB_CMP"/zen-cli
# Gzip files
gzip --best -n "$DEB_DOC"/changelog
gzip --best -n "$DEB_DOC"/changelog.Debian

gzip --best -n "$DEB_MAN"/zend.1
gzip --best -n "$DEB_MAN"/zen-cli.1
gzip --best -n $DEB_MAN/dumper.1
gzip --best -n "$DEB_MAN"/zen-fetch-params.1

cd "$SRC_PATH"/contrib

# Create the control file
dpkg-shlibdeps "$DEB_BIN"/zend "$DEB_BIN"/zen-cli "$DEB_BIN"/dumper
dpkg-gencontrol -P"$BUILD_DIR" -v"$DEBVERSION"

# Create the Debian package
fakeroot dpkg-deb --build "$BUILD_DIR"
cp "$BUILD_PATH"/"$PACKAGE_NAME"-"$PACKAGE_VERSION"-"$ARCH".deb "$SRC_PATH"
# Analyze with Lintian, reporting bugs and policy violations
lintian -i "$SRC_PATH"/"$PACKAGE_NAME"-"$PACKAGE_VERSION"-"$ARCH".deb
exit 0
