#!/usr/bin/perl

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

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

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

    s/element="1"/element="H"/;
    s/element="2"/element="He"/;
    s/element="3"/element="Li"/;
    s/element="4"/element="Be"/;
    s/element="5"/element="B"/;
    s/element="6"/element="C"/;
    s/element="7"/element="N"/;
    s/element="8"/element="O"/;
    s/element="9"/element="F"/;
    s/element="10"/element="Ne"/;
    s/element="11"/element="Na"/;
    s/element="12"/element="Mg"/;

    printf FILE_OUT;
    }

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

  close FILE_IN;
  close FILE_OUT;
  }
