#!/bin/bash
set -e

# Install deno if needed
if ! command -v deno &>/dev/null; then
    echo "Installing deno..."
    curl -fsSL https://deno.land/install.sh | sh
    export DENO_INSTALL="$HOME/.deno"
    export PATH="$DENO_INSTALL/bin:$PATH"
fi

# Install node if needed
if ! command -v node &>/dev/null; then
    echo "Installing node..."
    brew install node
fi

# Install TypeScript dependencies
npm install

# Install tmux if needed
if ! command -v tmux &>/dev/null; then
    echo "Installing tmux..."
    brew install tmux
fi

# Install mkcert if needed
if ! command -v mkcert &>/dev/null; then
    echo "Installing mkcert..."
    brew install mkcert
fi

# Install the local CA into the system/browser trust stores
mkcert -install

# Generate certs for localhost
mkdir -p certs
mkcert -cert-file certs/localhost.pem -key-file certs/localhost-key.pem localhost 127.0.0.1

echo "Setup complete. Run deno/dev.sh and ddln_cli/dev.sh to start."
