/****************************************************************************/
/*                                                                          */
/*  FILE: cmd_point_at.c                                                    */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute a client command to move the telescope to a given locaton.  */
/*      This comand does NOT cause the system to track.  It only points.    */
/*      So the effect of this command is lost if not followed very shortly  */
/*      by a "track" command.                                               */
/*                                                                          */
/*  HISTORY:                                                                */   
/*      June 1998 -- Chris Albertson Started work                           */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*                  Copyright (C) 1998 Chris Albertson.                     */
/*                                                                          */
/*   This program is free software; you can redistribute it and/or          */
/*   modify it under the terms of the GNU General Public License as         */ 
/*   published by the Free Software Foundation; either version 2, or        */ 
/*   (at your option) any later version.                                    */
/****************************************************************************/


/* Used by RCS and ident */
char cmd_point_at_rcsid[] = 
    "$Id: cmd_point_at.c,v 1.1 1998/08/04 05:08:47 chris Exp $";


#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "MLog.h"
#include "server_lib.h"
#include "commands.h"
#include "tasks.h"
#include "time_funct.h"

/* Prototypes of local functions */
static int validate_args(char args[]);

/* Arguments.  Decoded by validate_args() */
static double ra_deg;
static double dec_deg;


/****************************************************************************/
/*                                                                          */
/*  cmd_point_at                                                            */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute the "point_at" command.                                     */
/*                                                                          */
/*  ARGUMENTS:                                                              */
/*      cmd_point_at like all command functions accepts one string          */
/*      argument.  Inside this are the "real" arguments.  Theses are:       */
/*        1) RA  in decimal degrees                                         */
/*        2) DEC in decimal degrees                                         */
/*                                                                          */
/****************************************************************************/
int cmd_point_at(int sd, char args[])
{
    if (CMD_FAIL == validate_args(args))
    {
        return CMD_FAIL;
    } 
    
    ml_printf(ML_DEBUG, ML_INFORMATION,
        "point_at called with valid arguments ra=%f, dec=%f\n",
        ra_deg, dec_deg);
        
    /************************************************************************/
    /************************************************************************/
    /************************************************************************/
    /*                                                                      */
    /*                  THIS IS WHERE ALL THE CODE GOES                     */
    /*                                                                      */
    /************************************************************************/
    /************************************************************************/
    /************************************************************************/
        
    return CMD_OK;
}



/****************************************************************************/
/*                                                                          */
/*  validate_args                                                           */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Validate and decode command argument string.                        */
/*                                                                          */
/*  OUTPUT:                                                                 */
/*      Placed in static globals ra_deg and dec_deg.                        */
/*                                                                          */
/****************************************************************************/      
static int validate_args(char args[])
{ 
    int nargs;
    
    /* Common string used in lots of places */
    static const char    LocMsg[] = "        %s:%d <%s>\n";
    
    

    /* convert the args[] string to values.  First check for sanity */
    if ( !args )
    {
        /* null pointer */
        ml_print (ML_MANDATORY, ML_ERROR, 
            "ERROR no arguments passed\n");
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, "(null pointer)");
        return CMD_FAIL;
    }
    if ( strlen(args) < 3 )
    {
        /* to short to be valid */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Argument list to short to be valid >%s<\n", args);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

    /* convert string and return count of arguments */
    nargs = sscanf(args, "%lf %lf", &ra_deg, &dec_deg);
  
    if (nargs != 2)
    {
        /* Some error in args string detected */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Could not parse argment list >%s<\n", args);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

    /* If we get to this point we know the arguments are  */
    /* converted with no errors.  We still need to do a   */
    /* range check                                        */

    if ( (ra_deg > 360.0) || (ra_deg < 0.0) )
    {
         /* ra_deg is out of it's valid range */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR RA is out of range >%f<\n", ra_deg);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

    if ( (dec_deg > 90.0) || (dec_deg < -90.0) )
    {
         /* dec_deg is out of it's valid range */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR DEC is out of range >%f<\n", dec_deg);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }

  
    /* Finally, we know we have good input and can proceed      */        
    return CMD_OK;
}    
