#! /usr/bin/env fish

set firefox_profile_dir ~/.mozilla/firefox
set policies_dir /usr/lib/firefox/distribution
set firefox_dir (realpath (dirname (realpath (status filename)))/../../firefox)

if test (uname) = Darwin
    set firefox_profile_dir ~/Library/Application\ Support/Firefox
    set policies_dir /Applications/Firefox.app/Contents/Resources/distribution
end

if pgrep -xi firefox >/dev/null
    echo "error: firefox is running. close it first so prefs apply correctly." >&2
    exit 1
end

if not test -f "$firefox_profile_dir/profiles.ini"
    echo "error: $firefox_profile_dir/profiles.ini not found" >&2
    exit 1
end

set profiles (rg -Nor '$1' '^Path=(.*)' "$firefox_profile_dir/profiles.ini" 2>/dev/null)

if test (count $profiles) -eq 0
    echo "error: no profiles found in $firefox_profile_dir/profiles.ini" >&2
    exit 1
end

for profile in $profiles
    set profdir $firefox_profile_dir/$profile
    if string match -q '/*' $profile
        set profdir $profile
    end

    if not test -d "$profdir"
        echo "warning: profile dir $profdir not found, skipping" >&2
        continue
    end

    echo "configuring profile: $profile"

    # userChrome.css
    mkdir -p $profdir/chrome
    set chrome_target $profdir/chrome/userChrome.css
    set chrome_source $firefox_dir/userChrome.css
    ln -sfv $chrome_source $chrome_target

    # includes
    set includes_target $profdir/chrome/includes
    set includes_source $firefox_dir/includes
    ln -sfv $includes_source $includes_target

    # user.js
    set js_target $profdir/user.js
    set js_source $firefox_dir/user.js
    ln -sfv $js_source $js_target
end

# policies.json for extension installs and locked prefs
set policies_file $policies_dir/policies.json

set policies_content '{
  "policies": {
    "Preferences": {
      "toolkit.legacyUserProfileCustomizations.stylesheets": { "Value": true, "Status": "locked" },
      "browser.compactmode.show": { "Value": true, "Status": "locked" },
      "browser.uidensity": { "Value": 1, "Status": "locked" }
    },
    "ExtensionSettings": {
      "tridactyl.vim@cmcaine.co.uk": {
        "installation_mode": "force_installed",
        "install_url": "https://addons.mozilla.org/firefox/downloads/latest/tridactyl.vim@cmcaine.co.uk/latest.xpi",
        "private_browsing": true
      },
      "pywalfox@frewacom.org": {
        "installation_mode": "force_installed",
        "install_url": "https://addons.mozilla.org/firefox/downloads/latest/pywalfox@frewacom.org/latest.xpi",
        "private_browsing": true
      },
      "uBlock0@raymondhill.net": {
        "installation_mode": "force_installed",
        "install_url": "https://addons.mozilla.org/firefox/downloads/latest/uBlock0@raymondhill.net/latest.xpi",
        "private_browsing": true
      }
    }
  }
}'

if not test -d "$policies_dir"
    if test -w (dirname "$policies_dir")
        mkdir -p "$policies_dir"
    else
        sudo mkdir -p "$policies_dir"
    end
end

if test -w "$policies_dir"
    echo "$policies_content" >"$policies_file"
else
    echo "$policies_content" | sudo tee "$policies_file" >/dev/null
end

echo "done. restart firefox to apply changes."
