# as with the touch panel software, this little script, called from a batch file, creates the meta information
# for a winose update (a zip, created by the batch file prior to calling this script)
import sys
import json
from hashlib import sha1


def create_info_json(filename):
    from wnsq.base_settings import WINOSE_MAJOR, WINOSE_MINOR, WINOSE_REVISION
    try:
        hashing = sha1()
        with open(filename, 'rb') as packageFile:
            buffer = packageFile.read()
            hashing.update(buffer)

            json_data = json.dumps({
                'package': filename,
                'checksum': hashing.hexdigest(),
                'major': WINOSE_MAJOR,
                'minor': WINOSE_MINOR,
                'revision': WINOSE_REVISION
            })

            with open("updates\\update.json", 'w') as updateInfoFile:
                updateInfoFile.write(json_data)

    except Exception as exp:
        print("Error creating update meta information.\n{0}".format(exp))


if __name__ == '__main__':
    create_info_json(sys.argv[1])
