#!/usr/bin/perl
use strict;

my $outputp = "";
my $input = "";

my $version = 0.1;
my $vname = "separeDitags.pl";

if (!$ARGV[0] || $ARGV[0] eq "-h") {
	print "\n\t-i <FILE> \t:: file input to <FILE>\n",
	"\t-o <FILE> \t:: file output prefix <FILE>\n",
	"\t-h      \t:: shows help information\n",
	"\t-v      \t:: shows current version\n",
	"\n\t$vname Version $version\n\n";
	exit;
}

if ($ARGV[0] eq "-v") {
	print ("\n\t$vname Version $version\n\n");
	exit;
}


#
#first read command line to collect arguments
#
while (my $arg=shift(@ARGV)) {
	#
	#output file
	#
	if ($arg eq "-o") {
		if ($arg = shift(@ARGV)) {
			$outputp = $arg;
		}
		else {
			print STDERR "OUTPUT FILE NOT SPECIFIED!\n";
			exit;
		}
	}

	#
	#input file
	#
	elsif ($arg eq "-i") {
		if ($arg = shift(@ARGV)) {
			$input = $arg;
		}
		else {
			print STDERR "INPUT FILE NOT SPECIFIED!\n";
			exit;
		}
	}
}



my %sizes;



if ($input) {
	open INPUT, "<$input" or die "COULD NOT OPEN $input : $!\n";
	$input = "<INPUT>";
}
else {
	$input = "<>";
}


while (my $entry = eval ($input)) {
	chomp ($entry);

	(my $ditag, my $junk) = split ("=", $entry);
	my $n = length ($ditag);
	push (@{$sizes{$n}}, $entry);
}



while (my ($size, $entry) = each (%sizes)) {
	open OUTPUT, ">".$outputp.$size or die "COULD NOT OPEN $outputp.$size : $!\n";
	print $outputp.$size, "\n";
	while (@{$entry}) {
		print OUTPUT pop @{$entry}, "\n";
	}
}
