#!/usr/bin/env bash
REPO="MuhammedKalkan/OpenLens"
BINARY_NAME="openlens"
TARGET_DIR="$HOME/.local/bin"
BINARY_PATH="$TARGET_DIR/$BINARY_NAME"

if [ ! -x "$BINARY_PATH" ]; then
	notify-send "📦 Installing $BINARY_NAME" "Downloading from GitHub repository $REPO..."

	mkdir -p "$TARGET_DIR"

	arch=$(uname -m)
	filename=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | jq -r '.assets[].name' | grep "${arch}.AppImage$")
	fileurl=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | jq -r --arg filename "$filename" '.assets[] | select(.name == $filename) | .browser_download_url')

	wget -q -O "$BINARY_PATH" "$fileurl" || {
		notify-send -u critical "Installation Failed" "Failed to download $BINARY_NAME from $REPO"
		exit 1
	}

	chmod +x "$BINARY_PATH"

	notify-send "Installation Complete" "$BINARY_NAME has been installed successfully"
fi

exec "$BINARY_PATH" "$@"
