import os
import plistlib
import json

# ------------ USER CONFIGURABLE OPTIONS -------------
chrome_safe_browsing_options = 2  # 0 = No protection, 1 = Standard, 2 = Enhanced
chrome_HTTPS_Only_Mode_options = 1  # 0 = disallowed, 1 = force_enabled
TrackingPrevention_options = 3
EnhanceSecurityMode_options = 2
MD_SmartScreen_options = 1
BlockPotentiallyUnwantedApps_options = 1
WebsiteTypoProtection_options = 1
SiteSafetyServices_options = 1
Scareware_Blocker_options = 1
EnableTrackingProtection_options = 1
Https_Only_Mode_options = 1
SafeBrowsing_options = 1
PrivateBrowsing_options = 1
# ---------------------------------------------------

CHROME_APP_PATH  = "/Applications/Google Chrome.app"
EDGE_APP_PATH    = "/Applications/Microsoft Edge.app"
FIREFOX_APP_PATH = "/Applications/Firefox.app"

def write_plist(path, data):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, 'wb') as f:
        plistlib.dump(data, f)

def setup_chrome():
    if not os.path.exists(CHROME_APP_PATH):
        print("Chrome is not installed, skipping.")
        return

    chrome_plist_path = "/Library/Managed Preferences/com.google.Chrome.plist"
    chrome_policies = {
        "SafeBrowsingProtectionLevel": chrome_safe_browsing_options,
        "HttpsOnlyMode": "force_enabled" if chrome_HTTPS_Only_Mode_options == 1 else "disallowed"
    }
    write_plist(chrome_plist_path, chrome_policies)
    print("Chrome policies written to:", chrome_plist_path)

def setup_edge():
    if not os.path.exists(EDGE_APP_PATH):
        print("Edge is not installed, skipping.")
        return

    edge_plist_path = "/Library/Managed Preferences/com.microsoft.Edge.plist"
    edge_policies = {
        "TrackingPrevention": TrackingPrevention_options,
        "EnhanceSecurityMode": EnhanceSecurityMode_options,
        "TyposquattingCheckerEnabled": WebsiteTypoProtection_options,
        "SiteSafetyServicesEnabled": SiteSafetyServices_options,
        "ScarewareBlockerProtectionEnabled": Scareware_Blocker_options,
        "SmartScreenEnabled": MD_SmartScreen_options,
        "SmartScreenPuaEnabled": BlockPotentiallyUnwantedApps_options
    }
    write_plist(edge_plist_path, edge_policies)
    print("Edge policies written to:", edge_plist_path)

def setup_firefox():
    if not os.path.exists(FIREFOX_APP_PATH):
        print("Firefox is not installed, skipping.")
        return

    policy_dir = "/Applications/Firefox.app/Contents/Resources/distribution"
    if not os.path.exists(policy_dir):
        os.makedirs(policy_dir)

    policies_json_path = os.path.join(policy_dir, "policies.json")

    policies = {
        "policies": {
            "DisablePrivateBrowsing": True if PrivateBrowsing_options == 1 else False,
            "EnableTrackingProtection": True if EnableTrackingProtection_options == 1 else False,
            "HTTPSOnlyMode": True if Https_Only_Mode_options == 1 else False,
            "SafeBrowsing": {
                "MalwareProtection": True if SafeBrowsing_options == 1 else False,
                "PhishingProtection": True if SafeBrowsing_options == 1 else False
            }
        }
    }

    with open(policies_json_path, "w") as f:
        json.dump(policies, f, indent=4)
    print("Firefox policies written to:", policies_json_path)

def main():
    print("Checking installed browsers...")
    print("Chrome  installed:", os.path.exists(CHROME_APP_PATH))
    print("Edge    installed:", os.path.exists(EDGE_APP_PATH))
    print("Firefox installed:", os.path.exists(FIREFOX_APP_PATH))
    print("---")

    for name, fn in [("Chrome", setup_chrome), ("Edge", setup_edge), ("Firefox", setup_firefox)]:
        try:
            fn()
        except Exception as e:
            print(name + " setup failed:", str(e))

main()