#!/usr/bin/perl

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

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

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

    printf FILE_OUT;
    }

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

  close FILE_IN;
  close FILE_OUT;
  }
