#!/usr/bin/perl

use strict;


my %mimeTypes;
my $maxLength = 0;

if(open(FILE, "</etc/mime.types"))
{
	while(my $line = <FILE>)
	{
		next if(($line =~ /^#/) || ($line =~ /^\s*$/));
		
		my @words = split(/\s+/, $line);
		
		next if(scalar(@words) == 1);
		
		for(my $count = 1; $count < scalar(@words); $count++)
		{
			$mimeTypes{$words[$count]} = $words[0];
			
			$maxLength = length($words[$count]) if(length($words[$count]) > $maxLength);
		}
	}
	
	close(FILE);
}

my $output = '';
my $count = 1;

foreach my $extension (sort keys %mimeTypes)
{
	my $pad = '';
	$pad .= ' ' while(length($pad) + length($extension) < $maxLength + 1);
	
	$output .= ",\n" if($output ne '');
	$output .= "\t'$extension'$pad=> '$mimeTypes{$extension}'";
	
	$count++;
}

$output = "\$mime_types = array(\n$output\n);";

if(open(FILE, ">mime_type_var.code"))
{
	print FILE $output;
	close(FILE);
	
	print "Done. Content in mime_type_var.code file in current directory.\n";
}
else
{
	print "Error: Could not open file mime_type_var.code in current directory. Make sure that you are running this program in a place where your user has sufficient permissions to create files.\n";
}

exit;
