import sys
import os
import cairosvg

def convert_svg_to_png(input_path, output_path=None, width=None, height=None):
    if not os.path.isfile(input_path):
        raise FileNotFoundError(f"Input file '{input_path}' not found.")

    if output_path is None:
        base, _ = os.path.splitext(input_path)
        output_path = base + ".png"

    cairosvg.svg2png(
        url=input_path,
        write_to=output_path,
        output_width=width,
        output_height=height
    )

    print(f"Converted: {input_path} -> {output_path}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python svg_to_png.py input.svg [output.png] [width height]")
        sys.exit(1)

    input_svg = sys.argv[1]
    output_png = sys.argv[2] if len(sys.argv) > 2 else None
    width = int(sys.argv[3]) if len(sys.argv) > 3 else None
    height = int(sys.argv[4]) if len(sys.argv) > 4 else None

    convert_svg_to_png(input_svg, output_png, width, height)