//TODO: C Code. static functions are not good if we plan to have a test for them.
static bool merge_locations_into_dives(void)
{
	int i, j, tracer=0, changed=0;
	struct dive *gpsfix, *nextgpsfix, *dive;

	sort_table(&gps_location_table);

	for_each_dive (i, dive) {
		if (!dive_has_gps_location(dive)) {
			for (j = tracer; (gpsfix = get_gps_location(j, &gps_location_table)) !=NULL; j++) {
				if (dive_within_time_range (dive, gpsfix->when, SAME_GROUP)) {
					/*
					 * If position is fixed during dive. This is the good one.
					 * Asign and mark position, and end gps_location loop
					 */
					if ((dive->when <= gpsfix->when && gpsfix->when <= dive->when + dive->duration.seconds)) {
						copy_gps_location(gpsfix,dive);
						changed++;
						tracer = j;
						break;
					} else {
						/*
						 * If it is not, check if there are more position fixes in SAME_GROUP range
						 */
						if ((nextgpsfix = get_gps_location(j+1,&gps_location_table)) &&
						    dive_within_time_range (dive, nextgpsfix->when, SAME_GROUP)) {
							/*
							 * If distance from gpsfix to end of dive is shorter than distance between
							 * gpsfix and nextgpsfix, gpsfix is the good one. Asign, mark and end loop.
							 * If not, simply fail and nextgpsfix will be evaluated in next iteration.
							 */
							if ((dive->when + dive->duration.seconds - gpsfix->when) < (nextgpsfix->when - gpsfix->when)) {
								copy_gps_location(gpsfix,dive);
								tracer = j;
								break;
							}
						/*
						 * If no more positions in range, the actual is the one. Asign, mark and end loop.
						 */
						} else {
							copy_gps_location(gpsfix,dive);
							changed++;
							tracer = j;
							break;
						}
					}
				} else {
					/* If position is out of SAME_GROUP range and in the future, mark position for
					 * next dive iteration and end the gps_location loop
					 */
					if (gpsfix->when >= dive->when + dive->duration.seconds + SAME_GROUP) {
						tracer = j;
						break;
					}
				}
			}
		}
	}
	return changed > 0;
}
int main(int argc, char **argv)
{
    char *opt, *path, *url;
    char copt[STR_LEN];
    int dage, i, slen;
    size_t br = STR_LEN;
    struct gps_location rloc;

    opt = (char *)malloc(STR_LEN * sizeof(char));
    url = "http://maps.google.com/maps?q=";

    if (argc != 0) {
        path = argv[1];
        dage = get_gps_location(path, &rloc);

        if (dage < 0) {
            printf("Error! Error code is %d.\n", dage);
            return 0;
        }

        printf("File name GPS Stat: %s", opt);
        printf("Latitude : %f\n", rloc.latitude);
        printf("Longitude : %f\n", rloc.longitude);
        printf("Data Accuracy: %f\n", rloc.accuracy);
        printf("Google Map Location: %s%f,%f\n",
               url, rloc.latitude, rloc.longitude);
        printf("Data age: %d\n", dage);

        return 0;
    }

    while (strncmp(copt, "exit", 4) != 0) {
        printf("Enter path name: ");
        slen = getline(&opt, &br, stdin);

        if (slen > 0) {
            path = (char *)malloc(slen * sizeof(char));
            strncpy(path, opt, slen - 1);
            path[slen - 1] = '\0';
        }

        i = 0;
        while (i < STR_LEN) {
            copt[i] = tolower(opt[i]);
            i++;
        }

        if (strncmp(copt, "exit", 4) != 0) {
            printf("Path: %s\n", path);
            rloc.latitude = 0;
            rloc.longitude = 0;
            rloc.accuracy = 0;

            dage = get_gps_location(path, &rloc);
            free(path);

            if (dage < 0 && strncmp(copt, "exit", 4) != 0) {
                printf("Error! Error code is %d.\n", dage);
                continue;
            }

            printf("File name GPS Stat: %s", opt);
            printf("Latitude : %f\n", rloc.latitude);
            printf("Longitude : %f\n", rloc.longitude);
            printf("Data Accuracy: %f\n", rloc.accuracy);
            printf("Google Map Location: %s%f,%f\n",
                   url, rloc.latitude, rloc.longitude);
            printf("Data age: %d\n", dage);
        }
    }
    free(opt);

    return 0;
}