class Func
  def header
    print "module gms_size\n"
    print "  use datatype\n"
    print "  use mem_manager\n"
    print "  implicit none\n"
  end

  def contains
    print "contains\n"
  end

  def footer
    print "end module gms_size\n"
  end
end

class Func1
Modname = "size" 
  def header1(name) #name = "x", "y", "z"
    print "  interface ", Modname,"_", name, "\n"
  end

  def header2(name, type) #name = "x", "y", "z"
    if type.index(name) != nil then
      print "    module procedure ", Modname, "_",name ,"_", type, "\n"
    end
  end

  def header3
    print "  end interface\n\n"
  end

  def output(name, type)
    if type.index(name) != nil then
      print "  function ", Modname, "_", name, "_", type, "(input) result(output)\n"
      print "    type(var_", type, "), intent(in) :: input\n"
#      print "    integer :: i \n"
      print "    integer :: output\n\n"
      if name == "x" then 
      print "    if ( input%grid(1) == 0 ) then \n"
      print "      output = pub1 - plb1 + 1 \n"
      print "    else  \n"
      print "      output = pub1- plb1  \n"
      print "    end if \n"
      elsif name == "y" then
      print "    if ( input%grid(2) == 0 ) then \n"
      print "      output = pub2 - plb2 + 1 \n"
      print "    else  \n"
      print "      output = pub2- plb2 \n"
      print "    end if \n"
      elsif name == "z" then
      print "    if ( input%grid(3) == 0 ) then \n"
      print "      output = pub3 - plb3 + 1\n"
      print "    else  \n"
      print "      output = pub3- plb3  \n"
      print "    end if \n"
      end
      print "  end function ", Modname, "_", name, "_", type, "\n\n"
    else return
#      print "  function ",Modname,"_", name, "_", type, "(input) result(output)\n"
#      print "    type(var_", type, "), intent(in) :: input\n"
#      print "    integer :: output\n"
#      print "    integer :: new_id\n\n"

#      print "    stop \"", Modname, "_", name, "_", type, "\"\n "
#      print "    output= 0.0\n "
#      print "  end function ", Modname, "_", name, "_", type, "\n\n"
    end
  end
end

func = Func.new
func1 = Func1.new

type=[7]
name=[2]

type[0] = "x"
type[1] = "y"
type[2] = "z"
type[3] = "xy"
type[4] = "xz"
type[5] = "yz"
type[6] = "xyz"

name[0] = "x"
name[1] = "y"
name[2] = "z"

func.header

for  i in 0..2 do
  func1.header1(name[i])
  for  j in 0..6 do
    func1.header2(name[i], type[j])
  end
  func1.header3
end

func.contains

for i in 0..2 do
  for j in 0..6 do
    func1.output(name[i], type[j])
#    func2.output(name[i], type[j])
#    func4.output(name[i], type[j])
  end
end

func.footer
