+require "cruxutils/ohash.rb"
+require "tempfile"
+require "fileutils"
+
+module CruxUtils
+
+class Port
+ COMMENT_MAP = OHash.new
+ COMMENT_MAP["Description"] = :description
+ COMMENT_MAP["URL"] = :url
+ COMMENT_MAP["Packager"] = :packager
+ COMMENT_MAP["Maintainer"] = :maintainer
+ COMMENT_MAP["Depends on"] = :dependencies
+ COMMENT_MAP["Group"] = :group
+ COMMENT_MAP["Nice to have"] = :nice_to_have
+
+ def Port.open(dir)
+ yield Port.new(dir)
+ end
+
+ def initialize(dir)
+ @dir = dir
+
+ @attribs = Hash.new { |h, k| h[k] = "" }
+
+ @attribs[:dependencies] = Dependencies.new
+ @attribs[:source] = Sources.new
+ @attribs[:build] = BuildFunc.new
+
+ read
+ end
+
+ def method_missing(meth, *args)
+ if meth.to_s.match(/=$/) then
+ m2 = meth.to_s[0...-1].to_sym
+ @attribs[m2] = (args.length < 2 ? args.first : args)
+ else
+ @attribs[meth]
+ end
+ end
+
+ def dependencies
+ @attribs[:dependencies].uniq!
+ @attribs[:dependencies]
+ end
+
+ def has_footprint?
+ File.exist?(footprint)
+ end
+
+ def has_md5sum?
+ File.exist?(md5sum)
+ end
+
+ def write
+ Tempfile.open("pkgfile") do |file|
+ COMMENT_MAP.each do |key, symbol|
+ value = @attribs[symbol]
+ next if value.empty?
+
+ file << "# #{key}: " << value << "\n"
+ end
+
+ file << "\n"
+
+ vars = [:name, :version, :release, :source]
+ vars.each do |var|
+ file << "#{var}=" <<
+ @attribs[var] << "\n"
+ end
+
+ file << "\n" << build << "\n"
+ file.flush
+
+ FileUtils.cp(file.path, pkgfile)
+ end
+ end
+
+ private
+ def read
+ vars = [:name, :version, :release]
+ section = :comments
+
+ cmt_map = OHash.new.replace(COMMENT_MAP)
+
+ File.read(pkgfile).split("\n").each do |line|
+ line.chomp!
+
+ cmt_map.each do |key, symbol|
+ md = line.match(/^#\s*#{key}: (.+)$/)
+ unless md.nil?
+ @attribs[symbol].replace(md.captures.first.strip)
+ cmt_map.delete(key)
+ end
+ end
+
+ vars.each do |var|
+ md = line.match(/^#{var}=(.+)$/)
+ unless md.nil?
+ @attribs[var] = $1.strip
+ vars.delete(key)
+ end
+ end
+
+ # handle source array
+ section = :void if line.match(/^\s*$/) && section != :build
+
+ if section == :source && line.match(/(.+)\)$/)
+ @attribs[:source] << $1.strip
+ elsif section == :source && line.match(/(.+)\\$/)
+ @attribs[:source] << $1.strip
+ elsif line.match(/^source=\((.+)\)$/)
+ @attribs[:source] << $1.strip
+ elsif line.match(/^source=\((.+)\\$/)
+ @attribs[:source] << $1.strip
+ section = :source
+ end
+
+ section = :end unless line.match(/^\}/).nil?
+ if section == :build
+ # remove initial indentation
+ tmp = line.sub(/^( |\t)/, "")
+
+ @attribs[:build] << (tmp || "")
+ end
+ section = :build unless line.match(/^build/).nil?
+ end
+ end
+
+ def pkgfile
+ File.join(@dir, "Pkgfile")
+ end
+
+ def footprint
+ File.join(@dir, ".footprint")
+ end
+
+ def md5sum
+ File.join(@dir, ".md5sum")
+ end
+end
+
+class Dependencies < Array
+ def to_s
+ uniq.join(", ")
+ end
+
+ def replace(arg)
+ if arg.is_a?(Array)
+ super
+ else
+ clear
+
+ arg.split(", ").each { |d| self << d.strip }
+ end
+
+ uniq!
+
+ self
+ end
+end
+
+class Sources < Array
+ def to_s
+ indent = " " * "source=(".length
+ "(" + join(" \\\n" + indent) + ")"
+ end
+end
+
+class BuildFunc < Array
+ def to_s
+ inject("build() {\n") do |a, line|
+ if line != ""
+ line = "\t" + line
+ end
+
+ a << line << "\n"
+ a
+ end + "}"
+ end
+end
+
+end