/*
    Module : WrapperCode

    Purpose: Main program entry point, loads settings, calls functions;
             that's why it is the "wrapper" code!

    Version: 0.05

    Date   : 5th June 2008

    Started: 11th July 2005

    Author : Rick Murray
             http://www.heyrick.co.uk/eurovision/

*/



// include everything necessary
#include "euroscore.h"


         FILE *in = NULL;
         FILE *out = NULL;
unsigned char workspace[1024] = "";
unsigned int  year = 2008;
unsigned int  popularity = 0;
unsigned char appvers[] = "0.05";
unsigned char appdate[] = "2008/06/05";




int main(int argc, char **argv)
{
   time_t ttbuf;
   struct tm tmbuf;


   /***********************************
    * PROCESS COMMAND LINE PARAMETERS *
    ***********************************/

   // Check that we have two (and only two) parameters
   if (argc != 3)
   {
      fprintf(stderr, "Invalid number of parameters.\nSyntax: euroscore <year> <CSV file>\n\n");
      exit(EXIT_FAILURE);
   }


   /*********************
    * LET'S ALL REGGAE! *  (I know it didn't say that, but it sounded like it did!)
    *********************/

   // Get year
   year = atoi( argv[1] );
   if ( (year < 1956) || (year > 2056) )
   {
      fprintf(stderr, "Invalid year - should be 1956 to 2056, was %d.\n\n");
      exit(EXIT_FAILURE);
   }

   // Verify CSV file is not a number
   if ( atoi( argv[2] ) != 0 )
   {
      fprintf(stderr, "CSV filename (\"%s\") equated to a number, is this correct?\n");
      // don't exit, user might use a filename like "2005.csv" which satisfies this test!
   }

   // MemTrace initialise calls atexit(), and since this is last-in-first-out, we should be
   // certain that MemTrace's call is the FIRST one registered (thus, the last one called)
   memtrace_init();


   /*****************
    * LOAD CSV FILE *
    *****************/

   csvloader_loadfile( argv[2] );


   /******************************
    * WORK OUT THE SCORING ORDER *
    ******************************/

   country_process();


   /**********************
    * OUTPUT HTML HEADER *
    **********************/
    
   sprintf(workspace, "scorecard%04d.html", year);
   out = fopen(workspace, "wb");
   if (out == NULL)
   {
      fprintf(stderr, "Unable to open output file \"%s\".\n", workspace);
      exit(EXIT_FAILURE);
   }


   /**********************
    * OUTPUT HTML HEADER *
    **********************/

   fprintf(out, "<!doctype html public \"-//W3C//DTD HTML 3.2//EN\">\r\n\r\n");
   fprintf(out, "<html>\r\n<head>\r\n<title>The Eurovision Song Contest %d", year);
   fprintf(out, " - Scorecard</title>\r\n\r\n");
   fprintf(out, "<meta name=\"description\" content=\"Score analysis for the Eurovision ");
   fprintf(out, "Song Contest %d\">\r\n", year);
   fprintf(out, "<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\r\n");
   fprintf(out, "<meta http-equiv=\"content-language\" content=\"en\">\r\n");
   fprintf(out, "<meta name=\"resource-type\" content=\"document\">\r\n");
   fprintf(out, "<meta name=\"copyright\" content=\"This document copyright %d by ", year);
   fprintf(out, "Rick Murray. The images copyright EBU-UER, and taken from a live ");
   fprintf(out, "broadcast on BBC ONE/BBC THREE.\">\r\n"); // amend if NDR/ARD1
   fprintf(out, "<meta name=\"author\" content=\"Rick Murray\">\r\n");
   fprintf(out, "<meta name=\"generator\" content=\"EuroScore/%s\">\r\n", appvers);
   fprintf(out, "<meta name=\"rating\" content=\"general\">\r\n");
   fprintf(out, "<meta name=\"MSSmartTagsPreventParsing\" content=\"TRUE\">\r\n\r\n");
   fprintf(out, "</head>\r\n\r\n\r\n");
   fprintf(out, "<!--                                           -->\r\n");
   fprintf(out, "<!--  (C) Copyright %d Rick Murray           -->\r\n", year);
   fprintf(out, "<!--  Automagically created by EuroScore v%s -->\r\n", appvers);
   fprintf(out, "<!--                                           -->\r\n\r\n\r\n");
   fprintf(out, "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0022dd\" vlink=\"#002288\">\r\n\r\n");

   fprintf(out, "<h1 align=\"center\"><font color=\"purple\">The Eurovision Song Contest ");
   fprintf(out, "%d <br>Scorecard</font></h1>\r\n<p>\r\n\r\n", year);


   /************************
    * OUTPUT EACH COUNTRY! *
    ************************/

   country_outputeach(); // that was easy, wasn't it? <smirk>


   /********************
    * OUTPUT HTML TAIL *
    ********************/

   ttbuf = time( NULL );
   _localtime( &ttbuf, &tmbuf );
   
   fprintf(out, "<hr size = 3>\r\n");
   fprintf(out, "<p>\r\n\r\n");
   fprintf(out, "<a href=\"index.html\">Back to the Eurovision %d index</a><br>\r\n", year);
   fprintf(out, "<a href=\"../index.html\">Back to the Eurovision main index</a>\r\n");
   fprintf(out, "<p>\r\n\r\n");
   fprintf(out, "<hr size = 3>\r\n\r\n");
   fprintf(out, "This document was automatically generated by\r\n");
   // you must NOT alter the reference URL, unless I have granted specific permission, you
   // should link back to "http://www.heyrick.co.uk/eurovision/euroscore.html".
   fprintf(out, "<a href=\"http://www.heyrick.co.uk/eurovision/euroscore.html\">EuroScore</a>\r\n");
   fprintf(out, "v%s at %02d:%02d on %04d/%02d/%02d (CET).\r\n<p>\r\n\r\n", appvers,
                tmbuf.tm_hour,tmbuf.tm_min,(tmbuf.tm_year+1900),(tmbuf.tm_mon+1),tmbuf.tm_mday);
   // you may, optionally, omit the next four lines (until "end of omittable section")
   fprintf(out, "If you have any questions or comments on this software, please email me:<br>\r\n");
   fprintf(out, "&nbsp;&nbsp;<tt><b><font color=\"#0F004F\">heyrick</font></b></tt>\r\n");
   fprintf(out, "<i>-&#%03d;&#%03d;-</i> <tt><b><font color=\"#0F004F\">", 'a', 't');
   fprintf(out, "merseymail</font></b></tt>\r\n<i>-&#%03d;&#%03d;&#%03d;-</i> ", 'd', 'o', 't');
   fprintf(out, "<tt><b><font color=\"#0F004F\">com</font></b></tt>\r\n");
   // "end of omittable section" :-)
   fprintf(out, "<p>\r\n\r\n<hr size = 3>\r\n");
   fprintf(out, "<address>Copyright &copy; %04d Rick Murray<br>\r\n", (tmbuf.tm_year+1900));
   fprintf(out, "Images copyright &copy; %d EBU-UER<br>\r\n", year);
   fprintf(out, "Broadcast in beautiful widescreen by the BBC (on BBC ONE and BBC THREE)</address>\r\n");
   fprintf(out, "</body></html>\r\n"); // amend the above if NDR/ARD1

   fclose(out);
   out = NULL;


   /*************
    * ALL DONE! *
    *************/

   return 0;
}