#!/usr/bin/perl
# Attempt at breaking apart icons.svg.  However, more work is needed to
# determine bounding boxes, offsets, used defs, etc.
# GPL, Kees Cook 2005-01-01
#
# Usage: g-split icons.svg
#
# It will create a directory "icons.svg.dir" with each group spat out
# as a separate svg file.  It reproduces the entire defs section in a
# gross attempt to make sure that all resources are available for the
# SVG to render.  Also note: not all icons are actually stored in groups.
# 
use strict;
use warnings;
use SVG (-indent => "  ");
use SVG::Parser;

# The currently-loaded SVG file being split up
my $CURRENT;

sub getDefaultSVG
{
	my ($name,%args)=@_;
	my @children = $args{'children'}||();
	my %params = %{$args{'params'}};

	#$params{'width'}||="766pt";
	#$params{'height'}||="256pt";
	$params{'width'}="100%";
	$params{'height'}="100%";

	my $svg = new SVG(
		id=>"svg1",
		'sodipodi:version'=>"0.32",
		'sodipodi:docname'=>$name,
		'inkscape:version'=>"0.40cvs",
		'sodipodi:docbase'=>"/scratch/src/inkscape/inkscape/share/icons",
		'xmlns'=>"http://www.w3.org/2000/svg",
   		'xmlns:cc'=>"http://web.resource.org/cc/",
   		'xmlns:inkscape'=>"http://www.inkscape.org/namespaces/inkscape",
   		'xmlns:sodipodi'=>"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
   		'xmlns:rdf'=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
   		'xmlns:dc'=>"http://purl.org/dc/elements/1.1/",
   		'xmlns:xlink'=>"http://www.w3.org/1999/xlink",
		%params,
	);

	$svg->getFirstChild->addchilds(
			$CURRENT->getElements("defs"),
			$CURRENT->getElements("sodipodi:namedview"),
			$CURRENT->getElements("metadata"),
			@children);
	return $svg;
}

sub dumpChildren
{
	my ($kid,$depth,$dir)=@_;;
	return if (!defined($kid));

	if (! -d $dir && !mkdir($dir,0777)) {
		warn "$dir: $!\n";
		return;
	}
	if (!chdir($dir)) {
		warn "$dir: $!\n";
		return;
	}

	my @children=$kid->getChildren();
	foreach $kid (@children) {
		my $name=$kid->getElementName;
		my $id=$kid->getElementID;
		if ($name eq "g") {
			my %params;
			# Steal first thing's height/width (hoping it's the
			# bounding box).  For many icons, it's correct,
			# since the initial obj in the group is the 
			# invisible bounding rect.  Not true for all of
			# them, though...
			my $child=$kid->getFirstChild;
			$params{'height'}=$child->getAttribute('height');
			$params{'width'}=$child->getAttribute('width');
			## Relocate to 0,0
			#$child->setAttribute("x","0");
			#$child->setAttribute("y","0");
			

			my $file="$id.svg";
			open(FILE,">$file") || warn "$file: $!\n";
			print FILE getDefaultSVG($file,
						 params => \%params,
						 children => $kid,
						)->xmlify();
			close(FILE);

			printf("%s %s\n",
				"." x $depth,
				$id);
		}
		
		if ($name =~ /^g|svg$/) {
			dumpChildren($kid,$depth+1,$id)
				if ($depth<1);
		}
#		else {
#			printf("%s id:%s name:%s SKIPPED\n",
#				"." x $depth,
#				$id, $name);
#		}
	}

	if (!chdir("..")) {
		warn "..: $!\n";
		next;
	}
	rmdir($dir); # remove any empty dirs... kinda hacky... oh well
}

foreach my $filename (@ARGV) {
	if (! -r $filename) {
		warn "$filename: not readable\n";
		next;
	}

	$CURRENT=SVG::Parser->new()->parsefile($filename);
	
	my $dir="${filename}.dir";
	dumpChildren($CURRENT->getFirstChild,1,$dir);
}

