/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Torsten Jürgeleit
 *
 *	Name .....: printdate.c
 *	Created ..: Thursday 19-Dec-91 14:00:33
 *	Revision .: 0
 *
 *	Date        Author                 Comment
 *	=========   ====================   ====================
 *	19-Dec-91   Torsten Jürgeleit      Created this file!
 *
 ***************************************************************************
 *
 *	This is a very small subset of UDate to produce a free formatable
 *	output string of the current date and time.
 *
 *
 * Format commands:
 *
 *	\a = day
 *	\d = date (dd-mmm-yy)
 *	\t = time
 *	\q = double quotes
 *	\e = escape (0x1b = 27)
 *	\n = line feed
 *	\\ = backslash
 *
 *
 * Example:
 *
 *	PrintDate FORMAT "*tdc.b*t\q (\a \d \t)\q,13,10,0"
 *		->        dc.b    "Monday 11-Nov-91 11:11:11",13,10,0
 *
 *
 * Default format:
 *
 *	Same as AmigaDOS Date command ("\a \d \t").
 *
 *
 * Caveats:
 *
 *	Do NOT use '\' as escape character for the ARP library too. To check
 *	this use the ARP command 'set listall'.	(Default: set escape *)
 *	Otherwise the ARP command line parsing function GADS would interpret
 *	the format commands as escape sequences.
 *
 * $Revision Header ********************************************************/

	/* Includes */

#include <exec/types.h>
#include <libraries/arpbase.h>
#include <functions.h>

	/* Defines */

#define MAX_ARGUMENTS		2

#define ARGUMENT_NOLINE		0
#define ARGUMENT_FORMAT		1

#define DEFAULT_FORMAT		"\\a \\d \\t"

	/* Externals */

IMPORT struct DOSBase  *DOSBase;

	/* Globals */

struct ArpBase	*ArpBase;

BYTE *template  = "NOLINE/S,FORMAT/K",
     *xtra_help = "Usage: PrintDate [NOLINE] [FORMAT <format>]\n"
		  "\t[NOLINE] = suppress line feed\n"
		  "\t<format> = alternate output format string, "
				"default is \x22" DEFAULT_FORMAT "\x22";
	/* Main routine - no startup code */

   LONG
_main(LONG alen, BYTE *aptr)
{
   struct DateTime  date_time, *dat = &date_time;
   BYTE  str_day[LEN_DATSTRING], str_date[LEN_DATSTRING],
	 str_time[LEN_DATSTRING], *argv[MAX_ARGUMENTS];
   BYTE  c, *format;
   SHORT i;
   BOOL  error = FALSE;
   LONG  return_code = RETURN_FAIL;

   /* Open ARP library */
   if (!(ArpBase = OpenLibrary(ArpName, ArpVersion))) {
      Write(Output(), "Can't open ARP library V39+\n", 27L);
   } else {

      /* Get actual date and time into DateStamp and do range check */
      DateStamp(&dat->dat_Stamp);
      if (dat->dat_Stamp.ds_Days > 36500 || dat->dat_Stamp.ds_Minute >
			      60 * 24 || dat->dat_Stamp.ds_Tick > 50 * 60) {
	 Puts("Invalid date stamp received from AmigaDOS");
      } else {

	 /* Initialize DateTime structure */
	 dat->dat_Format  = FORMAT_DOS;
	 dat->dat_Flags   = 0;
	 dat->dat_StrDay  = str_day;
	 dat->dat_StrDate = str_date;
	 dat->dat_StrTime = str_time;

	 /* NULL terminate time string */
	 str_time[8] = '\0';

	 /* Convert DateStamp to strings in DateTime */
	 if (StamptoStr(dat)) {
	    Puts("StamptoStr() failed");
	 } else {

	    /* Clear argument array */
	    for (i = 0; i < MAX_ARGUMENTS; i++) {
	       argv[i] = NULL;
	    }

	    /* Parse command line arguments */
	    if (GADS(aptr, alen, xtra_help, &argv[0], template) < 0) {
	       Puts(argv[0]);
	    } else {

	       /* If no format string given then use default format */
	       if (!(format = argv[ARGUMENT_FORMAT])) {
		  format = DEFAULT_FORMAT;
	       }

	       /* Build date string from given format */
	       while (error == FALSE && (c = *format++) != '\0') {

		  /* Escape character ? */
		  if (c == '\\') {

		     /* Insert special date */
		     switch (c = *format++) {
			case 'a' :   /* Day */
			   Printf("%s", dat->dat_StrDay);
			   break;

			case 'd' :   /* Date */
			   Printf("%s", dat->dat_StrDate);
			   break;

			case 't' :   /* Time */
			   Printf("%s", dat->dat_StrTime);
			   break;

			case 'q' :   /* Double quotes */
			   Printf("\"");
			   break;

			case 'e' :   /* Escape */
			   Printf("\x1b");
			   break;

			case 'n' :   /* New line */
			   Printf("\n");
			   break;

			case '\\' :   /* Backslash */
			   Printf("\\");
			   break;

			default :    /* Invalid format type */
			   Printf("\nInvalid format type '\%c'\n", c);
			   error = TRUE;
			   break;
		     
		     }
		  } else {
		     Printf("%c", c);
		  }
	       }

	       /* If no error then append line feed */
	       if (error == FALSE) {
		  if (!argv[ARGUMENT_NOLINE]) {
		     Printf("\n");
		  }
	       }
	       return_code = RETURN_OK;
	    }
	 }
      }
      CloseLibrary(ArpBase);
   }

   /* MANX crt0.asm forget to close DOS library, so we have to do it */
   CloseLibrary(DOSBase);
   return(return_code);
}
