/*   ---------------------------------      -------     
 *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
 *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
 *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
 *   ---------------------------------  ~~~~~~~~~~~~~~~~
 *  TimeCom - Time a command.
 *  Copyright (C) 1992, 1993 Torsten Poulin
 *
 *  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 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author can be contacted by s-mail at
 *    Torsten Poulin
 *    Banebrinken 99, 2, 77
 *    DK-2400 Copenhagen NV
 *    DENMARK
 *
 * Created 16-Jan-92
 * $Id: TimeCom.c,v 37.2 93/02/13 21:42:07 Torsten Rel $
 * $Log:	TimeCom.c,v $
 * Revision 37.2  93/02/13  21:42:07  Torsten
 * Changed the version tag and added the copyright ditto.
 * 
 */

#include <exec/types.h>
#include <libraries/dos.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include "timecom_rev.h"

#define OPT_TO      0
#define OPT_APPEND  1
#define OPT_COMMAND 2


static LONG MySystemTags(struct DosLibrary *DOSBase,
                         UBYTE *command, ULONG firsttag, ...);

char const versionID[] = VERSTAG;
char const copyright[] = "$COPYRIGHT:© 1992,1993 Torsten Poulin$";

LONG entrypoint(VOID)
{
  struct RDArgs        *args;

  struct IntuitionBase *IntuitionBase;
  struct DosLibrary    *DOSBase;
  struct Library       *SysBase;

  BPTR  out;
  ULONG secs1 = 0, secs2 = 0, not_used = 0;
  LONG  arg[3];
  LONG  rc = RETURN_OK;


  SysBase = *(struct Library **) 4L;
  if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  {
    rc = RETURN_FAIL;
    goto exit1;
  }
  if (!(IntuitionBase = (struct IntuitionBase *)
	OpenLibrary("intuition.library", 33L)))
  {
    rc = RETURN_FAIL;
    goto exit2;
  }

  arg[OPT_TO] = arg[OPT_APPEND] = arg[OPT_COMMAND] = 0L;
    
  if (args = ReadArgs("TO/K,APPEND/S,COMMAND/F/A", arg, NULL))
  {
    CurrentTime(&secs1, &not_used);
    rc = MySystemTags(DOSBase, (UBYTE *) arg[OPT_COMMAND],
		      SYS_UserShell, TRUE,
		      TAG_DONE);
    CurrentTime(&secs2, &not_used);
    if (rc == -1)
      rc = RETURN_ERROR;
    secs2 -= secs1;

    if (out = Open(arg[OPT_TO] ? (UBYTE *) arg[OPT_TO] : "CONSOLE:",
		   (BOOL) arg[OPT_APPEND] ? MODE_READWRITE : MODE_NEWFILE))
    {
      if ((BOOL) arg[OPT_APPEND] && arg[OPT_TO])
	Seek(out, 0L, OFFSET_END);
      VFPrintf(out, "Elapsed time: %ld second", (LONG *) &secs2);
      FPuts(out, secs2 == 1 ? "." : "s.");
      if (arg[OPT_TO])
	VFPrintf(out, " Command: %s", (LONG *) &arg[OPT_COMMAND]);
      FPutC(out, '\n');
      Close(out);
    }
    else
    {
      LONG err = IoErr();
      PrintFault(err, "TimeCom");
      rc = RETURN_ERROR;
    }

    FreeArgs(args);
  }
  else
  {
    LONG err = IoErr();
    PrintFault(err, "TimeCom");
    rc = RETURN_ERROR;
  }

  CloseLibrary((struct Library *) IntuitionBase);
 exit2:
  CloseLibrary((struct Library *) DOSBase);
 exit1:
  return rc;
}


static LONG MySystemTags(struct DosLibrary *DOSBase,
                         UBYTE *command, ULONG firsttag, ...)
{
  return SystemTagList(command, (struct TagItem *) &firsttag);
}
