#!/usr/bin/env python3

# Description:
#   Adjust the SDR brightness setting in Hyprland's monitor configuration file.
#   Useful for adjusting brightness when HDR is enabled.

import sys
import os

if __name__ == "__main__":
    if len(sys.argv) != 2:
        new_brightness = 1
    else:
        try:
            new_brightness = float(sys.argv[1])
            if new_brightness < 1 or new_brightness > 1.5:
                raise ValueError()
        except Exception as e:
            new_brightness = 1

    print(f"Setting SDR brightness to: {new_brightness}\n")

    config_path = os.path.expanduser("~/.config/hypr/hyprland/monitors.conf")
    if not os.path.exists(config_path):
        print(f"Configuration file {config_path} does not exist.")
        sys.exit(1)

    with open(config_path, 'r') as file:
        lines = file.readlines()
        for line in lines:
            if "sdrbrightness" in line:
                old_line = line.strip()
                new_line = f"    sdrbrightness = {new_brightness}\n"
                lines[lines.index(line)] = new_line
                print(f"Updated: {old_line} to {new_line.strip()}\n")
                break

    with open(config_path, 'w') as file:
        file.writelines(lines)

    print(f"New {config_path} content: \n")
    with open(config_path, 'r') as file:
        print(file.read())
