/*

                                   _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

*/

/* midnightcode-license.c :: Midnight Code license library		*/


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

#include "preferences.h"
#include "midnightcode-colour.h"
#include "midnightcode-license.h"



char midnightcode_license_text_copyright[2][80] = {
  "                          Copyright (C) 2004-2007",
  "       \"Ian (Larry) Latter\" <ian dot latter at midnightcode dot org>"
};

char midnightcode_license_text_trademark[1][80] = {
  "    Midnight Code is a registered trademark of Ian Latter."
};

char midnightcode_license_text_freesoft[4][80] = {
  "    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."
};
  
char midnightcode_license_text_warranty[4][80] = {
  "    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."
};
  
char midnightcode_license_text_license[4][80] = {
  "    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"
};

long double midnightcode_license_permid;
long double midnightcode_license_tempid;



int
midnightcode_license_buffer(char ** buffer, int padding, int style,
                     int sections) {
  int	element;
  char * buffer_p;
  char  pad[81];
  int buffer_size;
  int buffer_offset;


  buffer_size = 4096;
  buffer_offset = 0;
  buffer_p = malloc(buffer_size);
  if(buffer_p == NULL)
    return -1;
  memset(buffer_p, 0, buffer_size);

  memset(pad, 0, 81);
  if(padding == 0 || padding > 50) {
    padding = 4;
  }
  memset(pad, ' ', padding);
  *buffer = buffer_p;

  if(sections < MIDNIGHTCODE_LICENSE_TEXT_NONE ||
     sections > MIDNIGHTCODE_LICENSE_TEXT_ALL) {
    sections = MIDNIGHTCODE_LICENSE_TEXT_ALL;
  }

  if(style != MIDNIGHTCODE_COLOUR_STYLE_ASCII) {
    switch(style) {
      default:
        snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
          "%s", MIDNIGHTCODE_COLOUR_DK_GREY);
        buffer_offset = strlen(buffer_p);
        break;
    }
  }

  if(sections & MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT) {
    for(element=0; element<2; element++) {
      snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
        "%s\n", midnightcode_license_text_copyright[element]);
      buffer_offset = strlen(buffer_p);
    }
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset), "\n");
    buffer_offset = strlen(buffer_p);
  }

  if(sections & MIDNIGHTCODE_LICENSE_TEXT_TRADEMARK) {
    for(element=0; element<1; element++) {
      snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
        "%s\n", midnightcode_license_text_trademark[element]);
      buffer_offset = strlen(buffer_p);
    }
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset), "\n");
    buffer_offset = strlen(buffer_p);
  }

  if(sections & MIDNIGHTCODE_LICENSE_TEXT_GPLV2) {
    for(element=0; element<4; element++) {
      snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
        "%s\n", midnightcode_license_text_freesoft[element]);
      buffer_offset = strlen(buffer_p);
    }
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset), "\n");
    buffer_offset = strlen(buffer_p);
  }

  if(sections & MIDNIGHTCODE_LICENSE_TEXT_WARRANTY) {
    for(element=0; element<4; element++) {
      snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
        "%s\n", midnightcode_license_text_warranty[element]);
      buffer_offset = strlen(buffer_p);
    }
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset), "\n");
    buffer_offset = strlen(buffer_p);
  }

  if(sections & MIDNIGHTCODE_LICENSE_TEXT_GETGPL) {
    for(element=0; element<4; element++) {
      snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
        "%s\n", midnightcode_license_text_license[element]);
      buffer_offset = strlen(buffer_p);
    }
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset), "\n");
    buffer_offset = strlen(buffer_p);
  }

  if(style != MIDNIGHTCODE_COLOUR_STYLE_ASCII) {
    snprintf((buffer_p + buffer_offset), (buffer_size - buffer_offset),
      "%s ", MIDNIGHTCODE_COLOUR_NORMAL);
    buffer_offset = strlen(buffer_p);
  }

  buffer_p = realloc(buffer_p, buffer_offset);
  if(buffer_p == NULL) {
    *buffer = NULL;
    return -1;
  }
  *buffer = buffer_p;


  return buffer_offset;
}



void
midnightcode_license(int padding, int style, int sections) {
  char *buffer;
  int buffer_size;


  buffer_size = midnightcode_license_buffer(&buffer, padding, style,
    sections);
  if(buffer == NULL)
    return;
  if(buffer_size <= 0) {
    if(buffer)
      free(buffer);
    return;
  }

  fprintf(stdout, "%s", buffer);
  fflush(stdout);
  free(buffer);


  return;
}



int
midnightcode_license_get_nodeid(char * id, int options,
                     struct in_addr * ipv4_address) {
  int id_size; 


  /* TODO: options - allow upper/lower case */

  id_size = MIDNIGHTCODE_LICENSE_NODEID_SIZE;
  switch(options) {

    case MIDNIGHTCODE_LICENSE_NODEID_SHORT:
      snprintf(id, id_size, "%.4X", ntohl(ipv4_address->s_addr) & 0xffff);
      break;

    case MIDNIGHTCODE_LICENSE_NODEID_FULL:
      snprintf(id, id_size, "%.8X", ntohl(ipv4_address->s_addr));
      break;

    default:
      snprintf(id, id_size, "%.8X", ntohl(ipv4_address->s_addr));
      break;

  }


  return 0;
}



int
midnightcode_license_get_permid(void) {
  long double id;


  /* TODO: sum ethernet addresses from each interface */ 
  id = 0;

  return id;
}

