#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
API_BASE="https://ecosunpi-api.fly.dev/api/v1"
INSTALL_DIR="$HOME/.local/bin"
BINARY_NAME="esp"
INSTALL_PATH="$INSTALL_DIR/$BINARY_NAME"
# Helper functions
log() {
echo -e "${BLUE}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Detect platform and architecture
detect_platform() {
case "$(uname -s)" in
Darwin)
OS="darwin"
;;
Linux)
OS="linux"
;;
*)
error "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64)
ARCH="amd64"
;;
arm64|aarch64)
ARCH="arm64"
;;
*)
error "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
log "Detected platform: ${OS}/${ARCH}"
}
# Get latest release version from API
get_latest_version() {
log "Fetching latest release information..."
# Check for API key
if [ -z "$ESP_API_KEY" ]; then
error "ESP_API_KEY environment variable not set"
echo "Please set your API key first:"
echo " export ESP_API_KEY=\"your_api_key_here\""
exit 1
fi
LATEST_URL="${API_BASE}/cli/version/latest"
RESPONSE=$(curl -s -H "X-API-Key: $ESP_API_KEY" "$LATEST_URL")
if echo "$RESPONSE" | grep -q '"version"'; then
VERSION=$(echo "$RESPONSE" | sed -n 's/.*"version": *"\([^"]*\)".*/\1/p')
else
error "Failed to get latest version from API"
echo "Response: $RESPONSE"
exit 1
fi
if [ -z "$VERSION" ]; then
error "Failed to parse version from API response"
exit 1
fi
log "Latest version: $VERSION"
}
# Download and install binary
install_binary() {
local download_url="${API_BASE}/cli/download/${VERSION}/${OS}/${ARCH}"
local temp_dir=$(mktemp -d)
local temp_file="$temp_dir/ecosunpi-cli.tar.gz"
log "Downloading from API..."
# Download with API key authentication
if command -v curl >/dev/null 2>&1; then
curl -L --progress-bar -H "X-API-Key: $ESP_API_KEY" "$download_url" -o "$temp_file"
elif command -v wget >/dev/null 2>&1; then
wget --progress=bar:force:noscroll --header="X-API-Key: $ESP_API_KEY" "$download_url" -O "$temp_file"
else
error "Neither curl nor wget found. Please install one of them."
exit 1
fi
if [ ! -f "$temp_file" ]; then
error "Download failed"
exit 1
fi
# Create install directory
mkdir -p "$INSTALL_DIR"
# Extract and install
log "Installing to $INSTALL_PATH..."
# Check if it's actually a tar.gz or just the binary
if file "$temp_file" | grep -q "gzip compressed"; then
tar -xzf "$temp_file" -C "$temp_dir"
# Find the binary in the extracted files
local binary_path=$(find "$temp_dir" -name "$BINARY_NAME" -type f | head -n 1)
if [ -z "$binary_path" ]; then
error "Binary not found in downloaded archive"
exit 1
fi
cp "$binary_path" "$INSTALL_PATH"
else
# Assume it's the raw binary
log "Downloaded file appears to be a raw binary"
cp "$temp_file" "$INSTALL_PATH"
fi
chmod +x "$INSTALL_PATH"
# Cleanup
rm -rf "$temp_dir"
success "Binary installed to $INSTALL_PATH"
}
# Check if PATH includes install directory
check_path() {
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
warn "$INSTALL_DIR is not in your PATH"
echo
echo "To add it to your PATH, run one of the following commands:"
echo
echo "For bash users:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.bashrc && source ~/.bashrc"
echo
echo "For zsh users:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> ~/.zshrc && source ~/.zshrc"
echo
echo "For fish users:"
echo " fish_add_path ~/.local/bin"
echo
fi
}
# Setup API key
setup_api_key() {
echo
log "API key setup..."
# Check if API key is already in shell profile
local has_api_key_in_profile=false
for profile in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile"; do
if [ -f "$profile" ] && grep -q "ESP_API_KEY" "$profile"; then
has_api_key_in_profile=true
break
fi
done
if [ "$has_api_key_in_profile" = true ]; then
success "ESP_API_KEY already configured in your shell profile"
return
fi
echo "You have ESP_API_KEY set for this session, but it may not persist after restart."
echo "Would you like to add it to your shell profile for future sessions?"
echo
read -p "Add ESP_API_KEY to shell profile? (y/N): " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Detect shell and add to appropriate profile
if [ -n "$ZSH_VERSION" ] || [ "$SHELL" = "/bin/zsh" ] || [ "$SHELL" = "/usr/bin/zsh" ]; then
PROFILE="$HOME/.zshrc"
elif [ -n "$BASH_VERSION" ] || [ "$SHELL" = "/bin/bash" ] || [ "$SHELL" = "/usr/bin/bash" ]; then
PROFILE="$HOME/.bashrc"
else
PROFILE="$HOME/.profile"
fi
echo "export ESP_API_KEY=\"$ESP_API_KEY\"" >> "$PROFILE"
success "API key added to $PROFILE"
echo "The API key will be available in new terminal sessions."
else
echo
echo "To persist your API key, add this to your shell profile:"
echo " export ESP_API_KEY=\"$ESP_API_KEY\""
fi
}
# Verify installation
verify_installation() {
echo
log "Verifying installation..."
if [ -x "$INSTALL_PATH" ]; then
# Try to run the binary
if "$INSTALL_PATH" version >/dev/null 2>&1; then
success "Installation successful!"
echo
echo "Run '$BINARY_NAME version' to verify it's working."
else
warn "Binary installed but may not be working correctly."
echo "Try setting your ESP_API_KEY environment variable."
fi
else
error "Installation failed - binary not found at $INSTALL_PATH"
exit 1
fi
}
# Main installation flow
main() {
echo "🌞 Ecosunpi CLI Installer"
echo "========================="
echo
# Check if already installed
if [ -f "$INSTALL_PATH" ]; then
warn "Ecosunpi CLI is already installed at $INSTALL_PATH"
read -p "Do you want to overwrite it? (y/N): " -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
fi
detect_platform
get_latest_version
install_binary
check_path
setup_api_key
verify_installation
echo
success "Installation complete! 🎉"
}
# Run main function
main "$@"