Index: /trunk/gsdl/bin/script/import.pl
===================================================================
--- /trunk/gsdl/bin/script/import.pl	(revision 1286)
+++ /trunk/gsdl/bin/script/import.pl	(revision 1287)
@@ -60,4 +60,6 @@
     print STDERR "   -maxdocs number        Maximum number of documents to import\n";
     print STDERR "   -groupsize number      Number of GML documents to group into one file\n";
+    print STDERR "   -sortmeta metadata     Sort documents alphabetically by metadata for\n";
+    print STDERR "                          building. This will be disabled if groupsize > 1\n";
     print STDERR "   -debug                 Print imported text to STDOUT\n\n";
 }
@@ -69,5 +71,5 @@
     my ($verbosity, $importdir, $archivedir, $keepold, 
 	$removeold, $gzip, $groupsize, $debug, $maxdocs, $collection,
-	$configfilename, $collectcfg, $pluginfo, 
+	$configfilename, $collectcfg, $pluginfo, $sortmeta,
 	$archive_info_filename, $archive_info, $processor);
     if (!parsargv::parse(\@ARGV, 
@@ -79,4 +81,5 @@
 			 'gzip', \$gzip,
 			 'groupsize/\d+/1', \$groupsize,
+			 'sortmeta/.*/', \$sortmeta,
 			 'debug', \$debug,
 			 'maxdocs/^\-?\d+/-1', \$maxdocs)) {
@@ -92,4 +95,12 @@
 	&print_usage();
 	die "\n";
+    }
+
+    # check sortmeta
+    $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
+    if (defined $sortmeta && $groupsize > 1) {
+	print STDERR "WARNING: import.pl cannot sort documents when groupsize > 1\n";
+	print STDERR "         sortmeta option will be ignored\n\n";
+	$sortmeta = undef;
     }
 
@@ -160,4 +171,5 @@
 	$processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize);
 	$processor->setarchivedir ($archivedir);
+	$processor->set_sortmeta ($sortmeta) if defined $sortmeta;
     } else {
 	$processor = new docprint ();
@@ -173,5 +185,5 @@
     # write out the archive information file
     if (!$debug) {
-	$processor->close_file_output();
+	$processor->close_file_output() if $groupsize > 1;
 	$archive_info->save_info($archive_info_filename);
     }
Index: /trunk/gsdl/perllib/arcinfo.pm
===================================================================
--- /trunk/gsdl/perllib/arcinfo.pm	(revision 1286)
+++ /trunk/gsdl/perllib/arcinfo.pm	(revision 1287)
@@ -93,5 +93,5 @@
 	my $i = 0;
 	while ($i < scalar (@{$self->{'order'}})) {
-	    if ($self->{'order'}->[$i] eq $OID) {
+	    if ($self->{'order'}->[$i]->[0] eq $OID) {
 		splice (@{$self->{'order'}}, $i, 1);
 		last;
@@ -105,9 +105,10 @@
 sub add_info {
     my $self = shift (@_);
-    my ($OID, $doc_file) = @_;
+    my ($OID, $doc_file, $sortmeta) = @_;
+    $sortmeta = "" unless defined $sortmeta;
 
     $self->delete_info ($OID);
     $self->{'info'}->{$OID} = [$doc_file];
-    push (@{$self->{'order'}}, $OID);
+    push (@{$self->{'order'}}, [$OID, $sortmeta]);
 }
 
@@ -119,6 +120,6 @@
     my @list = ();
 
-    foreach $OID (@{$self->{'order'}}) {
-	push (@list, [$OID, $self->{'info'}->{$OID}->[0]]);
+    foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
+	push (@list, [$OID->[0], $self->{'info'}->{$OID->[0]}->[0]]);
     }
 
@@ -133,6 +134,6 @@
     my @list = ();
 
-    foreach $OID (@{$self->{'order'}}) {
-	push (@list, [$self->{'info'}->{$OID}->[0], $OID]);
+    foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
+	push (@list, [$self->{'info'}->{$OID->[0]}->[0], $OID->[0]]);
     }
 
Index: /trunk/gsdl/perllib/docsave.pm
===================================================================
--- /trunk/gsdl/perllib/docsave.pm	(revision 1286)
+++ /trunk/gsdl/perllib/docsave.pm	(revision 1287)
@@ -56,4 +56,6 @@
     $self->{'archive_dir'} = "$ENV{'GSDLHOME'}/collect/$self->{'collection'}/archives";
 
+    $self->{'sortmeta'} = undef;
+
     return bless $self, $class;
 }
@@ -66,9 +68,71 @@
 }
 
+sub set_sortmeta {
+    my $self = shift (@_);
+    my ($sortmeta) = @_;
+
+    $self->{'sortmeta'} = $sortmeta;
+}
+
 sub process {
     my $self = shift (@_);
     my ($doc_obj) = @_;
 
-    my $archive_dir = $self->{'archive_dir'};
+    if ($self->{'groupsize'} > 1) {
+	$self->group_process ($doc_obj);
+
+    } else {
+	# groupsize is 1 (i.e. one document per GML file) so sortmeta 
+	# may be used
+
+	my $OID = $doc_obj->get_OID();
+	$OID = "NULL" unless defined $OID;
+	
+	# get document's directory
+	my $doc_dir = $self->get_doc_dir ($OID);
+    
+	# copy all the associated files, add this information as metadata
+	# to the document
+	$self->process_assoc_files ($doc_obj, $doc_dir);
+
+	my $doc_file 
+	    = &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.gml");
+	my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
+	    
+	if (!open (OUTDOC, ">$doc_file")) {
+	    print STDERR "docsave::process could not write to file $doc_file\n";
+	    return;
+	}
+
+	# save this document
+	$doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
+	close OUTDOC;
+
+	if ($self->{'gzip'}) {
+	    my $doc_file = $self->{'gs_filename'};
+	    `gzip $doc_file`;
+	    $doc_file .= ".gz";
+	    $short_doc_file .= ".gz";
+	    if (!-e $doc_file) {
+		print STDERR "error while gzipping: $doc_file doesn't exist\n";
+		return 0;
+	    }
+	}
+
+	# do the sortmeta thing
+	my ($metadata);
+	if (defined ($self->{'sortmeta'})) {
+	    $metadata = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'sortmeta'});
+	}
+
+	# store reference in the archive_info
+	$self->{'archive_info'}->add_info($OID, $short_doc_file, $metadata);
+    }
+}
+
+sub group_process {
+    my $self = shift (@_);
+    my ($doc_obj) = @_;
+
     my $OID = $doc_obj->get_OID();
     $OID = "NULL" unless defined $OID;
@@ -79,56 +143,18 @@
 
     # opening a new file, or document has assoicated files => directory needed
-    if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0))
-    {
-	# get the document's directory.
-	my $doc_info = $self->{'archive_info'}->get_info($OID);
-	my $doc_dir = "";
-	if (defined $doc_info && scalar(@$doc_info) >= 1) {
-	    # this OID already has an assigned directory, use the
-	    # same one.
-	    $doc_dir = $doc_info->[0];
-	    $doc_dir =~ s/\/?doc\.gml(\.gz)?$//;
-	} else {
-	    # have to get a new document directory
-	    my $doc_dir_rest = $OID;
-	    my $doc_dir_num = 0;
-	    do {
-		$doc_dir .= "/" if $doc_dir_num > 0;
-		if ($doc_dir_rest =~ s/^(.{1,8})//) {
-		    $doc_dir .= $1;
-		    $doc_dir_num++;
-		}
-	    } while ($doc_dir_rest ne "" && 
-		     ((-d &util::filename_cat ($archive_dir, "$doc_dir.dir")) || 
-		      ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
-	    $doc_dir .= ".dir";
-	    
-	}
-	
-	&util::mk_all_dir ("$archive_dir/$doc_dir");
-    
+    if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
+
+	# get document's directory
+	my $doc_dir = $self->get_doc_dir ($OID);
+
 	# copy all the associated files, add this information as metadata
 	# to the document
-	my @assoc_files = ();
-	foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
-	    my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
-	    $dir = "" unless defined $dir;
-	    if (-e $assoc_file->[0]) {
-		my $filepath = &util::filename_cat($archive_dir, $doc_dir, $afile);
-		&util::hard_link ($assoc_file->[0], $filepath);
-		$doc_obj->add_metadata ($doc_obj->get_top_section(),
-					"gsdlassocfile",
-					"$afile:$assoc_file->[2]:$dir");
-	    } else {
-		print STDERR "docsave::process couldn't copy the associated file " .
-		    "$assoc_file->[0] to $afile\n";
-	    }
-	}
-
-	if ($open_new_file)
-	{
+	$self->process_assoc_files ($doc_obj, $doc_dir);
+
+
+	if ($open_new_file) {
 	    # only if opening new file
 	    my $doc_file 
-		= &util::filename_cat ($archive_dir, $doc_dir, "doc.gml");
+		= &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.gml");
 	    my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");
 	    
@@ -139,5 +165,5 @@
 	    
 	    if (!open (OUTDOC, ">$doc_file")) {
-		print STDERR "docsave::process could not write to file $doc_file\n";
+		print STDERR "docsave::group_process could not write to file $doc_file\n";
 		return;
 	    }
@@ -153,4 +179,61 @@
     $self->{'gs_count'}++;
 }
+
+
+sub get_doc_dir {
+    my $self = shift (@_);
+    my ($OID) = @_;
+
+    my $doc_info = $self->{'archive_info'}->get_info($OID);
+    my $doc_dir = "";
+    if (defined $doc_info && scalar(@$doc_info) >= 1) {
+	# this OID already has an assigned directory, use the
+	# same one.
+	$doc_dir = $doc_info->[0];
+	$doc_dir =~ s/\/?doc\.gml(\.gz)?$//;
+    } else {
+	# have to get a new document directory
+	my $doc_dir_rest = $OID;
+	my $doc_dir_num = 0;
+	do {
+	    $doc_dir .= "/" if $doc_dir_num > 0;
+	    if ($doc_dir_rest =~ s/^(.{1,8})//) {
+		$doc_dir .= $1;
+		$doc_dir_num++;
+	    }
+	} while ($doc_dir_rest ne "" && 
+		 ((-d &util::filename_cat ($self->{'archive_dir'}, "$doc_dir.dir")) || 
+		  ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
+	$doc_dir .= ".dir";
+	
+    }
+	
+    &util::mk_all_dir (&util::filename_cat ($self->{'archive_dir'}, $doc_dir));
+
+    return $doc_dir;
+}
+
+
+sub process_assoc_files {
+    my $self = shift (@_);
+    my ($doc_obj, $doc_dir) = @_;
+
+    my @assoc_files = ();
+    foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
+	my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
+	$dir = "" unless defined $dir;
+	if (-e $assoc_file->[0]) {
+	    my $filepath = &util::filename_cat($self->{'archive_dir'}, $doc_dir, $afile);
+	    &util::hard_link ($assoc_file->[0], $filepath);
+	    $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
+					 "gsdlassocfile",
+					 "$afile:$assoc_file->[2]:$dir");
+	} else {
+	    print STDERR "docsave::process couldn't copy the associated file " .
+		"$assoc_file->[0] to $afile\n";
+	}
+    }
+}
+    
 
 sub close_file_output
