new object $mime_lib: $libraries;

var $mime_lib mime_types = #[];
var $root inited = 1;

public method .mime_type() {
  arg filename;
  var ext;

  ext = filename.explode(".").last();
  catch ~keynf {
    return mime_types[ext];
  } with {
    return "text/plain";
  }
};

public method .startup() {
  var line, i;
  
  if (caller() != $sys) {
    throw(~perm, "Caller is not $sys");
  }
  fopen("/mime.types");
  mime_types = #[];
  catch ~eof {
    while (1) {
      line = fread();
      if (line && line[1] != "#") {
	line = line.explode();
	for i in (line.subrange(2)) {
	  mime_types = mime_types.add(i, line[1]);
	}
      }
    }
  }
  fclose();
};