#! /usr/bin/ruby -w
# kern-ps - create postscript files for kernel sources
# Copyright (C) 2003 Ed Cashin
# 
# 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; either version 2
# of the License, or (at your option) any later version.
# 
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# usage:
#   kern-ps {destdir} {header_dir} {files}
#
# example:
#   cd /path/to/2.5.70/..
#   kern-ps /tmp/ps 2.5.70/include 2.5.70/kernel/fork.c 2.5.70/mm/mmap.c
#   kern-ps-merge /tmp/ps > /tmp/all.ps
#   gv /tmp/all.ps
#

class Printer
  def initialize(dest)
    @destdir = dest
    # exit 1 unless system "sh -xc \": > #{@destdir}/all.ps\""
  end

  def ps(filename)
    if not test(?r, filename) then
      puts "skipping #{filename}"
      return
    end
    reldir = File.dirname(filename)
    d = "#{@destdir}/#{reldir}"
    exit 1 unless system "sh -xc \"mkdir -p #{d}\""
    f = File.basename(filename)
    psdoc = "#{d}/#{f}.ps"
    exit 1 unless system "sh -xc \"vgrind #{filename} > #{psdoc}\""
    # exit 1 unless system "sh -xc \"echo '(#{reldir}/#{f}.ps) run' >> #{@destdir}/all.ps\""
  end

  def headers(filename)
    h = []
    IO.foreach(filename) { |s|
      if s =~ /\#include\s+<([^>]+)>/ then
	h << $1
      end
    }
    h
  end
end

(destdir, hdir, files) = ARGV
headers = {}
p = Printer.new(destdir)
files.each { |f| 
  p.ps(f)
  p.headers(f).each { |h|
    headers[h] = true
  }
}
headers_so_far = headers.keys.sort
headers_so_far.each { |a|
  p.headers("#{hdir}/#{a}").each { |b|
    headers[b] = true
  }
}
headers.keys.sort.each { |k|
  p.ps("#{hdir}/#{k}")
}
