#!/usr/bin/env ruby

require 'optparse'
require "libexec/check"
require "libexec/get_master"
require "libexec/ip_addr_class"
require "libexec/get_mac"
require "libexec/collect-mac-common"

#############################
# 変数の初期化
#############################
filename = "etc/dcast.conf"
h = Hash.new
h.store("eth", "eth0")
h.store("netmask", get_interfaces_info("netmask", "eth0"))
h.store("network", get_interfaces_info("network", "eth0"))
h.store("broadcast", get_interfaces_info("broadcast", "eth0"))
h.store("ip_address", get_interfaces_info("ip_address", "eth0"))
h.store("prefix", "pc")
h.store("domain", "dcast.org")
h.store("digit", "2")
h.store("swap", "512")

#############################
# Check root user or not
#############################
check_uid()

#############################
# オプションの解析
#############################
parser = OptionParser.new(width=100)
parser.banner = "Usage: #{File.basename($0)} --eth=ETH --number=NUMBER [--startip=STARTIP] [--prefix=PREFIX] [--domain=DOMAIN] [--digit=DIGIT] [--swap=SWAP]"

parser.on("--number=NUMBER", "Number of slave nodes."){|arg|
  h.store("number", arg)
}
parser.on("--eth=ETH", "Ethernet Number in cluster network."){|arg|
  h.store("eth", arg)
}
parser.on("--startip [STRTIP] [#{"next your IP Address"}]", "Start IP Address."){|arg|
  h.store("startip", arg)
}
parser.on("--prefix [PREFIX] [pc]", "Prefix of slave node."){|arg|
  h.store("prefix", arg)
}
parser.on("--domain [DOMAIN] [dcast.org]", "Domain of slave node."){|arg|
  h.store("domain", arg)
}
parser.on("--digit [DIGIT] [2]", "Digit of IP Address."){|arg|
  h.store("digit", arg)
}
parser.on("--swap [SWAP] [512M]", "SWAP area of Slave Node."){|arg|
  h.store("swap", arg)
}
parser.on("--help", "-h", "Print this message and quit."){
  puts "set: --help"
  puts parser.help
  exit 0
}

#############################
# main
#############################
begin
  parser.summary_width=47
  parser.summary_indent=" "*1
  parser.parse!
# puts "argv = #{ARGV.inspect}"

  # 値の代入
  h.store("netmask", get_interfaces_info("netmask", h["eth"]))
  h.store("network", get_interfaces_info("network", h["eth"]))
  h.store("broadcast", get_interfaces_info("broadcast", h["eth"]))
  h.store("ip_address", get_interfaces_info("ip_address", h["eth"]))
  # local IP Address + 1
  b = IP_Addr.new(h["netmask"], h["ip_address"], 2)
  ip = ("%032b") % (b.ipaddr_change_10_2(h["ip_address"]).to_i(2) + 1)
  h.store("startip", b.ipaddr_change_2_10_dot(ip))

 # 不要な引数があるかどうかをチェック
  value = ["number", "eth", "netmask", "network", "broadcast", "startip", "prefix", "domain", "digit", "swap"]
  value.each{|i|
    if h[i] == nil
      $stderr.puts parser.help
      exit 1
    end
  }

  # swapが数値かどうかのチェック
  s = h["swap"].to_i
  h.store("swap", s)
  if h["swap"] =~ /M$/
    tmp = h["swap"].match(/M$/)
    swapsize = tmp.pre_match
  else
    swapsize = h["swap"]
  end
  if swapsize.to_i == 0
    puts "--swapの値が間違えています。" +  "[" + h["swap"].to_s + "]"
    exit 1
  end

  # swapの最後にMがなければ足す
  unless h["swap"] =~ /M$/
    swap = h["swap"].to_s + "M"
    h.store("swap", swap)
  end

  # numberが数値かどうかのチェック
  if h["number"].to_i == 0
    puts "--numberの値が間違えています。" +  "[" + h["number"] + "]"
    exit 1
  end

 # 不要な引数があるかどうかのチェック２
  ARGV.inspect.each{|a|
    if a != "[]"
      puts "予約されていない引数です。" + a
      exit 1
    end
  }

  # 入力した値を画面に出力
  value.each{|i|
    puts i + " = " + h[i]
  }

  mac = get_interfaces_info("mac_address", h["eth"])
  h.store("mac_address", mac)

  # filename(dcast.conf)に出力
  dcast_conf_write(filename, h)

  get_mac(h, filename)   # Collect Mac Address in cluster network.

  # filename(dcast.conf)に出力
  dcast_conf_buttom_write(filename)

  puts "MAC ADDRESS finished collecting."
  puts "Created \"./" + filename + "\"."

rescue OptionParser::ParseError => err
  $stderr.puts err.message
#  $stderr.puts parser.help
  exit 1
end
