/*
 *****************************************************
 *
 *  SaVi by Robert Thurman (thurman@geom.umn.edu) and
 *          Patrick Worfolk (worfolk@alum.mit.edu).
 *
 *  Copyright (c) 1997 by The Geometry Center.
 *  This file is part of SaVi.  SaVi is free software;
 *  you can redistribute it and/or modify it only under
 *  the terms given in the file COPYRIGHT which you should
 *  have received along with this file.  SaVi may be
 *  obtained from:
 *  http://savi.sourceforge.net/
 *  http://www.geom.uiuc.edu/locate/SaVi
 *
 *****************************************************
 *
 * gv_init.c
 *
 * Routines for displaying satellites in geomview.
 *
 * $Id: gv_init.c,v 1.9 2004/12/30 22:48:20 lloydwood Exp $
 */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "constants.h"
#include "Satellite.h"
#include "orbit_utils.h"
#include "gv_utils.h"
#include "savi.h"
#include "sats.h"
#include "globals.h"

static char tname[] = "trans_          ";

/*
 * gv_sat_create
 *
 * Creates geomview objects for the satellite
 */
void
gv_sat_create(Satellite s, const Constellation * pconstellation)
{
  int i;

  for (i = 0; i < N_VIEW_MODULES; i++)
    if (view_modules[i].display_fn)
      view_modules[i].display_fn(s, pconstellation);
}

/*
 * gv_sat_transform
 *
 * Writes a gv transform for a satellite
 */
void
gv_trans_create(Satellite s)
{
  sprintf(tname + 6, "%-10d", s->id);
  gv_transform(tname, s->transform);
}

/*
 * gv_sat_list_create
 *
 * Creates all satellites in geomview
 */
void
gv_sat_list_create(const Constellation * pconstellation)
{
  Satellite_list sl = pconstellation->satellites;

  gv_begin();
  while (sl) {
    gv_sat_create(sl->s, pconstellation);
    sl = sl->next;
  }
  gv_end();
}

/*
 * gv_trans_list_create
 *
 * Updates transformation matrices containing positions of all satellites
 */
void
gv_trans_list_create(Satellite_list sl)
{
  gv_begin();
  while (sl) {
    gv_trans_create(sl->s);
    sl = sl->next;
  }
  gv_end();
}



/*
 * load up handle file into geomview
 *
 * Returns true if successful.
 */
unsigned int
gv_init()
{
  unsigned int buggy_geomview = FALSE;

  /* call initialize proc for gv_utils */
  gv_init_proc();

  /* set gv flag on for display */
  geomview_set_flag(TRUE);

  /* figure out if we have a buggy version of Geomview which puts
     the logo upside down! */
  {
    char *ver;
    static char badvers[] = "1.6.1pX-X11";
    static char knownlatestvers[] = "1.8.1";
    unsigned int i;

    ver = gv_version();

    /* If Geomview could be newer, tell user. */
    if (strncmp(knownlatestvers, ver, 5) > 0) {
      error_format("Geomview %s has been made available from "
	           "http://www.geomview.org/", knownlatestvers);
      error_format("now using Geomview %s.", ver);
    }

    if (0 == strcmp("1.6.1-X11", ver))
      buggy_geomview = TRUE;
    else
      for (i = 1; i < 6; i++) {
	badvers[6] = '0' + i;
	if (0 == strcmp(badvers, ver))
	  buggy_geomview = TRUE;
      }
  }

  /* load up savi base oogl file */
  if (!gv_sendfile(HANDLE_FILE_SAVI)) {
    error_format("gv_init: Can't load file %s", HANDLE_FILE_SAVI);
    return FALSE;
  }

  if (!geomview_logo) {
    return TRUE;
  }

  if (buggy_geomview) {
    geomview_logo = FALSE;
    error("Geomview with buggy display detected. Logo display disabled.");
    return FALSE;
  } else {

    /* load up geomview handles file */
    if (!gv_sendfile(HANDLE_FILE_LOGO)) {
      error_format("gv_init: Can't load file %s", HANDLE_FILE_LOGO);
      return FALSE;
    }
  }

  /* turn on logo */
  logo_on_cmd(0, NULL);

  return TRUE;
}
