#!/usr/bin/env ruby
require 'optparse'
require "rubygems/specification"
require_relative '../lib/pathname/common_prefix'

opt = OptionParser.new do |opt|
  opt.version = Gem::Specification.load(File.join(__dir__, "../pathname-common_prefix.gemspec")).version
  opt.banner = <<EOB
Usage: common-prefix
    Show prefix common to pathnames from standard input
Usage: common-prefix file [file [file ...]]
    Show prefix common to pathnames in files
EOB
end
opt.parse!

paths = ARGF.each_line.select {|line| line !~ /^$/}.collect(&:chomp)
begin
  puts Pathname.common_prefix(paths)
rescue => err
  $stderr.puts "ERROR: #{err}"
  abort opt.banner
end
