/*****************************************************
**     _________       __                           **
**     \_   ___ \_____|__| _____  ________  ___     **
**      /    \  \/_  __ \ |/     \/  ___/_ \/   \   **
**      \     \___|  | \/ |  | |  \___ \  / ) |  \  **
**       \______  /__| |__|__|_|  /____ \__/__|  /  **
**         ____\/____ _        \/ ___ \/      \/    **
**         \______   \ |_____  __| _/___            **
**          |    |  _/ |\__  \/ __ | __ \           **
**          |    |   \ |_/ __ \  / | ___/_          **
**          |_____  /__/____  /_  /___  /           **
**               \/Antipode\/  \/    \/             **
******************************************************
******************************************************
**       Copyright 2000-2003 Crimson Blade          **
******************************************************
** Contributors: Noplex, Krowe, Emberlyna, Lanthos  **
******************************************************/

/*
 * Website: http://www.crimson-blade.org/
 * File: changelog.c
 * Name: Player account module
 * Author: John 'Noplex' Bellone (jbellone@comcast.net)
 * Terms:
 * File may be modified or redistributed as long as the author is
 * contacted via email or other means. Description may be altered
 * to fit the needs. This header should be kept intact as it is
 * except for the option to alter the description.
 * Description:
 * One line appending change(s) log.
 */
 
 /*****************************************************************
 ** PHP script (displays changes on web with number sequencing ****
 ******************************************************************
<?php
     $c = $_GET['c'];
     $log = "/home/path/to/changes.dat";
     $fp = fopen($log, "r");

     $bg = true;
      $j = ($c * 9);
     while(($line = fgets($fp)))
     {
       if($j > 0)
       {
       	 $j--;
       	 continue;
       }

       if(($i <= 9))
       {
         if($bg == true) {
          $color = "#333333";
          $bg = false;
         }
         else if($bg == false) {
          $color = "#000000";
          $bg = true;
         }
        ?>
         <tr><td valign="top" bgcolor="<?php print $color; ?>">
         <span class="text"><?php print $line; ?></span></td></tr>
        <?php
       $i++;
       }
     }
     fclose($fp);
     $fp = fopen($log, "r");
     $p=$i=0;
     ?>
      <tr><td valign="top"><div align="center">
      <a href="changes.php?c=0">1</a>
     <?php
     while(($line = fgets($fp)))
     {
        if($i == 9)
        {
          $p++;
     ?>
      <a href="changes.php?c=<?php print $p; ?>"><?php print ($p+1); ?></a>
     <?php
          $i=0;
        }
        $i++;
     }
     ?>
      </div></td></tr>
     <?php
     fclose($fp);
?>
 *****************************************************************/

#include <string.h>
#include <stdio.h>
#include <time.h>
#include "mud.h"

#define CHANGES_FILE SYSTEM_DIR "changes.dat"

void append_changes( CHAR_DATA *ch, char *str );
static char *timestamp(void);

/* stamp that time! -Nopey */
static char *timestamp(void)
{
   static char buf[MAX_STRING_LENGTH];
   struct tm *t = localtime(&current_time);

   sprintf( buf, "%04d-%02d-%02d", t->tm_year+1900, t->tm_mon+1,  t->tm_mday );
   return buf;
}
                          
/* append changes to the changes.dat file inside /system/ -Nopey */
void append_changes( CHAR_DATA *ch, char *str )
{
    FILE *fp;

    if ( IS_NPC(ch) || str[0] == '\0' )
	return;

    if ( ( fp = fopen( CHANGES_FILE, "a" ) ) == NULL )
    {
	perror( CHANGES_FILE );
	send_to_char( "Could not open the file!\n\r", ch );
    }
    else
    {
	fprintf( fp, "[ %s ] %s: %s\n", timestamp(), capitalize(ch->name), str );
	FCLOSE( fp );
    }
    return;
}

/* The changes function :o -Nopey */
void do_changes(CHAR_DATA *ch, char *argument)
{
  if(IS_NPC(ch))
  {
    send_to_char("Huh?\n\r", ch);
    return;
  }
  
  if(*argument == '\0' || !argument || !IS_IMMORTAL(ch))
  {
    set_char_color(AT_WHITE, ch);
    show_file(ch, CHANGES_FILE);
    return;
  }
  else if(IS_IMMORTAL(ch))
  { 
    append_changes(ch, argument);
    send_to_char("Changes log appended.\n\r", ch);
    return;
  }
}