
#include <stdio.h>
#include "SpiceUsr.h"

int main(int argc, char **argv)
{
    if (argc < 4)
    {
        fprintf(stderr, "usage: plot <bsp file> <name> <SPK ID>\n");
        fprintf(stderr, "to get SPK ID do ./cspice/exe/commnt -r name.bsp | grep 'SPK ID'\n");
        return 1;
    }

    SpiceDouble tdb;
    SpiceDouble pos[3];
    SpiceDouble light_time;

    furnsh_c("metakernel");
    furnsh_c(argv[1]);

    printf("objName = \"%s\";\n", argv[2]);
    printf("startYear = 2010;\n");

    str2et_c("2010-JAN-1", &tdb);
    int day;
    SpiceDouble min, max;
    int min_set = 0, max_set = 0;
    for (day = 0; day < 365*20; day++)
    {
        spkpos_c(argv[3], tdb, "J2000", "NONE", "399", pos, &light_time);
    
        if (min_set == 0 || pos[0] < min)
        {
            min_set = 1;
            min = pos[0];
        }
        if (min_set == 0 || pos[1] < min)
        {
            min_set = 1;
            min = pos[1];
        }
        if (min_set == 0 || pos[2] < min)
        {
            min_set = 1;
            min = pos[2];
        }
        if (max_set == 0 || pos[0] > max)
        {
            max_set = 1;
            max = pos[0];
        }
        if (max_set == 0 || pos[1] > max)
        {
            max_set = 1;
            max = pos[1];
        }
        if (max_set == 0 || pos[2] > max)
        {
            max_set = 1;
            max = pos[2];
        }

        tdb += 60*60*24;
    }

    printf("range = %f;\n", max - min);
    printf("data = [\n");

    str2et_c("2010-JAN-1", &tdb);
    for (day = 0; day < 365*20; day++)
    {
        spkpos_c(argv[3], tdb, "J2000", "NONE", "399", pos, &light_time);

        char str[101];
        etcal_c(tdb, 100, str);
        printf("[%f, %f, %f],\n", pos[0], pos[1], pos[2]);
        tdb += 60*60*24;
    }

    printf("];\n");

    return 0;
}

