#!/usr/bin/env bash
# BlueStack LLC - 2GB Swap File Setup Script
# Usage: sudo bash a1-swap-setup.sh [size_in_GB, default=2]
set -euo pipefail

SIZE_GB="${1:-2}"
if swapon --show | grep -q '/swapfile'; then
  echo "Swap file already active:"
  swapon --show
  exit 0
fi

echo "Creating ${SIZE_GB}G swap file at /swapfile..."
fallocate -l "${SIZE_GB}G" /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=$((SIZE_GB * 1024))
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
grep -q '/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab

echo "Swap enabled successfully:"
free -h
