/*
 *	tp.c
 *
 *	Usage: tp [taskname] [priority]
 *
 *	This program changes the priority of the specified task. If
 *	priority is not specified, then the current priority of the
 *	task is displayed. If the taskname is not specified, then the
 *      current tasks's priority is changed (or displayed). This changes
 *      task priorities, not process priorities, so it is possible to 
 *      change the priorities of system tasks. Extreme care must be
 *      taken when doing this as the default priority settings don't
 *      leave much room in them	for change. Note that if three tasks 
 *      have the same name then	then only one (and it's impossible to
 *	say which one) will be changed.
 *
 *	Program		Date		Action
 *	D. Thomas	18 Oct 1987	v1.0 Initial Coding.
 *
 */

#include "exec/tasks.h"
#include "functions.h"

#define USAGE "[%s rev. 1.0, 18 October 1987]\nUsage: %s taskname priority\n"

extern struct ExecBase *SysBase;	/* Exec's base ptr */

main (argc, argv)

int   argc;
char *argv[];

{

  register struct Task *t;	    /* task pointer */
  long int              priority = -999;   /* new task priority */
  long int              oldpriority;
  char                 *taskname = NULL;

  if (argc == 3 && 
      (sscanf (argv[2], "%ld", &priority) != 1 ||
       priority < -128 ||
       priority > 127)) {
    printf (USAGE, argv[0], argv[0]);
    printf ("'%s' is not a valid task priority.\nTask priorities are decimal integers from -128 to +127\n",
      argv[2]);
    exit (1);
  }
  else if (argc == 3)
    taskname = argv[1];
  else if (argc == 2 && sscanf (argv[1], "%ld", &priority) != 1)
    taskname = argv[1];

  if ((t = FindTask (taskname)) == 0) {
    printf ("Task '%s' not found on system.\n", taskname);
    exit (1);
  }

  if (priority >= -128 && priority < 127) {
    oldpriority = SetTaskPri (t, priority);
    printf ("Task %s%spriority changed from %ld to %ld\n",
      taskname, (taskname != NULL ? " " : ""), oldpriority, priority);
  }
  else
    printf ("Task %s%spriority is %d\n", taskname,
     (taskname != NULL ? " " : ""), t->tc_Node.ln_Pri);

  exit (0);

}
