/****************************************************************************/
/*                                                                          */
/*  FILE: cmd_status.c                                                      */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute a client command to return status information.  Status      */
/*      is in the form of newline terminated ASCII text lines of the form   */
/*                                                                          */
/*      Attribute: value                                                    */
/*                                                                          */
/*      Software reading this status information shal not depend on the     */
/*      order of the lines.  They should instead scan the output.           */
/*      Also as new attributes may be added from time to time, these        */
/*      programs shall ignore un recognized Attribute lines.                */
/*                                                                          */
/*  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_status_rcsid[] = 
    "$Id: cmd_status.c,v 1.2 1998/09/18 06:27:13 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"
#include "parameters.h"

/* prototype of local function */
static void logerr(int sd, char file[], int line);


static int RtnCode;


/****************************************************************************/
/*                                                                          */
/*  cmd_status                                                              */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute the "status" command.                                       */
/*                                                                          */
/*  ARGUMENTS:                                                              */
/*      None.  All status information is always given. args[] is ignored.   */
/*                                                                          */
/****************************************************************************/
int cmd_status(int sd, char args[])
{  
    char    buffer[120];
    int     res;
    
    
    RtnCode = CMD_OK;
    
    ml_printf(ML_DEBUG, ML_INFORMATION,
        "cmd_status called\n");  
            
    res = sl_Send2Client(sd, "Global Variables:\n");
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Port:         %d\n", gbl_port);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Syslog:       %d\n", gbl_syslog);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Msg_level:    %d\n", gbl_msg_level);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Sitename:     %s \n", gbl_sitename);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Observer:     %s \n", gbl_observer);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Observatory:  %s \n", gbl_observatory);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Latitude:     %f \n", gbl_latitude);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Longitude:    %f \n", gbl_longitude);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd,  __FILE__, __LINE__);
    
    sprintf(buffer, "  Altitude:     %f \n", gbl_altitude);
    res = sl_Send2Client(sd, buffer);
    if (res < 0) logerr(sd, __FILE__, __LINE__);
    
    return RtnCode;
}


static void logerr(int sd, char file[], int line)
{ 
        ml_printf(ML_MANDATORY, ML_ERROR,
            "ERROR: Send to client failed on socket %d\n", sd);
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "       File: %s,  Line: %d\n", file, line);

    RtnCode = CMD_FAIL;
    return;
}
