#!/usr/bin/env ruby

if(ARGV.length == 0) then
  ( Dir.glob("*.c")  + Dir.glob("*/*.c") ).each { |file|
    puts file
    Kernel.system("./movercsid.rb #{file}") or raise "couldn't do #{file}"
  }
  exit
end

HeaderString = ['#ifndef lint', 
                'static const char rcsid[] _U_ =', 
                '"@(#) $Header:$ (LBL)";', 
                '#endif'].join("\n")

Kernel.test(?e, ARGV[0]) or raise "#{ARGV[0]} doesn't exist."

def do_reorder(file)
  File.rename(ARGV[0], ARGV[0] + ".bak");
  o = File.open(ARGV[0], "w");
  lines = File.open(ARGV[0] + ".bak").readlines
  lintline = (0..(lines.length-1)).detect { |i| lines[i] =~ /^#ifndef lint/ }
  lintlineend = (lintline..(lintline+7)).detect { |i| lines[i] =~ /^#endif/ }
  interfaceline = (0..(lines.length-1)).detect { |i| lines[i] =~ /^#include\s*["<]interface.h[>"]/ }
  if(lintline == nil) then
    puts "#{file} lacks rcsid"
  elsif(lintlineend == nil) then
    raise "#{file} odd"
  elsif(lintline < interfaceline) then
    move = lines[lintline..lintlineend]
    lines[lintline..lintlineend] = []
    # place the header conveniently after a block of include statements 
    afterinterface = ((interfaceline-3)..(lines.length-1)).detect { |i| lines[i] !~ /^#include\s*/ }
    raise "wha?" if(afterinterface == nil) 
    move.map! { |ln| ln.gsub(/ =/, " _U_ =") }
    lines[afterinterface+1, 0] = move
  end
  o.puts lines
end

seenLint = false;
seenInterface = false;
seenStdinc = false;

File.open(ARGV[0]).each  { |ln|
  case ln
  when /tcpdump-stdinc.h/ then
    seenStdinc = true
  when /interface.h/ then
    if(seenLint) then
      do_reorder(ARGV[0])
      exit
    end
    seenInterface = true
  when /ifndef lint/ then
    seenLint = true;
    if(seenInterface) then
      puts "#{ARGV[0]} is already ok"
      exit
    end
  end
}

if(seenLint && !seenInterface) then
  # put an interface.h in there, and call us again.
  puts "warning: #{ARGV[0]} rcsid without interface.h; inserting"
  File.rename(ARGV[0], ARGV[0] + ".bak");
  o = File.open(ARGV[0], "w");
  lines = File.open(ARGV[0] + ".bak").readlines
  # try to place it after the config.h.
  haveconfigline = (0..(lines.length-1)).detect  { |i| lines[i] =~/HAVE_CONFIG_H/ }
  
  includeinterface = "\n"'#include "interface.h"' + "\n\n"
  # insert stdin
  if(!seenStdinc) then 
    if(!haveconfigline) then
      # have neither, append at the bottom 
      lines.push("\n#ifdef HAVE_CONFIG_H\n" , '#include "config.h"' , 
"#endif\n", '#include "tcpdump-stdinc.h"', includeinterface)
    else
      # have just config.h, append both after endif
      afterendifline = (haveconfigline..(lines.length-1)).detect  { |i| lines[i] =~/^#endif/ }
      lines[afterendifline+1,0] = [ "\n" + '#include "tcpdump-stdinc.h"', includeinterface ] 
    end
  else
    # don't have interface (that's the whole problem, but do have stdinc.
    afterstdinc = (0..(lines.length-1)).detect  { |i| lines[i] =~/tcpdump-stdinc/ }
    lines[afterstdinc+1,0] = ("\n" + includeinterface)
  end

#  if(haveconfigline) then
#    afterendifline = (haveconfigline..(lines.length-1)).detect  { |i| lines[i] =~/^#endif/ }
#    lines[afterendifline+1,0] = [ includeinterface ]
#  else
#    lines.push("\n#ifdef HAVE_CONFIG_H\n" , '#include "config.h"' , 
#"#endif\n", includeinterface)
#  end

  o.puts lines
  o.close
  Kernel.system("./movercsid.rb #{ARGV[0]}")
end


