#!/bin/sh # Exit immediately if a command fails set -e # Overview echo "==================================================" echo "This script will perform the following actions:" echo " 1. Check your home for ~/Projects" echo " 2. Update the system package repository." echo " 3. Clone vlang into ~/Projects and build it" echo " 4. Build all v tools and recompile it with prod" echo "==================================================" # Ask for confirmation read -p "Do you want to proceed? [y/N]: " -r response response=$(echo "$response" | tr '[:upper:]' '[:lower:]') if [[ "$response" != "y" ]]; then echo "Operation cancelled." exit 1 fi # Helper function to run commands with sudo if not already root run_as_root() { if [ "$(id -u)" -eq 0 ]; then "$@" else # Asking for sudo permission before executing sudo "$@" fi } # Get projects directory and create it if necessary PROJECTS_DIR="${XDG_PROJECTS_DIR:-$HOME/Projects}" if [ ! -d "$PROJECTS_DIR" ]; then echo "Creating directory $PROJECTS_DIR" mkdir -p "$PROJECTS_DIR" else echo "Found $PROJECTS_DIR" fi if [ "$(id -u)" -ne 0 ]; then echo "Privileged access required to install packages." sudo -v || { echo "Authentication failed."; exit 1; } fi # Install all necessary packages for vlang PACKAGES="git build-essential libsqlite3-dev sqlite3 unixodbc postgresql-client libpq-dev libxi-dev libxcursor-dev mesa-common-dev make" echo "Updating package lists..." run_as_root apt-get update -y echo "Installing packages: $PACKAGES" if run_as_root apt-get install -y $PACKAGES; then echo "Success: All packages were installed successfully!" else # This block triggers if set -e isn't used or if custom failure logic is needed echo "Error: Package installation failed." >&2 exit 1 fi # Clone and Build Vlang inside PROJECTS_DIR (as regular user) cd "$PROJECTS_DIR" if [ -d "v" ]; then echo "Directory 'v' already exists in $PROJECTS_DIR. Skipping clone." else echo "Cloning V repository..." git clone --depth=1 https://github.com/vlang/v fi echo "Building V..." cd v make # Verify vlang executable if [ -x "./v" ]; then echo "Success: V compiler successfully built!" echo "Executable path: $(pwd)/v" else echo "Error: V build failed. Binary not found." >&2 exit 1 fi # Build v for prod + tools ./v -prod self ./v -prod build-tools # Symlink V echo "Creating symlink..." run_as_root ./v symlink # Verify symlink hash -r 2>/dev/null || true if command -v v >/dev/null 2>&1; then echo "Success: V is installed and available in PATH!" v version else echo "Error: 'v' command was not found in PATH." >&2 exit 1 fi