#!/bin/bash # Configure applications to use tmpfs caches # Run this after setting up tmpfs mounts to automatically configure your apps set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log() { echo -e "${BLUE}[INFO]${NC} $1" } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warn() { echo -e "${YELLOW}[WARNING]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running as root if [[ $EUID -ne 0 ]]; then error "This script must be run as root (use sudo)" exit 1 fi echo "🔧 Application tmpfs Cache Configurator" echo "=======================================" echo "" # Check if tmpfs mounts exist if ! mount | grep -q "tmpfs.*tmpfs-cache"; then error "No tmpfs-cache mounts found!" echo "Please run the optimizer first: sudo ./one-button-optimizer.sh" exit 1 fi log "Found tmpfs cache mounts:" mount | grep "tmpfs.*tmpfs-cache" | awk '{print " â€ĸ " $3}' echo "" configure_applications_for_tmpfs() { log "Auto-configuring applications to use tmpfs caches..." echo "" local configured_count=0 local current_user=$(logname 2>/dev/null || echo $SUDO_USER) local user_home=$(eval echo ~$current_user) # Browser Configuration echo "🌐 Configuring Browsers:" # Firefox for firefox_profile in "$user_home"/.mozilla/firefox/*.default* "$user_home"/.mozilla/firefox/*.default-release*; do if [[ -d "$firefox_profile" ]]; then local prefs_file="$firefox_profile/prefs.js" if [[ -f "$prefs_file" ]]; then # Check if already configured if grep -q "tmpfs-cache" "$prefs_file" 2>/dev/null; then log " â„šī¸ Firefox already configured" ((configured_count++)) continue fi # Backup original cp "$prefs_file" "$prefs_file.backup.$(date +%s)" 2>/dev/null || true # Remove old cache settings sed -i '/browser.cache.disk.parent_directory/d' "$prefs_file" 2>/dev/null || true # Add new cache location echo 'user_pref("browser.cache.disk.parent_directory", "/tmp/tmpfs-cache/browser/firefox");' >> "$prefs_file" # Create cache directory mkdir -p /tmp/tmpfs-cache/browser/firefox chown -R $current_user:$current_user /tmp/tmpfs-cache/browser/firefox success " ✅ Firefox configured to use tmpfs cache" ((configured_count++)) fi fi done # Chrome/Chromium if command -v google-chrome &>/dev/null || command -v chromium &>/dev/null; then local chrome_dir="$user_home/.config/google-chrome" local chromium_dir="$user_home/.config/chromium" for browser_dir in "$chrome_dir" "$chromium_dir"; do if [[ -d "$browser_dir" ]]; then local browser_name=$(basename "$browser_dir") mkdir -p /tmp/tmpfs-cache/browser/$browser_name # Create wrapper script to launch with cache dir local desktop_file="" if [[ "$browser_name" == "google-chrome" ]]; then desktop_file="$user_home/.local/share/applications/google-chrome.desktop" if [[ -f "$desktop_file" ]] && grep -q "tmpfs-cache" "$desktop_file" 2>/dev/null; then log " â„šī¸ Chrome already configured" ((configured_count++)) continue fi mkdir -p "$user_home/.local/share/applications" cat > "$desktop_file" << 'CHROMEOF' [Desktop Entry] Version=1.0 Name=Google Chrome (tmpfs optimized) Exec=google-chrome --disk-cache-dir=/tmp/tmpfs-cache/browser/google-chrome %U StartupNotify=true Terminal=false Icon=google-chrome Type=Application Categories=Network;WebBrowser; MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https; CHROMEOF else desktop_file="$user_home/.local/share/applications/chromium.desktop" if [[ -f "$desktop_file" ]] && grep -q "tmpfs-cache" "$desktop_file" 2>/dev/null; then log " â„šī¸ Chromium already configured" ((configured_count++)) continue fi mkdir -p "$user_home/.local/share/applications" cat > "$desktop_file" << 'CHROMIUMEOF' [Desktop Entry] Version=1.0 Name=Chromium (tmpfs optimized) Exec=chromium --disk-cache-dir=/tmp/tmpfs-cache/browser/chromium %U StartupNotify=true Terminal=false Icon=chromium Type=Application Categories=Network;WebBrowser; MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https; CHROMIUMEOF fi chown $current_user:$current_user "$desktop_file" 2>/dev/null || true chmod +x "$desktop_file" 2>/dev/null || true success " ✅ $browser_name configured to use tmpfs cache" ((configured_count++)) fi done fi # Brave if [[ -d "$user_home/.config/BraveSoftware" ]]; then mkdir -p /tmp/tmpfs-cache/browser/brave local brave_desktop="$user_home/.local/share/applications/brave-browser.desktop" if [[ -f "$brave_desktop" ]] && grep -q "tmpfs-cache" "$brave_desktop" 2>/dev/null; then log " â„šī¸ Brave already configured" ((configured_count++)) else mkdir -p "$user_home/.local/share/applications" cat > "$brave_desktop" << 'BRAVEEOF' [Desktop Entry] Version=1.0 Name=Brave Browser (tmpfs optimized) Exec=brave-browser --disk-cache-dir=/tmp/tmpfs-cache/browser/brave %U StartupNotify=true Terminal=false Icon=brave-browser Type=Application Categories=Network;WebBrowser; MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https; BRAVEEOF chown $current_user:$current_user "$brave_desktop" 2>/dev/null || true chmod +x "$brave_desktop" 2>/dev/null || true success " ✅ Brave configured to use tmpfs cache" ((configured_count++)) fi fi # Development Tools Configuration echo "" echo "đŸ’ģ Configuring Development Tools:" # NPM if command -v npm &>/dev/null; then local npm_cache=$(sudo -u $current_user npm config get cache 2>/dev/null) if [[ "$npm_cache" == *"tmpfs-cache"* ]]; then log " â„šī¸ NPM already configured" ((configured_count++)) else sudo -u $current_user npm config set cache /tmp/tmpfs-cache/development/npm 2>/dev/null && \ success " ✅ NPM cache configured" && ((configured_count++)) fi fi # Pip if command -v pip3 &>/dev/null || command -v pip &>/dev/null; then if [[ -f "$user_home/.config/pip/pip.conf" ]] && grep -q "tmpfs-cache" "$user_home/.config/pip/pip.conf" 2>/dev/null; then log " â„šī¸ Pip already configured" ((configured_count++)) else mkdir -p "$user_home/.config/pip" cat > "$user_home/.config/pip/pip.conf" << 'PIPEOF' [global] cache-dir = /tmp/tmpfs-cache/development/pip PIPEOF chown -R $current_user:$current_user "$user_home/.config/pip" 2>/dev/null || true success " ✅ Pip cache configured" && ((configured_count++)) fi fi # KDE/Plasma Configuration if [[ "$XDG_CURRENT_DESKTOP" == *"KDE"* ]] || [[ "$DESKTOP_SESSION" == *"plasma"* ]]; then echo "" echo "đŸ–Ĩī¸ Configuring KDE/Plasma:" # Link thumbnails if [[ -L "$user_home/.cache/thumbnails" ]] && [[ "$(readlink -f "$user_home/.cache/thumbnails")" == *"tmpfs-cache"* ]]; then log " â„šī¸ KDE thumbnails already configured" ((configured_count++)) else if [[ -d "$user_home/.cache/thumbnails" ]] && [[ ! -L "$user_home/.cache/thumbnails" ]]; then mv "$user_home/.cache/thumbnails" "$user_home/.cache/thumbnails.backup.$(date +%s)" 2>/dev/null || true fi mkdir -p /tmp/tmpfs-cache/kde/thumbnails ln -sf /tmp/tmpfs-cache/kde/thumbnails "$user_home/.cache/thumbnails" chown -R $current_user:$current_user /tmp/tmpfs-cache/kde/thumbnails success " ✅ KDE thumbnail cache linked to tmpfs" ((configured_count++)) fi fi echo "" if [[ $configured_count -gt 0 ]]; then success "🎉 Configured/verified $configured_count applications!" echo "" log "💡 Restart the configured applications to use tmpfs caches" echo "" echo "📊 Check cache usage with: df -h /tmp/tmpfs-cache/*" else log "â„šī¸ No applications detected for automatic configuration" echo "" echo " You can manually configure other applications to use:" echo " â€ĸ Browser cache: /tmp/tmpfs-cache/browser/" echo " â€ĸ Dev tools: /tmp/tmpfs-cache/development/" echo " â€ĸ IDE cache: /tmp/tmpfs-cache/ide/" fi } # Run the configuration configure_applications_for_tmpfs echo "" success "✅ Configuration complete!"