/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2007
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    Midnight Code is a registered trademark of Ian Latter.

    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, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    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
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* htmlnx.c :: Midnight Code HTML test and example program		*/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>

#include <libmidnightcode.h>


int
main(int argc, char **argv) {
  int ret_i;
  char original_url[256] = "\0";
  char absolute_url[256] = "\0";
  char relative_url[256] = "\0";
  char protocol_url[256] = "\0";
  int help = 0;

  // Prepare switches
  struct midnightcode_switch_t switches[] = {
    { "ou", "orig_url", "Original URL",
      MIDNIGHTCODE_PARSE_TYPE_STRING, 256, 0, &original_url },
    { "h", "help", "Display this help/usage screen",
      MIDNIGHTCODE_PARSE_TYPE_NONE, 0, 0, &help },
    { 0 }
  };

  // Process switches
  if((ret_i = midnightcode_switch_process(argc, argv, switches)) != 0) {
    fprintf(stdout, "\n");
    midnightcode_logo(0,
      MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
    midnightcode_license(0,
      MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    fprintf(stdout, "    Error in switches, switch \"%s\" is unkown"
            " at parameter number %d.\n\n", argv[ret_i], ret_i);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT,
                              argv[0], switches);
    exit(1);
  }
  if(help) {
    midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
  midnightcode_license(0,
    MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
    MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT,
                              argv[0], switches);
    exit(1);
  }

  fflush(stdout);

  // Process license and logo
  midnightcode_logo(0,
    MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);
  midnightcode_license(0,
    MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
    MIDNIGHTCODE_LICENSE_TEXT_ALL);


  // Program title
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_H1,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "HTML File Parser: %s", argv[0]);

  fprintf(stdout, "\n");
  fflush(stdout);

  // Checking URL Protocol
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Validating URL protocol");
  if(!strlen(original_url) || 
     midnightcode_html_urlproto(protocol_url, original_url,
       strlen(original_url)) < 1) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }

  // Checking URL Base
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Validating URL base");
  if(!strlen(original_url) || 
     midnightcode_html_urlbase(absolute_url, original_url,
       strlen(original_url), MIDNIGHTCODE_HTML_URL_TYPE_ABSOLUTE) < 1 ||
     midnightcode_html_urlbase(relative_url, original_url,
       strlen(original_url), MIDNIGHTCODE_HTML_URL_TYPE_RELATIVE) < 1) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    exit(1);
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Absolute URL protocol is [%s]", protocol_url);
  midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Absolute URL base is [%s]", absolute_url);
  midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);

  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
                           MIDNIGHTCODE_STATUS_STYLE_NORMAL,
                           "Relative URL base is [%s]", relative_url);
  midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);

  exit(0);
}

