/* This function allows you to post a virtual room on your web page that
   mirrors the room on the mud.  The program creates a file with a name
   <vnum>.html where <vnum> is the room vnum.  Exits from the room in the
   mud are created as links to the associated rooms.

   This function is under continued development.  Based on mud-specific
   improvements in later versions, I only plan to release this initial
   version to the public.  Feel free to use the "Explore" feature on 
   http://clandestine.mudnet.net to see how this feature works.  */
#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <malloc.h>
#include "merc.h"
#include "recycle.h"
 
#if !defined(macintosh)
extern  int     _filbuf         args( (FILE *) );
#endif

DECLARE_DO_FUN(do_split		);
ROOM_INDEX_DATA *find_location args (( CHAR_DATA *ch, char *arg ));

void do_htmlroom(CHAR_DATA *ch, char *argument)
{

  FILE *fp; 
  ROOM_INDEX_DATA *location;
  EXIT_DATA *pExit;
  sh_int door;
  char buf[MAX_INPUT_LENGTH]; 
  char arg1[MAX_INPUT_LENGTH]; 
  char arg2[MAX_INPUT_LENGTH]; 
  int vnum;
  int end_vnum;

#ifdef DEBUG
        Debug ("do_htmlroom");
#endif

    argument = one_argument( argument, arg1 );
    argument = one_argument( argument, arg2 );
  
     if ( arg1[0] == '\0' )
    {
	send_to_char( "Syntax:  htmlroom <starting vnum> <optional ending vnum>\n\r", ch );
	return;
    }
	vnum = atoi(arg1);

     if ( arg2[0] == '\0' )
	 end_vnum = atoi(arg1);
     else
	{
	 end_vnum = atoi(arg2);
	 if ((end_vnum < vnum) || (end_vnum - vnum > 999))
	   {
		send_to_char( "You may only write 1000 rooms at a time.\n\r", ch );
		return;
	   }
	}

    for ( ; vnum <= end_vnum ; vnum++ )
    {
	sprintf(buf,"%d",vnum);
      if ( ( location = find_location( ch, buf ) ) == NULL )
	{
	  sprintf(buf,"No location %d.\n\r",vnum);
	  send_to_char( buf, ch );
	  continue;
	}

	fclose(fpReserve);
	sprintf( buf,"/usr/home/redge/public_html/explore/%d.html",vnum);
	if ( (fp = fopen( buf, "w") ) == NULL)
	  {
	     bug( "room.html: fopen", 0 );
	     perror( buf );
	  }
	else
	  {
	  fprintf(fp, "<html>\n");
	  fprintf(fp, "<head>\n");
	  fprintf(fp, "<title>\n");
	  fprintf(fp, "%s\n", location->name );
	  fprintf(fp, "</title>\n");
	  fprintf(fp, "<BODY TEXT=""#C0C0C0"" BGCOLOR=""#000000"" LINK=""#00FFFF""\n");
	  fprintf(fp, "VLINK=""#FFFFFF"" ALINK=""#008080"">\n");
	  fprintf(fp, "<h1><center>%s</center></h1>\n", location->name );
	  fprintf(fp, "<p>%s</p>\n", location->description );
	  fprintf(fp, "<hr><center>\n");

        for (door=0;door<1;door++)
        {
	    if ((pExit = location->exit[door]) != NULL)
		{		   
	 	sprintf ( buf, "<a href=""%d.html"">[%s]</a>", 
					(int) pExit->u1.to_room->vnum,
					dir_name[door]);
		}
	    else
	 	sprintf ( buf, "[%s]", dir_name[door]);
        }
	  fprintf(fp,"%s<br>\n", buf );
	  
        for (door=3;door<6;door++)
        {
	    if ((pExit = location->exit[door]) != NULL)
		{		   
	 	sprintf ( buf, "<a href=""%d.html"">[%s]</a>", 
					(int) pExit->u1.to_room->vnum,
					dir_name[door]);
		}
	    else
	 	sprintf ( buf, "[%s]", dir_name[door]);
	    fprintf(fp,"%s | \n", buf );
        }

        for (door=1;door<3;door++)
        {
	    if ((pExit = location->exit[door]) != NULL)
		{		   
	 	sprintf ( buf, "<a href=""%d.html"">[%s]</a>", 
					(int) pExit->u1.to_room->vnum,
					dir_name[door]);
		}
	    else
	 	sprintf ( buf, "[%s]", dir_name[door]);
	    fprintf(fp,"%s<br>\n", buf );
        }

           
	  fprintf(fp, "</Center>\n");
	  fprintf(fp, "<CENTER><P><A HREF=""http://clandestine.mudnet.net"" TARGET=""_top""><Font Size=+1>\n");
	  fprintf(fp, "The Official Clandestine Homepage</A> </P></CENTER></Font>\n");
	
	  fprintf(fp, "</body>\n");
	  fprintf(fp, "</html>\n");
	  fclose( fp ); 
	  fpReserve = fopen( NULL_FILE, "r" );
	  sprintf( buf,"Room %d is on the web!\n\r",vnum);
	  send_to_char( buf, ch );
	  }
    }

}

