#!/usr/bin/ruby

class Array
  def sort_byversion
    self.sort { |a,b| 
      cmpa, cmpb = [ a, b ].map { |pkg| 
        pkg.split(/[-\.]/).delete_if { |d| 
          d !~ /^\d+$/ }.map { |v| 
          v.to_i 
        } 
      } 
      cmpa <=> cmpb
    }
  end
end

if ([ "scriptroute-0.2.9-1", 
      "scriptroute-0.2.11-1" ].sort_byversion[0] != "scriptroute-0.2.9-1"  )
  puts "sanity failed"
  exit
end

# may also need 
#  "libstdc++"
    
individuals  = [ 
  "libpcap",
  "ruby-1.6",
  "ruby-devel-1.6",
  "ruby-libs-1.6",
  "ruby-libs-1.6",
  "undns",
].map { |pkg|
  "http://www.scriptroute.org/redhat/" + Dir.glob("#{pkg}*").sort_byversion.reverse[0]
}

of = "scriptroute-install"
srs = [ "scriptroute-client", "scriptroute-server", "scriptroute" ].map { |pkg|
  "http://www.scriptroute.org/redhat/" + Dir.glob("#{pkg}-[0-9]*").sort_byversion.reverse[0]
}

File.open(of, 'w') { |o|
  o.puts "#!/bin/sh"
  o.puts "# process this file by running `wget http://www.scriptroute.org/redhat/#{of}` as root"
  individuals.each { |i|
    o.puts "rpm -Uvh #{i}"
  }
  o.puts "rpm -Uvh #{srs.join(' ')}"
  o.puts "echo 'now run sr-rockettrace www.cs.washington.edu'"
  o.puts "echo 'you may have to type \"rehash\" first'"
  o.puts "echo 'to find other tools, type \"sr\" and hit <tab> a couple times'"
  o.puts "echo 'to see what your shell finds'"
}

#of = "planetlab-install"
#File.open(of, 'w') { |o|
  #o.puts "#!/bin/sh"
  #o.puts "# process this file by running \"wget -nc http://www.scriptroute.org/redhat/#{of} && sh ./#{of}\""
  #o.puts "# as a sudo-capable user; the -nc tells wget to overwrite earlier downloads"
  #plabs = individuals - [ "scriptroute-server", "scriptroute" ]
  #plabs.each { |i|
    #o.puts "rpm --quiet -q #{i.gsub(/^http:\/\/www.scriptroute.org\/redhat\//, '').gsub(/\.i.86\.rpm/,'')} || sudo rpm -Uvh #{i}"
  #}
  #o.puts "sudo rpm -Uvh #{srs.join(' ')}"
  #o.puts "echo 'now run sr-rockettrace www.cs.washington.edu'"
  #o.puts "echo 'you may have to type \"rehash\" first'"
  #o.puts "echo 'to find other tools, type \"sr\" and hit <tab> a couple times'"
  #o.puts "echo 'to see what your shell finds'"
#}

