#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}'
if 0;
#use strict;
use warnings;

my $file = $ARGV[0];
open OUT, ">clean.txt" or die;
open OUT2, ">header.txt" or die;
say OUT2 "codes = \{";
open(my $fh, '<:encoding(UTF-8)', $file)
  or die "Could not open file '$file' $!";

my $i = 1;
while (my $row = <$fh>) {
  chomp $row;
  if ($row =~ m/ELLIPSOIDS.C7/)
  {
	my @arr = split(' ',$row);
	my @ellip = split('C', $arr[0]);
        $arr[2] =~ tr/\{//d;
        $arr[-1] =~ tr/\}//d;
        my @name = @arr[2..$#arr-2];
        s/\'/\"/g for @name;
        say OUT "case lower (codes ($i, :))";
	say OUT "  Code = $ellip[1];";
        say OUT "  Name = @name;";
        say OUT "  SemimajorAxis = $arr[-2];";
        say OUT "  InverseFlattening = $arr[-1]";
        say OUT "";
        say OUT2 "         \"$ellip[1]\", @name, \" \";...";
        $i ++;
  }
}
close OUT;
close OUT2;
