/*

                                   _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

*/

/* init.c :: Midnight Code library based system initialiser		*/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <linux/wait.h>
#include <sys/wait.h>
#include <sys/reboot.h>
#include <sys/mount.h>
#include <signal.h>
#include <errno.h>
#include <syslog.h>

#include <libmidnightcode.h>
#include "init.h"



struct midnightcode_socket_head_t * sockets;
struct midnightcode_interface_head_t * interface_list_head_p;
struct midnightcode_route_head_t * route_list_head_p;

struct midnightcode_interface_element_t * primary_interface;


int
init_bootp(void) {


  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL,
    "Retrieving all host interfaces");
  if(midnightcode_interface_list(interface_list_head_p) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    return -1;
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }


  primary_interface = NULL;
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL,
    "Retrieving only eth0");
  if(midnightcode_interface_byname(interface_list_head_p,
                               "eth0", &primary_interface) != 1) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    return -1;
  } else {
    if(!primary_interface) {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
      return -1;
    } else {
      midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
    }
  }


  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL,
    "Initialise socket library");
  if(midnightcode_socket_initialise(sockets) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    return -1;
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }


  primary_interface->is_static = 0;
  midnightcode_status_text(MIDNIGHTCODE_STATUS_TYPE_TEXT,
    MIDNIGHTCODE_STATUS_STYLE_NORMAL,
    "Configure eth0");
  if(midnightcode_interface_configure(primary_interface) < 0) {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_FAIL);
    return -1;
  } else {
    midnightcode_status_flag(MIDNIGHTCODE_STATUS_FLAG_OKAY);
  }




  return  0;
}



int
main(int argc, char **argv) {
  void * tmp_void_p;
  int ret_i;
  int help = 0;

  struct midnightcode_switch_t switches[] = {
    { "h", "help", "Display this help/usage screen",
      MIDNIGHTCODE_PARSE_TYPE_NONE, 0, 0, &help },
    { 0 }
  };


  /* NOTE: Initialise permanent structures */
  midnightcode_list_init(socket_struct_head, midnightcode_socket_element_t);
  tmp_void_p = (void *)&socket_struct_head;
  sockets = tmp_void_p;

  midnightcode_list_init(interface_struct_head,
    midnightcode_interface_element_t);
  tmp_void_p = (void *)&interface_struct_head;
  interface_list_head_p = tmp_void_p;

  midnightcode_list_init(route_struct_head, midnightcode_route_element_t);
  tmp_void_p = (void *)&route_struct_head;
  route_list_head_p = tmp_void_p;


  midnightcode_logo(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON);

  if((ret_i = midnightcode_switch_process(argc, argv, switches)) != 0) {
    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);
    fflush(stdout);
    exit(1);
  }
  if(help) {
    midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
      MIDNIGHTCODE_LICENSE_TEXT_COPYRIGHT);
    midnightcode_switch_usage(MIDNIGHTCODE_SWITCH_STYLE_SOFT,
      argv[0], switches);
    fflush(stdout);
    exit(1);
  }

  midnightcode_license(0, MIDNIGHTCODE_COLOUR_STYLE_BLOOD_MOON, 
    MIDNIGHTCODE_LICENSE_TEXT_ALL);
  fflush(stdout);

  init_bootp();
  fflush(stdout);

  return 0;
}

