#!/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. It is also changed to
# serve some file types as text/plain, since browsers still don't have an
# option to just view text/* in the browser, but need external programs for
# anything but text/plain.

use strict;
open MIMETYPES, "/etc/mime.types" or exit;
print "mimetype.assign = (\n";
my %extensions;
my %translate_types = (
	"text/x-diff" => "text/plain",
);

while(<MIMETYPES>) {
  chomp;
  s/\#.*//;
  next if /^\w*$/;
  if(/^([a-z0-9\/+-.]+)\s+((?:[a-z0-9.+-]+[ ]?)+)$/) {
    my $mime = $1; my $exts = $2;
    # Translate the mime type if needed
    $mime = $translate_types{$mime} if exists $translate_types{$mime};
    # 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";
