#!/usr/bin/perl -w
#
# Author:	David Griffith <dave@661.org>
# Date:		January 1, 2019
# Version:	1.0
# License:	Public Domain
#
# Convert a mime.conf file to a mime.types file.
# Usage: mimeconf2mimetypes.pl <mime.conf> [-o <mime.types>]
#
# Some servers don't accept MIME type descriptions as a mime.conf file.  
# For those situtations where you have a mime.conf file and you need 
# mime.types, this script will quickly and easily do the conversion for 
# you.

# The original motivation for creating this script was to make things 
# easier when serving up a mirror of the Interactive Fiction Archive 
# (http://ifarchive.org) with a Gopher server.  The specific gopher server 
# being used, pygopherd(8), accepts a list of mime types as a mime.types 
# file, not the Apache-style mime.conf.

use strict;
use Pod::Usage;
use File::Basename;
use Getopt::Long qw(:config no_ignore_case);

my $progname = basename($0);
my %options;
my $infile = $ARGV[0];
my $outfile;
my $line;
my $command;
my $type;
my $version = "1.0";
my @extension;

GetOptions(
	'usage|?' => \$options{usage},
	'h|help' => \$options{help},
	'o|output=s' => \$options{output},
	'v|version' => \$options{version}
	);

pod2usage(1) if $options{usage};
pod2usage(-verbose => 2) if $options{help};

if ($options{version}) {
	print "$progname version $version\n";
	exit;
}

if (!$infile) { pod2usage(1); }

if ($options{output}) {
	$outfile = $options{output};
	open (OUTFILE, ">", $outfile) or die "Unable to write to $outfile\n";
} else {
	*OUTFILE = *STDOUT;
}

open (INFILE, "<", $infile) or die "Unable to read $infile\n";

while ($line = <INFILE>) {
	if ($line =~ /.*\S.*/) {
		($command, $type, @extension) = split(/\s+/, $line);
	}

	if ($command =~ "^AddType") {
		foreach $_ (@extension) {
			print OUTFILE "$type $_\n";
		}
	}
}

__END__


=head1 NAME

mimeconf2mimetypes.pl - Convert a mime.conf file to a mime.types file

=head1 SYNOPSIS

mimeconf2mimetypes.pl <mime.conf> [-o <output file>]

=head1 DESCRIPTION

Some servers don't accept MIME type descriptions as a mime.conf file.  
For those situtations where you have a mime.conf file and you need 
mime.types, this script will quickly and easily do the conversion for 
you.

=head2 Option flags

  -?			Print simple usage message.
  -h			Verbose help message.
  -o outfile		Write mime.types format to this file.

=head2 Examples

Basic invocation
    mimeconf2mimetypes.pl mime.conf > mime.types

Slightly trickier
    mimeconf2mimetypes.pl < mime.conf > mime.types
    cat /home/joe/mime.conf | genmimetypes > /etc/pygopherd/mime.types

Without redirection
    mimeconf2mimetypes.pl mime.conf -o /etc/pygopherd/mime.types

=head1 REVISION HISTORY

  Version 1.0		Initial public release

=head1 NOTES

The original motivation for creating this script was to make things 
easier when serving up a mirror of the Interactive Fiction Archive 
(http://ifarchive.org) with a Gopher server.  The specific gopher server 
being used, pygopherd(8), accepts a list of mime types as a mime.types 
file, not the Apache-style mime.conf.

=head1 LICENSE

Public Domain

=head1 AUTHOR

David Griffith <dave@661.org>

=cut