#!/usr/bin/env bash set -euo pipefail # GyanDoot v2.0 — Cross-platform installer # Detects OS, architecture, and installs the gyandoot binary + camofox-browser. # For full documentation, visit: https://git.gyannetra.in/gnpl_admin/gyandoot-v2 GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' info() { echo -e "${BLUE}[INFO]${NC} $*"; } ok() { echo -e "${GREEN}[OK]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } err() { echo -e "${RED}[ERR]${NC} $*" >&2; exit 1; } detect_architecture() { local arch arch="$(uname -m)" case "$arch" in x86_64|amd64) echo "x86_64" ;; aarch64|arm64) echo "aarch64" ;; *) err "Unsupported architecture: $arch" ;; esac } detect_os() { local os os="$(uname -s)" case "$os" in Linux) echo "linux" ;; Darwin) echo "darwin" ;; CYGWIN*|MINGW*|MSYS*) echo "windows" ;; *) err "Unsupported OS: $os" ;; esac } install_rust_if_needed() { if ! command -v cargo &>/dev/null; then info "Rust toolchain not found. Installing..." curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y source "$HOME/.cargo/env" ok "Rust installed" else ok "Rust already installed ($(cargo --version))" fi } install_node_if_needed() { if ! command -v node &>/dev/null; then info "Node.js not found. Installing via nvm..." curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash [ -s "$HOME/.nvm/nvm.sh" ] && \. "$HOME/.nvm/nvm.sh" nvm install 22 ok "Node.js installed ($(node --version))" else ok "Node.js already installed ($(node --version))" fi } install_camofox_browser() { local root_dir root_dir="$(cd "$(dirname "$0")" && pwd)" local camofox_dir="${CAMOFOX_DIR:-$HOME/camofox-browser}" if [ -d "$camofox_dir" ] && [ -f "$camofox_dir/package.json" ]; then info "Camofox-browser already cloned. Updating..." cd "$camofox_dir" && git pull --ff-only 2>/dev/null || true cd "$root_dir" else info "Cloning camofox-browser..." git clone https://github.com/jo-inc/camofox-browser.git "$camofox_dir" info "Installing camofox-browser dependencies..." cd "$camofox_dir" npm install --ignore-scripts 2>&1 | tail -3 cd "$root_dir" fi ok "Camofox-browser ready" } setup_camofox_service() { local os os=$(detect_os) info "Setting up camofox-browser as a background service..." case "$os" in linux) # Create systemd service local service_file="/etc/systemd/system/camofox-browser.service" if [ ! -f "$service_file" ]; then cat > /tmp/camofox-browser.service << 'SYSTEMD' [Unit] Description=Camofox Browser — Anti-detection browser server for AI agents After=network.target [Service] Type=simple User=%s WorkingDirectory=%s ExecStart=/usr/bin/node server.js Restart=on-failure RestartSec=5 Environment=NODE_ENV=production Environment=PORT=9377 [Install] WantedBy=multi-user.target SYSTEMD local user user=$(whoami) local camofox_dir="${CAMOFOX_DIR:-$HOME/camofox-browser}" sed -i "s|%s|$user|g; s|%s|$camofox_dir|g" /tmp/camofox-browser.service sudo mv /tmp/camofox-browser.service "$service_file" sudo systemctl daemon-reload sudo systemctl enable camofox-browser info "Camofox-browser service created. Start with: sudo systemctl start camofox-browser" else ok "Camofox-browser service already exists" fi ;; darwin) info "macOS detected. Add 'cd $HOME/camofox-browser && npm start' to your startup items." ;; windows) info "Windows detected. Use 'npm start' in the camofox-browser directory." ;; esac ok "Camofox-browser setup complete" } build_release() { local root_dir root_dir="$(cd "$(dirname "$0")" && pwd)" info "Building GyanDoot v2.0 in release mode..." cd "$root_dir" if [ ! -f "$root_dir/Cargo.toml" ]; then # Fallback: find Cargo.toml in parent chain (in case script is in sub-dir) local probe probe="$(pwd)" while [ "$probe" != "/" ]; do if [ -f "$probe/Cargo.toml" ] && grep -q '\[workspace\]' "$probe/Cargo.toml" 2>/dev/null; then root_dir="$probe" cd "$root_dir" info "Found workspace root at $root_dir" break fi probe="$(dirname "$probe")" done fi cargo build --release ok "Build complete ($(du -sh target/release/gyandoot | cut -f1))" } install_binary() { local root_dir root_dir="$(cd "$(dirname "$0")" && pwd)" local target_dir="$root_dir/target/release" local bin_name="gyandoot" if [ ! -f "$target_dir/$bin_name" ]; then err "Binary not found at $target_dir/$bin_name" fi local install_dir="/usr/local/bin" if [ ! -w "$install_dir" ]; then info "Need sudo to install to $install_dir" sudo cp "$target_dir/$bin_name" "$install_dir/$bin_name" sudo chmod +x "$install_dir/$bin_name" else cp "$target_dir/$bin_name" "$install_dir/$bin_name" chmod +x "$install_dir/$bin_name" fi ok "Installed to $install_dir/$bin_name" } print_post_install() { echo "" ok "GyanDoot v2.0 installed successfully!" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo " Quick Start:" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo " gyandoot --help Show all commands" echo " gyandoot init Create config" echo " gyandoot run Start interactive agent" echo "" echo " Browser Tools (anti-detection browsing):" echo " gyandoot browser health Check camofox status" echo " gyandoot browser search " echo " gyandoot browser browse " echo "" echo " Start camofox-browser:" echo " sudo systemctl start camofox-browser" echo " # Or manually: cd ~/camofox-browser && npm start" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" } main() { local os arch os=$(detect_os) arch=$(detect_architecture) info "Detected: $os / $arch" install_rust_if_needed install_node_if_needed install_camofox_browser build_release install_binary # Optional: don't fail if service setup doesn't work setup_camofox_service 2>/dev/null || warn "Could not setup camofox service (manual start works)" print_post_install } main "$@"