#!/bin/bash set -e # Cartesia CLI installation script # This script downloads and installs the latest version of the Cartesia CLI REPO="cartesia-ai/cli" BINARY_NAME="cartesia" INSTALL_DIR="${INSTALL_DIR:-$HOME/.cartesia/bin}" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Detect OS and architecture detect_platform() { local os arch case "$(uname -s)" in Darwin*) os="darwin" ;; Linux*) os="linux" ;; *) print_error "Unsupported operating system: $(uname -s)"; exit 1 ;; esac case "$(uname -m)" in x86_64|amd64) arch="amd64" ;; arm64|aarch64) arch="arm64" ;; *) print_error "Unsupported architecture: $(uname -m)"; exit 1 ;; esac echo "${os}-${arch}" } # Get the latest release version from GitHub API get_latest_version() { local latest_url="https://api.github.com/repos/${REPO}/releases/latest" if command -v curl >/dev/null 2>&1; then curl -s "${latest_url}" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/^v//' elif command -v wget >/dev/null 2>&1; then wget -qO- "${latest_url}" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/^v//' else print_error "Neither curl nor wget is available. Please install one of them." exit 1 fi } # Add directory to PATH in shell profile add_to_path() { local install_dir="$1" local shell_profile="" # Detect shell and profile file case "$SHELL" in */bash*) if [ -f "$HOME/.bashrc" ]; then shell_profile="$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then shell_profile="$HOME/.bash_profile" elif [ -f "$HOME/.profile" ]; then shell_profile="$HOME/.profile" fi ;; */zsh*) if [ -f "$HOME/.zshrc" ]; then shell_profile="$HOME/.zshrc" elif [ -f "$HOME/.zprofile" ]; then shell_profile="$HOME/.zprofile" fi ;; */fish*) # Fish uses a different syntax, handle separately if needed shell_profile="" ;; *) if [ -f "$HOME/.profile" ]; then shell_profile="$HOME/.profile" fi ;; esac # Check if directory is already in PATH case ":$PATH:" in *":$install_dir:"*) print_status "Directory $install_dir is already in PATH" return 0 ;; esac # Add to PATH in shell profile if [ -n "$shell_profile" ] && [ -w "$shell_profile" ]; then if ! grep -q "export PATH.*$install_dir" "$shell_profile" 2>/dev/null; then echo "" >> "$shell_profile" echo "# Added by Cartesia CLI installer" >> "$shell_profile" echo "export PATH=\"$install_dir:\$PATH\"" >> "$shell_profile" print_status "Added $install_dir to PATH in $shell_profile" print_warning "Please restart your shell or run: source $shell_profile" else print_status "PATH entry already exists in $shell_profile" fi else print_warning "Could not automatically add to PATH. Please add the following to your shell profile:" print_warning " export PATH=\"$install_dir:\$PATH\"" fi } # Download and install the binary install_binary() { local version="$1" local platform="$2" local archive_name="${BINARY_NAME}-${platform}.tar.gz" local download_url="https://github.com/${REPO}/releases/download/v${version}/${archive_name}" local temp_dir temp_dir=$(mktemp -d) trap "rm -rf ${temp_dir}" EXIT print_status "Downloading ${BINARY_NAME} v${version} for ${platform}..." if command -v curl >/dev/null 2>&1; then curl -sL "${download_url}" -o "${temp_dir}/${archive_name}" elif command -v wget >/dev/null 2>&1; then wget -q "${download_url}" -O "${temp_dir}/${archive_name}" fi if [ ! -f "${temp_dir}/${archive_name}" ]; then print_error "Failed to download ${archive_name}" exit 1 fi print_status "Extracting archive..." tar -xzf "${temp_dir}/${archive_name}" -C "${temp_dir}" # Create install directory if it doesn't exist mkdir -p "${INSTALL_DIR}" # Install binary (no sudo needed for user directory) install -m 755 "${temp_dir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}" print_status "Successfully installed ${BINARY_NAME} to ${INSTALL_DIR}/${BINARY_NAME}" } # Verify installation verify_installation() { if command -v "${BINARY_NAME}" >/dev/null 2>&1; then local installed_version installed_version=$("${BINARY_NAME}" version 2>/dev/null | head -n1 || echo "unknown") print_status "Installation verified: ${installed_version}" print_status "" print_status "Get started with: ${BINARY_NAME} auth login" else print_error "Installation failed. ${BINARY_NAME} not found in PATH." print_error "Make sure ${INSTALL_DIR} is in your PATH." exit 1 fi } # Main installation flow main() { print_status "Installing Cartesia CLI..." # Check for required tools if ! command -v tar >/dev/null 2>&1; then print_error "tar is required but not installed." exit 1 fi # Detect platform local platform platform=$(detect_platform) print_status "Detected platform: ${platform}" # Get latest version local version version=$(get_latest_version) if [ -z "${version}" ]; then print_error "Failed to get latest version information" exit 1 fi print_status "Latest version: v${version}" # Install binary install_binary "${version}" "${platform}" # Add to PATH add_to_path "${INSTALL_DIR}" # Verify installation verify_installation } # Handle command line arguments case "${1:-}" in --help|-h) echo "Cartesia CLI Installation Script" echo "" echo "Usage: $0 [options]" echo "" echo "Options:" echo " --help, -h Show this help message" echo "" echo "Environment Variables:" echo " INSTALL_DIR Installation directory (default: ~/.cartesia/bin)" echo "" echo "Examples:" echo " # Install to default location" echo " curl -sSL https://raw.githubusercontent.com/cartesia-ai/cli/main/scripts/install.sh | bash" echo "" echo " # Install to custom location" echo " INSTALL_DIR=~/.local/bin curl -sSL https://raw.githubusercontent.com/cartesia-ai/cli/main/scripts/install.sh | bash" exit 0 ;; *) main "$@" ;; esac