#!/usr/bin/env python3.9

from configparser import ConfigParser
from pathlib import Path
from subprocess import run
import time
import sys


FIREFOX_DIR = Path("/Users/ambv/Library/Application Support/Firefox/")


def main():
    assert_exists(FIREFOX_DIR)
    assert_exists(FIREFOX_DIR / 'installs.ini')
    assert_exists(FIREFOX_DIR / 'profiles.ini')
    switch_to_profile_or_die(name='safe')
    try:
        proc = run(['open', '-a', 'Firefox'])
        if proc.stdout:
            print(proc.stdout)
        if proc.stderr:
            print(proc.stderr, file=sys.stderr)
        if proc.returncode:
            print("error: unable to start Firefox", file=sys.stderr)
            sys.exit(3)
    finally:
        time.sleep(5)
        switch_to_profile_or_die(name='default')


def assert_exists(path):
    if not path.exists():
        print(f"fatal: no Firefox config under {path}", file=sys.stderr)
        sys.exit(1)


def switch_to_profile(name):
    # Switch all installs to profile `name` first
    installs_path = FIREFOX_DIR / 'installs.ini'
    installs = ConfigParser()
    installs.optionxform = lambda option: option
    installs.read(installs_path)
    for install in installs:
        installs[install]['Default'] = f'Profiles/{name}'
    with installs_path.open('w') as f:
        installs.write(f, space_around_delimiters=False)

    # Now patch profiles.ini, along with the duplicate "Install" sections
    profiles_path = FIREFOX_DIR / 'profiles.ini'
    profiles = ConfigParser(default_section='General')
    profiles.optionxform = lambda option: option
    profiles.read(profiles_path)
    found = False
    for profile in profiles:
        if 'Name' not in profiles[profile]:
            if (
                profile.startswith('Install')
                and profile[len('Install'):] in installs
            ):
                profiles[profile]['Default'] = f'Profiles/{name}'
            continue

        if profiles[profile]['Name'] == name:
            found = True
            profiles[profile]['Default'] = '1'
        elif 'Default' in profiles[profile]:
            del profiles[profile]['Default']

    if not found:
        raise LookupError(f'profile {name!r} not found in Firefox profile.ini')

    with profiles_path.open('w') as f:
        profiles.write(f, space_around_delimiters=False)


def switch_to_profile_or_die(name):
    try:
        return switch_to_profile(name)
    except LookupError as le:
        print(le, file=sys.stderr)
        sys.exit(2)


if __name__ == '__main__':
    main()
