/*
*	args.c -- parses the commandline 
*
*	CopyLeft Aquila
*
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "args.h"
#include "globals.h"

int get_string_size (char* string)
{
  int i;
  i = 0;
  while (string[i] != '\0') {
    i++;
  }
  return i;
}

int parse_cmdline (int argc, char* argv[], 
		   char* srcfile, char* dstfile, char* inputfile)
{
  int error;
  int i;

  error = 0;
  
  progname = malloc (get_string_size (argv[0])+1);
  strncpy (progname, argv[0], get_string_size (argv[0]));
  progname [get_string_size (argv[0])+1] = '\0';

  /* no arguments present! */
  if (argc == 1) {
    print_usage(argv);
    return 0;
  }
  /* Args are present */
  if (argc > 1) {
    if ( strcmp (argv[1], "-version") == 0) {
      print_version ();
      exit(1);
    }
    if ( strcmp (argv[1], "-help") == 0) {
      print_usage ();
      exit(1);
    }
    /* Else assume that the arg is the input file...*/
    strncpy (srcfile, argv[1], get_string_size (argv[1]));
    srcfile[get_string_size(argv[1])] = '\0';
  }

  /* Parse the rest of the args */
  for (i = 1; i < argc; i++) {
    if ( strcmp (argv[i], "-text") == 0) {/* ok, got textfile */
      strncpy (inputfile, argv[i+1], get_string_size (argv[i+1]) );
      if (inputfile_type == 0) {
	inputfile_type = TEXT;
      } else {
	print_usage ();
	exit(0);
      }
      i++;
    } 

    if ( strcmp (argv[i], "-html") == 0) { /* got a html file */
      strncpy (inputfile, argv[i+1], get_string_size (argv[i+1]) );
      if (inputfile_type == 0) {
	inputfile_type = HTML;
      } else {
	print_usage ();
	exit(0);
      }
      i++;
    }
    if ( strcmp (argv[i], "-output") == 0) {
      strncpy (dstfile, argv[i+1], get_string_size (argv[i+1]));
      dstfile[get_string_size (argv[i+1])] = '\0';
      outputfile = 1;
      i++;
    }
    /* options */
    if (strcmp (argv[i], "-font") == 0) {
      font = malloc (get_string_size (argv[i+1])+1 );
      strncpy (font, argv[i+1], get_string_size (argv[i+1]));
      font[get_string_size (argv[i+1])+1] = '\0';
      i++;
    }
    if (strcmp (argv[i], "-fontsize") == 0) {
      fontsize = malloc (get_string_size (argv[i+1])+1 );
      strncpy (fontsize, argv[i+1], get_string_size (argv[i+1]));
      fontsize [get_string_size (argv[i+1])+1] = '\0';
      i++;
    }
    if (strcmp (argv[i], "-title") == 0) {
      title = malloc (get_string_size (argv[i+1])+1);
      strncpy (title, argv[i+1], get_string_size (argv[i+1]));
      title [ get_string_size (argv[i+1])+1] = '\0';
      i++;
    }
    if (strcmp (argv[i], "-bgcolor") == 0) {
      bgcolor = malloc (get_string_size (argv[i+1])+1);
      strncpy (bgcolor, argv[i+1], get_string_size (argv[i+1]));
      bgcolor [ get_string_size (argv [i+1])+1] = '\0';
      i++;
    }
  }
      
  return 1;
}

int print_usage (char* argv[])
{
  print_version ();

  printf ("Usage: %s  <bmpfile> -option 1 ... -option n\n\n", progname);
  printf ("bmp2html converts a bmp image file to a html\n");
  printf ("file that can be viewed in a browser\n\n");
  printf ("Following options are valid:\n");
  printf ("-text <textfile>\tTakes a textfile as input for the\n");
  printf ("\t\t\tgenerated html-image. Note: Spaces does appear in\n");
  printf ("\t\t\tthe generated image.\n");
  printf ("-html <htmlfile>\tSame as above except that html tags\n");
  printf ("\t\t\twill be interpreted. Note only one of these two\n");
  printf ("\t\t\toptions can be specified.\n");
  printf ("-output <file>\t\tSpecifies the to write the html-image to.\n");
  printf ("\t\t\tDefault is stdout.\n");
  printf ("-title \"title\"\t\tSpecifies the title of \n");
  printf ("\t\t\tgenerated html document.\n");
  printf ("-font \"font\"\t\tSpecifies the font to used.\n");
  printf ("\t\t\tIt's a good idea to use fixed width font.\n");
  printf ("-fontsize \"fontsize\"\tThe font size (interpreted as in html:\n");
  printf ("\t\t\t<FONT SIZE=\"fontsize\">)\n");
  printf ("-bgcolor \"bgcolor\"\tSets the background color. Specified\n");
  printf ("\t\t\tas the html color code f.ex. #F5F5F5 (red-green-blue)\n");
  printf ("-version\t\tPrints the current version and exits.\n");
  printf ("-help\t\t\tYou're looking at it...\n\n");
  printf ("Arguments to the specified arguments to options should as a \n");
  printf ("thumb rule always be in \"\"'s \n");
  
  
  





	
  return 1;
}

int print_version ()
{
  printf("bmp2html --  ");
  printf(version);
  printf("\n");
  
  return 1;
}
