#include <iostream.h>
#include <fstream.h>
#include <string.h>
// This program is to apply a template to a webdocument

const int MAXCHARS = 20;

void Applyfile();
void Applylist();
void Help();
bool ApplyTemp(char tfile[] ,char pfile[]);
//^^ this function returns true if completed successfully false otherwise ^^

int main()
{
  char choice = 'a';
  
  while (choice != 'q'&& choice != 'Q')
    {
      cout << "Welcome to JAMBE Webtool" << endl;
      cout << "Please choose an option: " << endl;
      
      cout << "Apply a template to a single file (A)" << endl;
      cout << "Apply a template to a list of files (L)" << endl;
      cout << "Help (H)" << endl;
      cout << "Quit (Q)" << endl;
      cin >> choice;
    
      switch(choice)
	{
	case 'a':
	case 'A':
	  Applyfile();
	  break;
	case 'L':
	case 'l':
	  Applylist();
	  break;
	case 'h':
	case 'H':
	  Help();
	  break;
	default:
	  break;
	}
  
   
   }
  
  return 0;
  
}


void Applyfile()
{ 
  char tfile[MAXCHARS];
  char pfile[MAXCHARS];

do
  {
  cout << "Please enter name of template file (or ! to quit to menu)" << endl;
  cin >> tfile;
  if(tfile[0] == '!')
    break;
  cout << "Please enter name of file to process" << endl;
  cin >> pfile;
  
  }while(!ApplyTemp(tfile,pfile));
 
}

void Applylist()
{
  char tfile[MAXCHARS];
  char pfile[MAXCHARS];
  char lfile[MAXCHARS];

  cout << "Please enter name of template file" << endl;
  cin >> tfile;
  
  cout << "Please enter name of file (which contains list of file names) to process" << endl;
  cin >> lfile;
  
  ifstream lin(lfile);
    
    if(lin.fail())
      {
	cout << "couldn't open list file (" << lfile << ") please try again " << endl;
	return;
      }
  
  else
    while(lin.peek() != EOF)
      {
	lin >> pfile;
	
	ApplyTemp(tfile,pfile);
      }
  
}


void Help()
{
  cout << "Help is not avaliable yet" << endl;




}

bool ApplyTemp(char tfile[] ,char pfile[])
{
  bool complex = false; // used for a complex <body> tag
  char ch;
  char code[(2*MAXCHARS)];

  char sfile[(MAXCHARS+8)]= "webt\\";

  ifstream tin(tfile);

  if(tin.fail())
    {
      cout << "Could not open " << tfile << endl;
      return false;
    }

    ifstream pin(pfile);

  if(pin.fail())
    {
      cout << "Could not open " << pfile << endl;
      return false;
    }

  
  strncat(sfile,pfile,MAXCHARS);
  // cout << "Output file is " << sfile << endl;
  
  ofstream pout(sfile);
  
  if(pout.fail())
    {
      cout << "Could not open " << sfile << endl;
      cout << "Please make sure you have a folder called webt in the current directory" << endl;
    }
  else
    cout << "Successfully opened " << sfile << " (Output file)";
  cout << " and " << pfile << " (Input file)" << endl;

  while(tin.peek() != EOF)//get template to right place for head of other file
    {
      tin.get(ch);
      if(ch == '<')
	{
	  //  cout << "DEBUG: found a <" << endl;
	  tin >> code;
	  // cout << "DEBUG: Next is " << code << endl;
	  if(!strcmp(code,"head>")||!strcmp(code,"HEAD>"))
	    {
	      // cout << "Found head in template" << endl;
	     break;
	    }
	  else
	    pout << ch << code;
	}
      
      else
	pout << ch;
    }


  
  while(pin.peek() != EOF)//get prosessing file to right place for head
    {
      pin >> code;
      if(!strcmp("<head>", code)||!strcmp(code,"<HEAD>"))
	{
	  //cout << "found <head> in prossesing file " << endl;
	  pout << code;
	  break;
	}
      
    }
  

  while(pin.peek() != EOF)//get info from processing file
    {
      pin.get(ch);
      if(ch == '<')
	{
	  pin >> code;
	  
	  if(!strcmp("/HEAD>", code))
	    {
	      //  cout << "found /HEAD>" << endl;
	      break;
	    }
	  
	  else if(!strcmp("/head>", code))
	    {
	      //cout << "found /head>" << endl;
	      break;
	    }
	  else
	    pout << ch << code;
	  //  cout << ch << code;
	}
      else
	pout << ch;
      // cout << ch;
    }

  //Drop template head
   
  while(tin.peek() != EOF)//get template file to right place for body
    {
      tin >> code;
      if(!strcmp("<body", code)||!strcmp("<BODY", code))
	{
	  pout << "<body " << endl;
	  // cout << "found <body in template (complex)" << endl;
	   complex = true;
	  while(tin.peek() != EOF)
	    {
	      tin.get(ch);
	      pout << ch;
	      if(ch == '>')
		{
		  code[5]= ch;
		  code[6]= '\0';
		  //  cout << "code is now |" << code << "| " << endl;
		  break;
		}
	    }
	}
      
      if(!strcmp("<body>", code)||!strcmp("<BODY>", code))
	{
	  if(!complex)
	    pout << "<body>";
	  
	  break;
	}
      
    }


  while(tin.peek() != EOF)//get template to right place for body of other file
    {
      tin.get(ch);
      if(ch == '#')
	{
	  //cout << "DEBUG: found a #" << endl;
	  tin >> code;
	  //  cout << "DEBUG: Next is " << code << endl;
	  if(!strcmp(code,"###"))
	    break;
	  else
	    pout << ch << code;
	}
      
      else
	pout << ch;
    }
  //cout << "DEBUG: found right place in " << tfile << endl;
  
  while(pin.peek() != EOF)//get prosessing file to right place
    {
      pin >> code;
      if(!strcmp("<body", code))
	{
	  // cout << "found <body " << endl;
	  while(pin.peek() != EOF)
	    {
	      pin.get(ch);
	      if(ch == '>')
		{
		  code[5]= ch;
		  code[6]= '\0';
		  //cout << "code is now |" << code << "| " << endl;
		  break;
		}
	    }
	}
      
      if(!strcmp("<body>", code))
	{
	  break;
	}
      
    }
  
   cout << "DEBUG: found right place in " << pfile << endl;

  while(pin.peek() != EOF)//get info from processing file
    {
      pin.get(ch);
      if(ch == '<')
	{
	  pin >> code;
	  //cout << "found a <" << code << endl;
	  if(!strcmp("/body", code)||!strcmp("/BODY", code))
	    {
	      cout << "found /body" << endl;
	      break;
	    }
	  
	  if(!strcmp("/body>", code)||!strcmp("/BODY>", code))
	    {
	      cout << "found /body>" << endl;
	      break;
	    }
	  else
	    pout << ch << code;
	  cout << ch << code;
	}
      else
	pout << ch;
       cout << ch;
      // cout << "break to here" << endl;
   }
  cout << "copied info from " << pfile << endl;
  
  while(tin.peek() != EOF)
    {
      tin.get(ch);
      
      pout << ch;     
      
    }
   cout << "Finished prosessing " << tfile << endl;
  
  return true;
}





