#!/usr/bin/perl -w

# This script is based on /usr/share/lighttpd/create-mime-assign.pl. This
# script is changed to include a charset for text types.

use strict;
open MIMETYPES, "/etc/mime.types" or exit;
print "mimetype.assign = (\n";
my %extensions;
while(<MIMETYPES>) {
  chomp;
  s/\#.*//;
  next if /^\w*$/;
  if(/^([a-z0-9\/+-.]+)\s+((?:[a-z0-9.+-]+[ ]?)+)$/) {
    my $mime = $1; my $exts = $2;
    # Append encoding for text formats
    if ($mime =~ /^text\//) {
      $mime .= "; charset=utf-8";
    }
    foreach(split / /, $exts) {
      # mime.types can have same extension for different
      # mime types
      next if $extensions{$_};
      $extensions{$_} = 1;

      print "\".$_\" => \"$mime\",\n";
    }
  }
}
print ")\n";
