#!/usr/bin/perl

foreach $inputfile (@ARGV) {
 print "Now splitting file $inputfile\n";
 open(IN, "<$inputfile");
 while($line = <IN>) {
  if (not ($line =~ /^<?xml/ || $line =~/^<(\/|)occurrences>/)) {
   if ($line =~ /^<!--/) {
    # Begin of a new output file
    $line =~ s/^<!--(.*)-->$/$1/;
    $line =~ s/^cic:/rdf/;
    $line =~ s/#xpointer\(1\/([^\/]*)\/([^\/]*)\)/,$1,$2/;
    $line =~ s/#xpointer\(1\/([^\/]*)\)/,$1/;
    $dir = $line;
    $dir =~ s/\/[^\/]*$//;
    close(OUT);
    system("mkdir -p $dir");
    open(OUT, ">>$line");
   } else {
    print OUT $line;
   }
  }
 }
 close(IN);
}
close(OUT);
