#!/usr/bin/perl

foreach (@ARGV)
  {
  open FILE_IN, "<$_" or die "Unable to read file $_";

  s/.tmp~//;
  open FILE_OUT, ">$_.xml" or die "Unable to write file $_.xml";
  printf FILE_OUT "<gml>\n<molecule name=\"$_\">\n";

  while (<FILE_IN>)
    {
    s/0  0  0  0  0  0  0  0  0//g;
    s/0  0  0  0  0  0//g;

    s{\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*}
    {<atom element=\"\4\" x=\"\1\" y=\"\2\" z=\"\3\"/>\n}g;

    printf FILE_OUT;
    }

  printf FILE_OUT "</molecule>\n</gml>\n";

  close FILE_IN;
  close FILE_OUT;
  }
