/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: itahosts.ctr
*/

/*
Copyright (C) 2012-2013, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file itahosts.c The itahosts module.
*/


#line 10 "itahosts.ctr"

#include "dk3all.h"
#include "itadmin.h"





#line 17 "itahosts.ctr"



/**	Keywords used by the module.
*/
char const * const itadmin_hosts_c8_kw[] = {
/* 0 */
"hosts",

/* 1 */
"w",

/* 2 */
"hosts.ldif",

/* 3 */
"dn: cn=",

/* 4 */
"+ipHostNumber=",

/* 5 */
",ou=Hosts,",

/* 6 */
"objectClass: ipHost",

/* 7 */
"objectClass: device",

/* 8 */
"objectClass: top",

/* 9 */
"cn: ",

/* 10 */
"ipHostNumber: ",

/* 11 */
"127.0.0.1\tlocalhost\tloopback",

/* 12 */
"dn: cn=localhost+ipHostNumber=127.0.0.1",

/* 13 */
"cn: localhost",

/* 14 */
"cn: loopback",

/* 15 */
"ipHostNumber: 127.0.0.1",

NULL


#line 89 "itahosts.ctr"
};


int
itadmin_hosts_output(itadmin_job *job)
{
  FILE			*fipo;		/* Output file. */
  itadmin_network	*cnw;		/* Current network. */
  itadmin_network	*pnw;		/* Previous network. */
  itadmin_host		*host;		/* Current host to process. */
  char const		*al;		/* Current alias to process. */
  int			 dnw;		/* Flag: Different network. */
  int			 any	= 0;	/* Flag: Any host found. */
  int			 back	= 0;
  

#line 104 "itahosts.ctr"
  fipo = dk3sf_c8_fopen_app(
    itadmin_hosts_c8_kw[0], itadmin_hosts_c8_kw[1], job->app
  );
  if(fipo) {
    if(!(job->hlh)) {
      fputs(itadmin_hosts_c8_kw[11], fipo);
      fputc('\n', fipo);
      fputc('\n', fipo);
    }
    dk3sto_it_reset(job->i_hoi);
    pnw = cnw = NULL;
    while(NULL != (host = (itadmin_host *)dk3sto_it_next(job->i_hoi))) {
      if((host->ipv4) && (host->sn)) {
        dnw = 0;
	cnw = (itadmin_network *)dk3sto_it_find_like(
	  job->i_nw, (void *)(&(host->ipv4)), 1
	);
	if(pnw) {
	  if(cnw) {
	    if(cnw->ip4 != pnw->ip4) {
	      dnw = 1;
	    }
	  } else {
	    dnw = 1;
	  }
	} else {
	  if(cnw) {
	    dnw = 1;
	  }
	}
	pnw = cnw;
	if(dnw) { fputc('\n', fipo); }
        itadmin_tool_write_ip(fipo, host->ipv4);
	fputc('\t', fipo);
	fputs(host->sn, fipo);
	if((host->fnf) && (host->dnsd)) {
	  fputc('.', fipo);
	  fputs(host->dnsd, fipo);
	  fputc(' ', fipo);
	  fputs(host->sn, fipo);
	}
	if(host->i_al) {
	  dk3sto_it_reset(host->i_al);
	  while(NULL != (al = (char const *)dk3sto_it_next(host->i_al))) {
	    fputc(' ', fipo);
	    fputs(al, fipo);
	  }
	}
	fputc('\n', fipo);
	any = 1;
      }
    }
    if(any) {
      back = 1;
    }
#if DK3_CHAR_SIZE == 1
    if(!dk3sf_fclose_fn_app(fipo, itadmin_hosts_c8_kw[0], job->app)) {
      back = 0;
    }
#else
    if(!dk3sf_fclose_fn_app(fipo, NULL, job->app)) {
      back = 0;
    }
#endif
  } else {	

#line 169 "itahosts.ctr"
  } 

#line 170 "itahosts.ctr"
  return back;
}



int
itadmin_hosts_ldif_output(itadmin_job *job)
{
  char			 bu[ITADMIN_CONFIG_LINE_SIZE];
  itadmin_host		*host;
  FILE			*fipo;
  char const		*al;
  int			 ie;
  int			 any	= 0;
  int			 back	= 0;
  

#line 186 "itahosts.ctr"
  ie = dk3app_get_encoding(job->app);
  if(dk3str_to_c8u_app(bu,sizeof(bu),job->ldapb,ie,job->app)) {
    fipo = dk3sf_c8_fopen_app(
      itadmin_hosts_c8_kw[2], itadmin_hosts_c8_kw[1], job->app
    );
    if(fipo) {
      if(!(job->hlh)) {
        fputs(itadmin_hosts_c8_kw[12], fipo);
	fputs(itadmin_hosts_c8_kw[5], fipo);
	fputs(bu, fipo);
	fputc('\n', fipo);
	/* objectClass: ipHost */
	fputs(itadmin_hosts_c8_kw[6], fipo);
	fputc('\n', fipo);
	/* objectClass: device */
	fputs(itadmin_hosts_c8_kw[7], fipo);
	fputc('\n', fipo);
	/* objectClass: top */
	fputs(itadmin_hosts_c8_kw[8], fipo);
	fputc('\n', fipo);
	/* cn: localhost */
	fputs(itadmin_hosts_c8_kw[13], fipo);
	fputc('\n', fipo);
	/* cn: loopback */
	fputs(itadmin_hosts_c8_kw[14], fipo);
	fputc('\n', fipo);
	/* ipHostNumber: 127.0.0.1 */
	fputs(itadmin_hosts_c8_kw[15], fipo);
	fputc('\n', fipo);
	fputc('\n', fipo);
      }
      dk3sto_it_reset(job->i_hoi);
      while(NULL != (host = (itadmin_host *)dk3sto_it_next(job->i_hoi))) {
        if((host->ipv4) && (host->sn)) {
	  any = 1;
	  /* DN */
	  fputs(itadmin_hosts_c8_kw[3], fipo);
	  fputs(host->sn, fipo);
	  if((host->fnf) && (host->dnsd)) {
	    fputc('.', fipo);
	    fputs(host->dnsd, fipo);
	  }
	  fputs(itadmin_hosts_c8_kw[4], fipo);
	  itadmin_tool_write_ip(fipo, host->ipv4);
	  fputs(itadmin_hosts_c8_kw[5], fipo);
	  fputs(bu, fipo);
	  fputc('\n', fipo);
	  /* objectClass: ipHost */
	  fputs(itadmin_hosts_c8_kw[6], fipo);
	  fputc('\n', fipo);
	  /* objectClass: device */
	  fputs(itadmin_hosts_c8_kw[7], fipo);
	  fputc('\n', fipo);
	  /* objectClass: top */
	  fputs(itadmin_hosts_c8_kw[8], fipo);
	  fputc('\n', fipo);
	  /* cn: */
	  if((host->dnsd) && (host->fnf)) {
	    fputs(itadmin_hosts_c8_kw[9], fipo);
	    fputs(host->sn, fipo);
	    fputc('.', fipo);
	    fputs(host->dnsd, fipo);
	    fputc('\n', fipo);
	  }
	  fputs(itadmin_hosts_c8_kw[9], fipo);
	  fputs(host->sn, fipo);
	  fputc('\n', fipo);
	  if(host->i_al) {
	    dk3sto_it_reset(host->i_al);
	    while(NULL != (al = (char const *)dk3sto_it_next(host->i_al))) {
	      fputs(itadmin_hosts_c8_kw[9], fipo);
	      fputs(al, fipo);
	      fputc('\n', fipo);
	    }
	  }
	  /* ipHostNumber: */
	  fputs(itadmin_hosts_c8_kw[10], fipo);
	  itadmin_tool_write_ip(fipo, host->ipv4);
	  fputc('\n', fipo);
	  fputc('\n', fipo);
	}
      }
      if(any) {
        back = 1;
      }
#if DK3_CHAR_SIZE == 1
      if(!dk3sf_fclose_fn_app(fipo, itadmin_hosts_c8_kw[2], job->app)) {
        back = 0;
      }
#else
      if(!dk3sf_fclose_fn_app(fipo, NULL, job->app)) {
        back = 0;
      }
#endif
    } else {			

#line 281 "itahosts.ctr"
    }
  } else {		

#line 283 "itahosts.ctr"
  }
  

#line 285 "itahosts.ctr"
  return back;
}


