#!/usr/bin/env python

from sys import exit
import re

# match a simplified form of rule with
# at least one extension
rule = re.compile("""
    ([a-z0-9\/.+-]+)
    \s+
    ([a-z0-9.+-][a-z0-9. +-]*)
""", re.IGNORECASE | re.VERBOSE)

try:
    mime_file = open("/etc/mime.types", "r")

    # collapse lines ending with a slash
    text = re.sub(r"\\(\n|(\r\n?))", "", mime_file.read())
except (OSError, IOError):
    exit()

print("mimetype.assign = (")

types = set()
for line in map(str.strip, text.splitlines()):
    # rule matching will prevent comments or
    # empty lines to pass

    match = rule.match(line)
    if not match:
        continue
    
    mime, exts = match.groups()

    for ext in exts.split():
        if not ext in types:
            types.add(ext)
            print('".%s" => "%s",' % (ext, mime.lower()))

print(")")
