#!/bin/bash

# 1. Target the default Linux Firefox profile path
FF_DIR="$HOME/.mozilla/firefox"

if [ ! -d "$FF_DIR" ]; then
    echo "Firefox profile directory not found."
    exit 1
fi

# 2. Iterate through all profile directories
find "$FF_DIR" -maxdepth 1 -type d -name "*.*" | while read -r profile; do
    echo "Configuring Profile: $(basename "$profile")"
    
    # 3. Force the about:config preference via user.js
    USER_JS="$profile/user.js"
    PREF='user_pref("toolkit.legacyUserProfileCustomizations.stylesheets", true);'
    
    if [ -f "$USER_JS" ]; then
        if ! grep -q "toolkit.legacyUserProfileCustomizations.stylesheets" "$USER_JS"; then
            echo "" >> "$USER_JS"
            echo "$PREF" >> "$USER_JS"
        fi
    else
        echo "$PREF" >> "$USER_JS"
    fi

    # 4. Build the chrome folder structure
    CHROME_DIR="$profile/chrome"
    mkdir -p "$CHROME_DIR"

    # 5. Populate userChrome.css
    cat << 'EOF' > "$CHROME_DIR/userChrome.css"
/* 1. Force compact padding on right-click context menus */
menupopup > menuitem, menupopup > menu {
  padding-block: 2px !important;
  padding-inline: 4px !important;
}
menupopup {
  --proton-contextmenu-padding: 2px 0 !important;
}

/* 2. Force compact padding on the sandwich/hamburger menu and submenus */
:root {
  --panel-menuitem-padding: 2px 8px !important;
}

/* 3. Strip out the forced touchscreen spacing expansion across all menus */
#downloadsPanel, #appMenu-popup, .panel-subview-body, panelmultiview {
  --arrowpanel-menuitem-padding: 2px 8px !important;
  --panel-menuitem-padding: 2px 8px !important;
}

/* 4. Fix row heights inside the main panel */
.panel-menuitem-button, 
toolbarbutton.subviewbutton, 
.widget-overflow-list .toolbarbutton-1 {
  padding-block: 2px !important;
  min-height: unset !important;
}
EOF

done

echo "Done! Fully close and restart Firefox."
