/* vi: set sw=4 ts=4: */
/*
 * Mini date implementation for busybox
 *
 * by Matthew Grant <grantma@anathoth.gen.nz>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
*/

#include "internal.h"
#define BB_DECLARE_EXTERN
#define bb_need_invalid_date
#define bb_need_memory_exhausted
#include "messages.c"
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>


/* This 'date' command supports only 2 time setting formats, 
   all the GNU strftime stuff (its in libc, lets use it),
   setting time using UTC and displaying int, as well as
   an RFC 822 complient date output for shell scripting
   mail commands */

/* Input parsing code is always bulky - used heavy duty libc stuff as
   much as possible, missed out a lot of bounds checking */

/* Default input handling to save suprising some people */

struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
{
	int nr;

	nr = sscanf(t_string, "%2d%2d%2d%2d%d",
				&(tm_time->tm_mon),
				&(tm_time->tm_mday),
				&(tm_time->tm_hour),
				&(tm_time->tm_min), &(tm_time->tm_year));

	if (nr < 4 || nr > 5) {
		fatalError(invalid_date, t_string); 
	}

	/* correct for century  - minor Y2K problem here? */
	if (tm_time->tm_year >= 1900)
		tm_time->tm_year -= 1900;
	/* adjust date */
	tm_time->tm_mon -= 1;

	return (tm_time);

}


/* The new stuff for LRP */

struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
{
	struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time;

	itm_time = *tm_time;
	jtm_time = *tm_time;
	ktm_time = *tm_time;
	ltm_time = *tm_time;
	mtm_time = *tm_time;
	ntm_time = *tm_time;

	/* Parse input and assign appropriately to tm_time */

	if (sscanf(t_string, "%d:%d:%d",
			   &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) {

		*tm_time = itm_time;
		return (tm_time);

	} else if (sscanf(t_string, "%d:%d",
					  &jtm_time.tm_hour, &jtm_time.tm_min) == 2) {

		*tm_time = jtm_time;
		return (tm_time);

	} else if (sscanf(t_string, "%d.%d-%d:%d:%d",
					  &ktm_time.tm_mon,
					  &ktm_time.tm_mday,
					  &ktm_time.tm_hour,
					  &ktm_time.tm_min, &ktm_time.tm_sec) == 5) {

		ktm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
		*tm_time = ktm_time;
		return (tm_time);

	} else if (sscanf(t_string, "%d.%d-%d:%d",
					  &ltm_time.tm_mon,
					  &ltm_time.tm_mday,
					  &ltm_time.tm_hour, &ltm_time.tm_min) == 4) {

		ltm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
		*tm_time = ltm_time;
		return (tm_time);

	} else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
					  &mtm_time.tm_year,
					  &mtm_time.tm_mon,
					  &mtm_time.tm_mday,
					  &mtm_time.tm_hour,
					  &mtm_time.tm_min, &mtm_time.tm_sec) == 6) {

		mtm_time.tm_year -= 1900;	/* Adjust years */
		mtm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
		*tm_time = mtm_time;
		return (tm_time);

	} else if (sscanf(t_string, "%d.%d.%d-%d:%d",
					  &ntm_time.tm_year,
					  &ntm_time.tm_mon,
					  &ntm_time.tm_mday,
					  &ntm_time.tm_hour, &ntm_time.tm_min) == 5) {
		ntm_time.tm_year -= 1900;	/* Adjust years */
		ntm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
		*tm_time = ntm_time;
		return (tm_time);

	}

	fatalError(invalid_date, t_string); 
}


int date_main(int argc, char **argv)
{
	char *date_str = NULL;
	char *date_fmt = NULL;
	char *t_buff;
	char c;
	int set_time = 0;
	int rfc822 = 0;
	int utc = 0;
	int use_arg = 0;
	time_t tm;
	struct tm tm_time;

	/* Interpret command line args */
	while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
		switch (c) {
		case 'R':
			rfc822 = 1;
			break;
		case 's':
			set_time = 1;
			if ((date_str != NULL) || ((date_str = optarg) == NULL))
				usage(date_usage);
			break;
		case 'u':
			utc = 1;
			if (putenv("TZ=UTC0") != 0) 
				fatalError(memory_exhausted);
			break;
		case 'd':
			use_arg = 1;
			if ((date_str != NULL) || ((date_str = optarg) == NULL))
				usage(date_usage);
			break;
		default:
			usage(date_usage);
		}
	}

	if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
		date_fmt = &argv[optind][1];   /* Skip over the '+' */
	else if (date_str == NULL) {
		set_time = 1;
		date_str = argv[optind];
	} else {
		usage(date_usage);
	}

	/* Now we have parsed all the information except the date format
	   which depends on whether the clock is being set or read */

	time(&tm);
	memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
	/* Zero out fields - take her back to midnight! */
	if (date_str != NULL) {
		tm_time.tm_sec = 0;
		tm_time.tm_min = 0;
		tm_time.tm_hour = 0;
	}

	/* Process any date input to UNIX time since 1 Jan 1970 */
	if (date_str != NULL) {

		if (strchr(date_str, ':') != NULL) {
			date_conv_ftime(&tm_time, date_str);
		} else {
			date_conv_time(&tm_time, date_str);
		}

		/* Correct any day of week and day of year etc fields */
		tm = mktime(&tm_time);
		if (tm < 0)
			fatalError(invalid_date, date_str); 

		/* if setting time, set it */
		if (set_time) {
			if (stime(&tm) < 0) {
				fatalError("can't set date.\n");
			}
		}
	}

	/* Display output */

	/* Deal with format string */
	if (date_fmt == NULL) {
		date_fmt = (rfc822
					? (utc
					   ? "%a, %_d %b %Y %H:%M:%S GMT"
					   : "%a, %_d %b %Y %H:%M:%S %z")
					: "%a %b %e %H:%M:%S %Z %Y");

	} else if (*date_fmt == '\0') {
		/* Imitate what GNU 'date' does with NO format string! */
		printf("\n");
		exit(TRUE);
	}

	/* Handle special conversions */

	if (strncmp(date_fmt, "%f", 2) == 0) {
		date_fmt = "%Y.%m.%d-%H:%M:%S";
	}

	/* Print OUTPUT (after ALL that!) */
	t_buff = xmalloc(201);
	strftime(t_buff, 200, date_fmt, &tm_time);
	printf("%s\n", t_buff);

	return(TRUE);
}
