/* Copyright (c) 2006 Dirk Jagdmann <doj@cubic.org>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you
       must not claim that you wrote the original software. If you use
       this software in a product, an acknowledgment in the product
       documentation would be appreciated but is not required.

    2. Altered source versions must be plainly marked as such, and
       must not be misrepresented as being the original software.

    3. This notice may not be removed or altered from any source
       distribution. */

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

FILE *in, *out;

int unget=-1;
char str[256], ws[256];
void get_token()
{
  char c=0;
  int s=0, w=0;
  str[0]=ws[0]=0;

  if(unget >= 0)
    {
      ws[w++]=unget;
      unget=-1;
    }

  /* get whitespace */
  do {
    if(fread(&c, 1, 1, in) < 1)
      break;
    if(isspace(c))
      ws[w++]=c;
    else
      break;
  } while(1);
  ws[w]=0;

  if(feof(in) || ferror(in))
    return;

  str[s++]=c;

  /* get whitespace */
  do {
    if(fread(&c, 1, 1, in) < 1)
      break;
    if(!isspace(c))
      str[s++]=c;
    else
      break;
  } while(1);
  str[s]=0;

  unget=c;
}

void skip_line()
{
  char c=0;
  if(unget == '\n')
    {
      c='\n';
      fwrite(&c, 1, 1, out);
      return;
    }
  else if(unget >= 0)
    {
      c=unget;
      fwrite(&c, 1, 1, out);
      unget=-1;
    }

  do {
    if(fread(&c, 1, 1, in) < 1)
      return;
    fwrite(&c, 1, 1, out);
    if(c=='\n')
      return;
  } while(!feof(in) && !ferror(in));
}

int main(int argc, char **argv)
{
  enum {
    WAIT_FOR_IN,
    WAIT_FOR_SOA,
    WAIT_FOR_HN,
    WAIT_FOR_EMAIL,
    WAIT_FOR_PAR,
    WAIT_FOR_SER,
    COPY_THROUGH
  };
  int state=WAIT_FOR_IN;

  in=stdin;
  out=stdout;
  if(argc == 2)
    {
      char fn[PATH_MAX];
      snprintf(fn, sizeof(fn), "%s.bak", argv[1]);
      unlink(fn);
      if(rename(argv[1], fn) < 0)
	{
	  fprintf(stderr, "could not rename %s to %s : %s\n", argv[1], fn, strerror(errno));
	  return 1;
	}
      in=fopen(fn, "r");
      if(!in)
	{
	  fprintf(stderr, "could not open %s for reading : %s\n", fn, strerror(errno));
	  return 1;
	}
      out=fopen(argv[1], "w");
      if(!out)
	{
	  fprintf(stderr, "could not open %s for writing : %s\n", argv[1], strerror(errno));
	  return 1;
	}
    }
  else if(argc > 2)
    {
      printf("usage: dns_inc_serial [zonefile]\n");
      printf("if zonefile is given on command line it will be modified in place.\n");
      printf("otherwise the program reads from stdin and writes to stdout\n");
      return 1;
    }

  while(!feof(in) && !ferror(in))
    {
      int len;

      get_token();
      fputs(ws, out);

      if(str[0] == ';')
	{
	  fputs(str, out);
	  skip_line();
	  continue;
	}

      switch(state)
	{
	default:
	case WAIT_FOR_IN:
	  if(!strcmp(str, "IN"))
	    state=WAIT_FOR_SOA;
	  break;

	case WAIT_FOR_SOA:
	  if(!strcmp(str, "SOA"))
	    state=WAIT_FOR_HN;
	  break;

	case WAIT_FOR_HN:
	  len=strlen(str);
	  if(len>0 && str[len-1]=='.')
	    state=WAIT_FOR_EMAIL;
	  break;

	case WAIT_FOR_EMAIL:
	  len=strlen(str);
	  if(len>0 && str[len-1]=='.')
	    state=WAIT_FOR_PAR;
	  break;

	case WAIT_FOR_PAR:
	  if(!strcmp(str, "("))
	    state=WAIT_FOR_SER;
	  break;

	case WAIT_FOR_SER:
	  for(len=0; len<strlen(str); ++len)
	    if(!isdigit(str[len]))
	      {
		state=WAIT_FOR_IN;
		break;
	      }
	  if(state==WAIT_FOR_SER)
	    {
	      int ser=atoi(str)+1;
	      snprintf(str, sizeof(str), "%i", ser);
	      state=COPY_THROUGH;
	    }
	  else
	    state=WAIT_FOR_IN;
	  break;

	case COPY_THROUGH: break;
	}

      fputs(str, out);
    }

  fclose(in);
  fclose(out);
  return 0;
}
