#!/usr/bin/env ruby
# vim:ts=2 sw=2 et
# ltxpkg.rb : パ ruby 糶 ltxpkg.sh 獽続ノㄤ穨╰参
#             LaTeX package 甅ン琩高ㄣ
# "Edward G.J. Lee" <edt1023@cle.linux.org.tw> (08/25/05)
#
# This code is Public Domain.
# $Id: ltxpkg.rb,v 1.5 2005/09/09 09:34:27 edt1023 Exp $

# 眔㏑把计 basename
Thisver='1.0'
prgname=File.basename($0)

uSAGE = <<USAGE

  Query your LaTeX package. ltxpkg.rb v #{Thisver}
  Usage: #{prgname} package-name[.sty]

USAGE

# Τㄇ OS ⊿Τ whichτΤㄇ OS  which ︽ぃ妓
# ┮︽糶
def mywhich(p)
  paths = ENV['PATH'].split(':')
  paths.each do |i|
    cmd = File.join(i, p)
    if test(?x, cmd)
      $kpath=cmd
      break
    end
  end
end

# т箇更 packages
def prepkg(f)
  a=Array.new
  IO.foreach(f) do |line|
    if (line !~ /\<\def\>/) && (line !~ /^%/) && (line =~ /\\RequirePackage/)
      a << $'.gsub(/.*\{|\}.*/,"")
    end
  end
  puts a.uniq.sort
end

# т package  version
def styver(sty)
  thever=""
  a = IO.readlines(sty)
  open(sty) do |file|
    file.each_line do |line|
      if line =~ /\\def\s*\\RInfo/
        thever = $'.gsub(/\{|\}/,'')
        break
      elsif line =~ /\\.*def\s*\\.*fileversion|\\.*def\s*\\FMithmInfo|\\.*def\s*\\fontinstversion/
        thever = $'.gsub(/\{|\}/,'')
        break
      elsif line =~ /^\s*\\ProvidesPackage|^\s*\\ProvidesFile|^\s*\\ProvidesClass/
        s = $'
        s1 = a[file.lineno]
        s2 = a[file.lineno+1]
        s3 = a[file.lineno+2]
        s4 = a[file.lineno+3]
        s5 = a[file.lineno+4]
        s6 = a[file.lineno+5]
        s7 = a[file.lineno+6]
        if s =~ /\d.*\/\d.*\/\d.*/
          thever = s
        elsif s1 =~ /\d.*\/\d.*\/\d.*/
          thever = s1
        elsif s2 =~ /\d.*\/\d.*\/\d.*/
          thever = s2
        elsif s3 =~ /\d.*\/\d.*\/\d.*/
          thever = s3
        elsif s4 =~ /\d.*\/\d.*\/\d.*/
          thever = s4
        elsif s5 =~ /\d.*\/\d.*\/\d.*/
          thever = s5
        elsif s6 =~ /\d.*\/\d.*\/\d.*/
          thever = s6
        elsif s7 =~ /\d.*\/\d.*\/\d.*/
          thever = s7
        else
          thever = s
        end
        break
      else
        thever = "I can't determine the version. Please read the source."
      end
    end
  end
  puts "Version: " + thever.gsub(/\[|\]|\{.*\}|\\space|%.*/,' ').squeeze(' ')
end

# main()
# 耞猭把计の耞 kpsewhich 琌Τ杆ち抖杠碞秈︽耞
#kpath = File.basename(mywhich('kpsewhich').to_s)
mywhich('kpsewhich')
if ARGV.length != 1 or ARGV[0] =~ /^\W/
  puts uSAGE
  exit
elsif !$kpath
  puts
  puts "This script need kpsewhich. Abort!"
  puts
  exit
else
  styhead=File.basename(ARGV[0],'.sty')
  styfull=styhead + '.sty'
  stypath=`kpsewhich #{styfull}`.chomp
  if stypath == ""
    puts
    puts "  Seems you didn't install #{styhead} package."
    puts "  Or the package didn't in your kpathsea path."
    puts
    exit
  else
    puts
    puts "The position of this package is installed at:"
    puts stypath
    styver(stypath)
    puts
    puts "The preloaded package(s) of #{styhead} is(are):"
    prepkg(stypath)
    puts
  end
end
