using SPICE

file = "gm_de431.tpc"

ids = Int[]
gms = Float64[]
re = r"BODY(?<id>[0-9]+)_GM\s+= \(\s+(?<gm>[0-9\.\+\-E]+)\s+\)"
lines = open(readlines, file)
for line in lines
    s = string(line)
    if occursin(re, s)
        m = match(re, s)
        push!(ids, parse(Int, m["id"]))
        push!(gms, parse(Float64, m["gm"]))
    end
end

open("gm.jl", "w") do f
    write(f, "# Automatically generated by $(basename(@__FILE__)), do not edit!\n\n")
    write(f, "grav_param(::Float64, ::SolarSystemBarycenter) = $(gms[findfirst(ids .== 10)])\n")
    write(f, "grav_param(::SolarSystemBarycenter) = grav_param(Float64, SolarSystemBarycenter())\n")
    for (id, gm) in zip(ids, gms)
        try
            body = replace(titlecase(bodc2n(id)), r"\s"=>"")
            body = body == "Moon" ? "Luna" : body
            write(f, "grav_param(::Type{Float64}, ::$body) = $gm\n")
            write(f, "grav_param(::$body) = grav_param(Float64, $body())\n")
        catch
            @show id
        end
    end
end
