#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

import sys
from PIL import Image  # type: ignore[import]

if len(sys.argv) < 3:
    sys.stderr.write('So gehts:\n')
    sys.stderr.write('  add-icon-category category_name pfad/zu/den/bildern*.png\n')
    sys.exit(1)

category = sys.argv[1]
images = sys.argv[2:]

for image in images:
    im = Image.open(image)
    im.info['Comment'] = category

    from PIL import PngImagePlugin
    meta = PngImagePlugin.PngInfo()
    for k, v in im.info.items():
        if k not in ('interlace', 'gamma', 'dpi', 'transparency', 'aspect'):
            meta.add_text(k, v, 0)
    im.save(image, 'PNG', pnginfo=meta)
