/*
 * $Id: reverse.c,v 1.2 89/11/05 11:23:58 tynor Exp $
 *----------------------------------------------------------------------------
 *	FPLAN - Flight Planner
 *	Steve Tynor
 *	tynor@prism.gatech.edu
 *
 *	This program is in the public domain. Permission to copy,
 * distribute, modify this program is hearby given as long as this header
 * remains. If you redistribute this program after modifying it, please
 * document your changes so that I do not take the blame (or credit) for
 * those changes.  If you fix bugs or add features, please send me a
 * patch so that I can keep the 'official' version up-to-date.
 *
 *	Bug reports are welcome and I'll make an attempt to fix those
 * that are reported.
 *
 *	USE AT YOUR OWN RISK! I assume no responsibility for any
 * errors in this program, its database or documentation. I will make an
 * effort to fix bugs, but if you crash and burn because, for example,
 * fuel estimates in this program were inaccurate, it's your own fault
 * for trusting somebody else's code! Remember, as PIC, it's _your_
 * responsibility to do complete preflight planning. Use this program as
 * a flight planning aid, but verify its results before using them.
 *----------------------------------------------------------------------------
 */

static char rcsid[] = "$Id: reverse.c,v 1.2 89/11/05 11:23:58 tynor Exp $";

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

extern void distance_and_heading ();

/*----------------------------------------------------------------------------*/
static void reverse_incrementals ()
{
   int i;
   int last_non_inc = num_waypoints - 1;
   double h;

   for (i = num_waypoints - 2; i >= 0; i--)
      if (waypoints[i].db->mode == WP_INCREMENTAL)
	 distance_and_heading (waypoints[i].db->latitude, 
			       waypoints[i].db->longitude,
			       waypoints[last_non_inc].db->latitude,
			       waypoints[last_non_inc].db->longitude,
			       &waypoints[i].db->u.dist_since_last_wp, &h);
      else
	 last_non_inc = i;
}

/*----------------------------------------------------------------------------*/
void print_reverse ()
{
   int i, j;

   reverse_incrementals ();

   for (i = num_waypoints - 1; i >= 0; i--) {
      for (j = 1; j <= max_nav; j++) {
	 if ((i == num_waypoints - 1) ||
	     (strcmp (waypoints[i].vor_fix[j].db->desig, 
		      waypoints[i+1].vor_fix[j].db->desig))) {
	    printf ("nav(%d) %s;\n", j+1, waypoints[i].vor_fix[j].db->desig);
	 }
      }
      if (waypoints[i].wp_kind == WP_TO)
	 printf ("\nfrom %s;\n", waypoints[i].db->desig);
      else if (waypoints[i].wp_kind == WP_FROM)
	 printf ("to %s;\n", waypoints[i].db->desig);
      else if (waypoints[i].db->mode != WP_INCREMENTAL)
	 printf ("\tvia %s;\n", waypoints[i].db->desig);
      else {
	 printf ("\t\tvia %1.0lf", waypoints[i].db->u.dist_since_last_wp);
	 if (waypoints[i].db->name)
	    printf (", \"%s\"", waypoints[i].db->name);
	 if (waypoints[i].db->city)
	    printf (", \"%s\"", waypoints[i].db->city);
	 if (waypoints[i].db->comment)
	    printf (", \"%s\"", waypoints[i].db->comment);
	 printf (";\n");
      }
   }
}


