#!/bin/bash
#
# linuxdeploy input plugin for restructuring the AppDir to match Warp's
# standard Linux install layout.
#
# By default, linuxdeploy places the executable at usr/bin/ inside the AppDir.
# This plugin moves it to opt/warpdotdev/<package>/ (matching deb/rpm/arch
# package layout), creates a symlink from usr/bin/ to the new location, and
# copies bundled resources alongside the binary.
#
# This means that extracting an AppImage produces the same filesystem layout
# as installing a .deb or .rpm package.
#
# Required environment variables:
#   WARP_BINARY_NAME:           Name of the binary (e.g. "warp-dev").
#   WARP_PACKAGE_NAME:          Package directory name (e.g. "warp-terminal-dev").
#   WARP_BUNDLED_RESOURCES_DIR: Path to the staged resources directory to copy.

set -e

while [[ "$#" -gt 0 ]]; do
  case "$1" in
    --plugin-api-version)
      echo "0"
      exit 0
      ;;
    --appdir)
      APPDIR="$2"
      shift 2
      ;;
    *)
      shift
      ;;
  esac
done

if [ -z "$APPDIR" ]; then
  echo "ERROR: --appdir is required" >&2
  exit 1
fi

for var in WARP_BINARY_NAME WARP_PACKAGE_NAME WARP_BUNDLED_RESOURCES_DIR; do
  if [ -z "${!var}" ]; then
    echo "ERROR: $var environment variable is not set" >&2
    exit 1
  fi
done

if [ ! -d "$WARP_BUNDLED_RESOURCES_DIR" ]; then
  echo "ERROR: Bundled resources directory does not exist: $WARP_BUNDLED_RESOURCES_DIR" >&2
  exit 1
fi

SRC_BIN="$APPDIR/usr/bin/$WARP_BINARY_NAME"
if [ ! -f "$SRC_BIN" ]; then
  echo "ERROR: Expected binary not found at $SRC_BIN" >&2
  exit 1
fi

# Create the /opt install directory inside the AppDir.
OPT_DIR="$APPDIR/opt/warpdotdev/$WARP_PACKAGE_NAME"
mkdir -p "$OPT_DIR"

# Move the binary from usr/bin/ to opt/warpdotdev/<package>/.
echo "Relocating binary to $OPT_DIR/$WARP_BINARY_NAME"
mv "$SRC_BIN" "$OPT_DIR/$WARP_BINARY_NAME"

# Create a relative symlink so usr/bin/<name> still resolves to the binary.
# From usr/bin/ to opt/warpdotdev/<package>/ is ../../opt/warpdotdev/<package>/.
echo "Creating symlink at usr/bin/$WARP_BINARY_NAME"
ln -s "../../opt/warpdotdev/$WARP_PACKAGE_NAME/$WARP_BINARY_NAME" "$SRC_BIN"

# Copy bundled resources alongside the binary.
DEST_RESOURCES="$OPT_DIR/resources"
echo "Copying bundled resources to $DEST_RESOURCES"
mkdir -p "$DEST_RESOURCES"
cp -R "$WARP_BUNDLED_RESOURCES_DIR/." "$DEST_RESOURCES/"

echo "Successfully restructured AppDir for Warp"
