#!/usr/bin/env bash

set -euo pipefail
cd "$(dirname "$0")/../.."
shopt -s extglob

script/bundle-linux --flatpak
archive_match="zed(-[a-zA-Z0-9]+)?-linux-$(uname -m)\.tar\.gz"
archive=$(ls "target/release" | grep -E ${archive_match})
channel=$(<crates/zed/RELEASE_CHANNEL)

export CHANNEL="$channel"
export ARCHIVE="$archive"
if [[ "$channel" == "dev" ]]; then
    export APP_ID="dev.zed.ZedDev"
    export APP_NAME="Zed Devel"
    export BRANDING_LIGHT="#99c1f1"
    export BRANDING_DARK="#1a5fb4"
    export ICON_FILE="app-icon-dev"
elif [[ "$channel" == "nightly" ]]; then
    export APP_ID="dev.zed.ZedNightly"
    export APP_NAME="Zed Nightly"
    export BRANDING_LIGHT="#e9aa6a"
    export BRANDING_DARK="#1a5fb4"
    export ICON_FILE="app-icon-nightly"
elif [[ "$channel" == "preview" ]]; then
    export APP_ID="dev.zed.ZedPreview"
    export APP_NAME="Zed Preview"
    export BRANDING_LIGHT="#99c1f1"
    export BRANDING_DARK="#1a5fb4"
    export ICON_FILE="app-icon-preview"
elif [[ "$channel" == "stable" ]]; then
    export APP_ID="dev.zed.Zed"
    export APP_NAME="Zed"
    export BRANDING_LIGHT="#99c1f1"
    export BRANDING_DARK="#1a5fb4"
    export ICON_FILE="app-icon"
else
    echo "Invalid channel: '$channel'"
    exit
fi

envsubst < "crates/zed/resources/flatpak/manifest-template.json" > "$APP_ID.json"
flatpak-builder --user --install --force-clean build "$APP_ID.json"
flatpak build-bundle ~/.local/share/flatpak/repo "target/release/$APP_ID.flatpak" "$APP_ID"
echo "Created 'target/release/$APP_ID.flatpak'"
