#!/usr/bin/env ruby
=begin
=gplist

Print out info on GPhys-compatible variables in specified files.

==Usage 

   % gplist [-h] [ FILES... ]

where FILES are plane files (NetCDF, grib, GrADS control..) or directories. 
If omitted, equivalent to specifing the current directory.

==HISTORY

   2005/05/15  S Takehiro (created)
   2005/06/21  T Horinouchi (modified to use GDir (non-recursive version))
   2005/08/10  S Takehiro (utilize internal function for printing help message)

=end

require "numru/gphys"
include NumRu

require "getoptlong"

#------------------------ print help message ------------------------
def help
  file = File.open(__FILE__)
  after_begin = false
  after_end = false
  while (line = file.gets)
    after_end = true if /^=end/ =~ line
    print line if after_begin && !after_end
    after_begin = true if /^=begin/ =~ line
  end
  file.close
end

#---------------------- Option Configuration ----------------------
parser = GetoptLong.new(
  ["--help",     "-h", GetoptLong::NO_ARGUMENT      ])

begin
  parser.each do |opt, arg|
    case opt
    when "--help"
      help
      exit(0)
    end
  end
rescue
  raise "\n\n"+usage
end

#------------------------ Do the job ------------------------
GDir.top='/'

if ARGV.length==0
  paths = ['.']
else
  paths = ARGV
end

paths.each do |path| 
  print path,":\n"
  gdir = GDir.new(File.expand_path(path))
  # for a plane file:
  gdir.list_data_v.each{|s| print "  #{s}\n"}
  # for a directory:
  gdir.list_dirs.each do |sub|
    lsv = gdir.dir(sub).list_data_v
    if lsv.length>0
      print( (ARGV.length!=0 ? "#{path.sub(/\/+$/,'')}/" : ""), sub, ":\n")
      lsv.each{|s| print "  #{s}\n"}
    end
  end
end
