/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <stdio.h>

#ifdef VXWORKS_CFG
#define main power_
#endif

#include <strings.h>
#include "../h/cdefs.h"
#include "../h/rciTask.h"
#include "../h/rciMacros.h"
#include "../h/chase.h"
#include "../h/robotMath.h"
#include "../h/rciOptions.h"
#include "macros.h"

RCI_DESC *td;
RCI_RBT *rbt;
CHASE_CTRL *cb;
HOW *how;

char *getDefaultRobot();

main(argc, argv)
int argc;
char **argv;
{
	char *robotName = NULL;
	int simflag = NO;
	int powerOn = 1;

	argc--; argv++;
	while (argc)
	 { if (ARGUMENTS ("-sim="))
	    { char *sname;
	      if (strlen(sname=index(*argv,'=')+1) == 0)
	       { argc = -1;
		 break;
	       }
	      setSimulator(sname);
	      simflag = YES;
	    }
	   else if (ARGUMENT ("-sim"))
	    { simflag = YES;
	    }
	   else if (ARGUMENT ("on"))
	    { powerOn = 1;
	    }
	   else if (ARGUMENT ("off"))
	    { powerOn = 0;
	    }
	   else if (*argv[0] != '-' && !robotName)
	    { robotName = *argv;
	    }
	   else
	    { argc = -1;
	      break;
	    }
	   argc--; argv++;
	 }
	if (argc < 0)
	 { usageMsg (-1, 
"power [<robotName>] [-sim[=<simulatorAddr>]] (on|off)\n");
	 }
	if (!robotName)
	 { robotName = getDefaultRobot();
	 }	

	td = (RCI_DESC*)createChaseTask(robotName, simflag);
	cb = (CHASE_CTRL*)chaseTaskMemory(td);

	rbt = TASKROBOT(td, 0);
	how = rbt->how;

	SET_CHECKING (rbt, CHK_MAXVEL | CHK_MAXTOR | CHK_REQTOR | CHK_REQVEL);

	if (startChaseTask(td) < 0)
	 { RCI_EXIT (-1);
	 }
	if (powerOn)
	 { SET_POWER_ON_RELEASE (rbt, 1);
	   powerUpChaseTask (td, YES);
	 }
	else
	 { SET_POWER_ON_RELEASE (rbt, 0);
	 }
	rciBlock (td);

	rciClose();
	exit (0);
}
