#!/usr/bin/perl -w

use strict;

# Detect and remove the following codesnippets:

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_WINSOCK2_H
# include <winsock2.h>
#endif

my $found=0;
while(<>){
  # Replace sys/types.h u_XXX by their glib.h guXXX equivalents.
  s/([^a-zA-Z0-9_])u_int/$1guint/g;
  s/([^a-zA-Z0-9_])u_char/$1guchar/g;
  s/([^a-zA-Z0-9_])u_short/$1gushort/g;
  s/([^a-zA-Z0-9_])u_long/$1gulong/g;
  # Replace netinet/in.h hton-family by their glib.h equivalents.
  s/([^a-zA-Z0-9_])([hn]to[nh][ls])/$1g_$2/g;
  if ($found != 0 ||
	/# *ifdef +HAVE_SYS_TYPES_H/ ||
	/# *ifdef +HAVE_WINSOCK2_H/ ||
	/# *ifdef +HAVE_NETINET_IN_H/) {
    $found++;
    if ($found == 4) {
      $found = 0;
      if (/\S/) {  # Print fourth line, if it is not empty
        print;
      }
    }
  } else {
    print;
  }
}
