File mime-info-to-mime of Package shared-mime-info

#! /usr/bin/perl

# mime-info-to-mime
# Script to help with conversion from mime-info to shared-mime-info.
#
# Conversion for simple description is automatic and correct, for
# complicated or buggy descriptions, manual check is required (and
# convertor returns error code 1).
#
# Call it without arguments to perform conversion of all files in your
# system (previously installed files will not be overwritten) or use
# DESTDIR variable, if you want to create package.
#
# (c) 2004 SuSE CR
# Author: Stanislav Brabec <sbrabec@suse.cz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

if ( $ENV{"DESTDIR"} eq "" && $ARGV[0] ne "--force" ) {
    print "System-wide \"$0\" is disabled. It may create bogus definitions!\n";
    print "Use \"$0 --force\" to force it or set DESTDIR.\n";
    exit 1;
}

# directory with mime-info
$mimeinfodir=$ENV{"DESTDIR"} . "/usr/share/mime-info";

# directory with shared-mime-info
$sharedmimedir=$ENV{"DESTDIR"} . "/usr/share/mime/packages";

$rc=0;

system "mkdir -p $sharedmimedir ; rm -v 2>/dev/null \$(fgrep 2>/dev/null -l \"generated by mime-info-to-mime\" $sharedmimedir/*.xml)";

foreach $mimefile (glob("$mimeinfodir/*.mime")) {
    $mimename = $mimefile;
    $mimename=substr($mimename, length($mimeinfodir)+1, length($mimename)-length($mimeinfodir)-6);

    if (-e "$sharedmimedir/$mimename.xml")
    {
	print "WARNING: File $sharedmimedir/$mimename.xml already exists. No conversion.\n";
	next;
    }
# Maybe uncomment in future, but now gnome-vfs.xml is 4 times longer than freedesktop.org.xml.
# GNOME 2
#    if ( $mimename =~ "gnome-vfs" )
#    {
#	print "INFO: Not creating gnome-vfs.xml. Data types should be present in freedesktop.org.xml.\n";
#	next;
#    }
# GNOME 1.4
#    if ( $mimename =~ "gnome" )
#    {
#	print "INFO: Not creating gnome.xml. Data types should be present in freedesktop.org.xml.\n";
#	next;
#    }
    open($xml, ">$sharedmimedir/$mimename.xml");
    print $xml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    print $xml "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n";
    print $xml "<!-- generated by mime-info-to-mime (Remove this line to prevent overwriting!) -->\n";

    open($mime, "<$mimefile");
    $lineno=0;
    while ( <$mime> )
    {
	$lineno++;
	$line = $_;
	if ($line =~ /^[^ \t]+\/[^ \t]+/)
	{
	    chomp $line;
	    $mime_type=$line;
	}
	else
	{
	    if ($line =~ /^[ \t]/)
	    {
		if ($line =~ /^[ \t]+ext:[ \t]?/)
		{
		    $line =~ s/^[ \t]+ext:[ \t]?// ;
		    chomp $line;
		    foreach $ext (split " ", $line)
		    {
			$xmlstring{$mime_type}=$xmlstring{$mime_type} . "    <glob pattern=\"*.$ext\" />\n";
		    }
		}
		else
		{
		    if ($line =~ /^[ \t]+ext,[0-9]+:[ \t]?/)
		    {
			$line =~ s/^[ \t]+ext,[0-9]+:[ \t]?// ;
			chomp $line;
			print "WARNING in $mimename.mime $lineno: priority field ignored: $line\n";
			foreach $ext (split " ", $line)
			{
			    $xmlstring{$mime_type}=$xmlstring{$mime_type} . "    <glob pattern=\"*.$ext\" />\n";
			}
		    }
		    else
		    {
			if ($line =~ /^[ \t]+regex:[ \t]?/)
			{
			    $line =~ s/^[ \t]+regex:[ \t]?// ;
			    chomp $line;
			    print "ERROR in $mimename.mime $lineno: regex is not supported, FIXME(regex) created. Edit file manually.\n";
			    $rc=1;
			    $xmlstring{$mime_type}=$xmlstring{$mime_type} . "    <glob pattern=\"FIXME(regex) $line\" />\n";
			}
			else
			{
			    if ($line =~ /^[ \t]+regex,[0-9]+:[ \t]?/)
			    {
				$line =~ s/^[ \t]+regex,[0-9]+:[ \t]?// ;
				chomp $line;
				print "WARNING in $mimename.mime $lineno: priority field ignored: $line\n";
				print "ERROR in $mimename.mime $lineno: regex is not supported, FIXME(regex) created. Edit file manually.\n";
				$rc=1;
				$xmlstring{$mime_type}=$xmlstring{$mime_type} . "    <glob pattern=\"FIXME(regex) $line\" />\n";
			    }
			    else
			    {
				print "ERROR in $mimename.mime $lineno: unknown line format: $line\n";
				$rc=1;
			    }
			}
		    }
		}
	    }
	    else
	    {
		if ($line =~ /^$/)
		{
		    $mime_type="FIXME";
		}
		else
		{
		    chomp $line;
		    if ($line =~ /^#/ )
			{
			    $line =~ s/^#*[ \t]*// ;
			    print $xml "<!-- $line -->\n";
			}
			else
			{
			    print "ERROR in $mimename.mime $lineno: unknown line: $line\n";
			    $rc=1;
			}
		    }
		}
	    }
	}
	close($mime);

	if (-e "$mimeinfodir/$mimename.keys")
	{
	    open($keys, "<$mimeinfodir/$mimename.keys");
	    $lineno=0;
	    while ( <$keys> )
	    {
		$lineno++;
		$line = $_;
		if ($line =~ /^[^ \t]+\/[^ \t]+/)
		{
		    chomp $line;
		    $mime_type=$line;
		}
		else
		{
		    if ($line =~ /^[ \t]/)
		    {
			chomp $line;
			$line =~ s/^[ \t]*// ;
			if ($line =~ /^\[[^=]+\][^=]+=/)
			{
			    $lang=$line;
			    $lang =~ s/^\[([^=]+)\][^=]+=.*$/\1/ ;
			    $item=$line;
			    $item =~ s/^\[[^=]+\]([^=]+)=.*$/\1/ ;
			    $value=$line;
			    $value =~ s/^\[[^=]+\][^=]+=(.*)$/\1/ ;
			}
			else
			{
			    if ($line =~ /^[^=]+=/)
			    {
				$item=$line;
				$item =~ s/^([^=]+)=.*$/\1/ ;
				$value=$line;
				$value =~ s/^[^=]+=(.*)$/\1/ ;
				$lang="";
			    }
			    else
			    {
				print "ERROR in $mimename.keys $lineno: unknown line format: $line\n";
				$rc=1;
			    }
			}

			if ($item =~ /^description$/)
			{
			    if ($lang)
			    {
				$xmlstring_c{$mime_type}=$xmlstring_c{$mime_type} . "    <comment xml:lang=\"$lang\">$value</comment>\n";
			    }
			    else
			    {
				$xmlstring_c{$mime_type}=$xmlstring_c{$mime_type} . "    <comment>$value</comment>\n";
			    }
			}
		    }
		    else
		    {
			if ($line =~ /^$/)
			{
			    $mime_type="FIXME";
			}
			else
			{
			    chomp $line;
			    if ($line =~ /^#/ )
				{
				    $line =~ s/^#*[ \t]*// ;
				    print $xml "<!-- $line -->\n";
				}
				else
				{
				    print "ERROR in $mimename.keys $lineno: unknown line: $line\n";
				    $rc=1;
				}
			    }
			}
		    }
		}
		close($keys);
	    }

	    foreach $mime_type (keys %xmlstring) {
		print $xml "  <mime-type type=\"$mime_type\">\n";
		print $xml $xmlstring_c{$mime_type};
		print $xml $xmlstring{$mime_type};
		print $xml "  </mime-type>\n";
	    }
	    print $xml "</mime-info>\n";
	    close($xml);

	    undef %xmlstring;
	    undef %xmlstring_c;

	}
	exit $rc;
openSUSE Build Service is sponsored by