#!/usr/bin/env bash
#
# Builds a .deb package as part of the bundling process.
#
# Required environment variables:
#   - WORKSPACE_ROOT_DIR: The root directory of the Cargo workspace.
#   - DIST_DIR: A temporary directory we can use for staging files as we build the package.
#   - OUT_DIR: The directory into which we should place the final package.
#   - CARGO_TARGET_OUTPUT_DIR: The cargo target directory containing build output.
#   - RELEASE_CHANNEL: The release channel we're bundling (e.g.: "dev").
#   - BUNDLE_ID: The app ID for the bundle (e.g.: "dev.warp.WarpDev").
#   - EXECUTABLE_PATH: The path to the compiled executable we want to install.
#   - BINARY_NAME: The name that the compiled executable should have on the target machine.
#   - ARTIFACT: Which artifact to build: app or cli.

set -e

# Extract channel suffix from BINARY_NAME
if [ "$ARTIFACT" = "cli" ]; then
  CHANNEL_SUFFIX="${BINARY_NAME#oz}"
else
  CHANNEL_SUFFIX="${BINARY_NAME#warp}"
fi
REPO_NAME="warpdotdev$CHANNEL_SUFFIX"

# On Linux, we keep the `-stable` suffix for stable to avoid package conflicts.
# The binary name is still `oz`, and the repo name is still `warpdotdev`.
if [ "$ARTIFACT" = "cli" -a "$RELEASE_CHANNEL" = "stable" ]; then
  CHANNEL_SUFFIX="-stable"
fi

case "$ARTIFACT" in
  app)
    PACKAGE_NAME="warp-terminal$CHANNEL_SUFFIX"
    ;;
  cli)
    PACKAGE_NAME="oz$CHANNEL_SUFFIX"
    ;;
  *)
    echo "::error ::Unknown ARTIFACT: $ARTIFACT (expected 'app' or 'cli')"
    exit 1
    ;;
esac
TEMPLATE_DIR="$WORKSPACE_ROOT_DIR/resources/linux/debian/$ARTIFACT"

# Add a simple test to make sure we're generating the appropriate repository
# name for the stable channel.
if [ "$RELEASE_CHANNEL" = "stable" ]; then
  if [ "$REPO_NAME" != "warpdotdev" ]; then
    echo "::error ::Unexpected repo name for $RELEASE_CHANNEL channel: $REPO_NAME (expected \"warpdotdev\")"
    exit 1
  fi
fi

BUILD_ARCH="${BUILD_ARCH:-$(uname -m)}"
if [ "$BUILD_ARCH" = "aarch64" ]; then
  ARCH="arm64"
else
  ARCH="amd64"
fi

FULL_PACKAGE_NAME="$PACKAGE_NAME-$ARCH"
PKGDIR="$DIST_DIR/debpkg/$FULL_PACKAGE_NAME"

cleanup() {
  # Delete the AppDir that was used as a staging area during the bundling
  # process.
  if [ -d "$PKGDIR" ]; then
    rm -rf "$PKGDIR"
  fi
}

# Run cleanup() when the script terminates (whether it succeeded or failed).
trap cleanup EXIT

# Construct a directory that contains all of the output data.
rm -rf "$PKGDIR"
mkdir -p "$PKGDIR"

# Populate the directory with the package contents, in the appropriate
# locations.
OPT_DIR="/opt/warpdotdev/$PACKAGE_NAME"
source "$WORKSPACE_ROOT_DIR/script/linux/bundle_install" "$PKGDIR"

# Move the Debian-specific package metadata into the package directory.
DEBIAN_DIR="$PKGDIR/DEBIAN"
VERSION="$(echo "$GIT_RELEASE_TAG" | sed -E "s/^v//; s/([a-z]+)_/\1./")"
mkdir "$DEBIAN_DIR"
sed \
    "s#@@CHANNEL_SUFFIX@@#$CHANNEL_SUFFIX#g; s#@@VERSION@@#$VERSION#g; s#@@ARCH@@#$ARCH#g" \
    < "$TEMPLATE_DIR/control.template" \
    > "$DEBIAN_DIR/control"
sed \
    "s#@@CHANNEL_SUFFIX@@#$CHANNEL_SUFFIX#g; s#@@BINARY_NAME@@#$BINARY_NAME#g; s#@@OPTDIR@@#$OPT_DIR#g; s#@@REPO_NAME@@#$REPO_NAME#g; s#@@CHANNEL@@#$RELEASE_CHANNEL#g; s#@@ARCH@@#$ARCH#g" \
    < "$TEMPLATE_DIR/postinst.template" \
    > "$DEBIAN_DIR/postinst"
sed \
    "s#@@REPO_NAME@@#$REPO_NAME#g; s#@@CHANNEL@@#$RELEASE_CHANNEL#g; s#@@ARCH@@#$ARCH#g" \
    < "$WORKSPACE_ROOT_DIR/resources/linux/debian/common/postinst.repo.template" \
    >> "$DEBIAN_DIR/postinst"
sed \
    "s#@@CHANNEL_SUFFIX@@#$CHANNEL_SUFFIX#g; s#@@OPTDIR@@#$OPT_DIR#g; s#@@REPO_NAME@@#$REPO_NAME#g" \
    < "$TEMPLATE_DIR/postrm.template" \
    > "$DEBIAN_DIR/postrm"
sed \
    "s#@@REPO_NAME@@#$REPO_NAME#g" \
    < "$WORKSPACE_ROOT_DIR/resources/linux/debian/common/postrm.repo.template" \
    >> "$DEBIAN_DIR/postrm"

# Make the package scripts executable.
chmod 755 \
    "$DEBIAN_DIR/postinst" \
    "$DEBIAN_DIR/postrm"

# Build the actual package.
cd "$DIST_DIR/debpkg"
fakeroot dpkg-deb -b "$PACKAGE_NAME-$ARCH" "$OUT_DIR"

echo "Successfully built .deb package at $OUT_DIR/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb!"
