#! /usr/bin/ruby1.8
#
# =filelist.rb
#
# ファイルのリストを並べた html ファイルの作成.
#
# ==USAGE
#
#    % filelist.rb [options] [ext1, [ext2,…] ]
#
# ただし ext? はファイル拡張子.
#
# ==Description:
#
# 指定された拡張子のファイルのリストを並べた html ファイルを作成する.
#
# ==OPTIONS
#  --all                : 全てのファイルのリストを作る. デフォルトでは日付(YYYY-MM-DD 形式) の入ったファイルのみのリストを作る.
#  --debug              : デバッグモード
#   -h/--help           : help の出力
#  --nodir              : ディレクトリをリストに入れない.
#  -o/--output ファイル名  : 出力するファイル名. デフォルトは index.htm
#  --path ディレクトリ名 : リストを作りたいファイル達が格納されているディレクトリ.
#                         デフォルトはカレントディレクトリ.
#  --style   ファイル名 : スタイルシートファイルの指定. デフォルトは ./style.css
#
# ==EXAMPLE
#
#  % filelist-html.rb --path /GFD_Dennou_Club/dc-arch/hero html
#  % filelist-html.rb --output memo.htm html
#  % filelist-html.rb html txt tex
#
# ==TODO
#
# - getopts ではなく ruby1.8 推奨の optparse を使うようにしないといけない.
# - 作成者の名前が現在は login 名である.
#   漢字の名前にもできるようにしておくのが良いかもしれない. 
#   gate/userdb を parse すれば良い.
#   しかし, さしあたって pending とする.
# - 現在タイトルは絶対パス.
#   上の階層はとった方が良いかも. dcpam/memo くらいとか.
#   タイトルもオプションで与えられるようにした方が良いだろう.
# - 現在リストされる各ファイルの説明は一切ついていない.
#   SIGEN ファイルからとるようにしてもしても良いかもしれない.
#   が, 簡潔性優先ということでこのまま.
#
# ==HSITORY
#
#   2005/03/31 M. Ishiwatari 新規作成
#   2005/04/01 M. Ishiwatari ver. 0.1
#   2005/04/02 M. Ishiwatari ver. 0.2
#              <ul> タグが閉じていなかったのを修正
#              <a href=…> タグが閉じていなかったのを修正
#              タイムスタンプに関しては最終更新のみに
#              filelist-html.rb (このスクリプト)へのlink を作成
#              以上デバッグ協力 : 森川靖大
#   2005/04/07 M. Ishiwatari ver. 0.3
#              スタイルシートファイルのオプション指定.
#              ディレクトリに対応.
#   2005/05/09 M. Ishiwatari ver. 0.4
#              ruby1.8 を使用することに変更.
#   2005/05/16 M. Ishiwatari
#              ちょこっと整形.
#   2005/05/30 M. Ishiwatari
#              TODO 追加


##########################################
#
#    関数定義

# ファイルの拡張子を返す関数. 
# dennou に ruby1.6 しか入っていなかった時代に必要だった
#def File.extname(f)
#  if /.(\.[^.]*)$/m =~ basename(f)
#    $1
#  else
#    ''
#  end
#end

# html ヘッダ部
def htmlhead
   htmls = <<HTMLEOF
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=x-euc-jp">
<title>#{$title}</title>
<link href="#{$stylefile}" type="text/css" rel="stylesheet" />
</head>
HTMLEOF
return htmls
end

# 本文表題部
def htmltitle
  current_abs_path = Pathname.pwd
  command_abs_path = Pathname.new("#{$0}")
  command_path = command_abs_path.relative_path_from(current_abs_path).to_s
  command_name = File.basename("#{$0}")

  htmls = <<HTMLEOF
<body>
<h1>#{$title}</h1> 
<ul>
<li>#{$time_update} (#{$editor}) 最終更新 (<a href="#{command_path}">#{command_name}</a> を使用)</li>
</ul>
<h2>ファイルリスト</h2> 
HTMLEOF
return htmls
end

# html フッタ部
def htmlfoot
  htmls = <<HTMLEOF    
</body>
</html>
HTMLEOF
  return htmls
end

# help の出力
def print_help
  print "#{$0}:指定された拡張子のファイルのリストを並べた html ファイルを作成する. \n"
  print " \n"
  print "使い方 \n"
  print "    % filelist.rb [options] [ext1, [ext2,…] ] \n"
  print "ただし ext? はファイル拡張子.\n"
  print " \n"
  print "オプション \n"
  print "  --all                : 全てのファイルのリストを作る. \n"
  print "                         デフォルトでは日付(YYYY-MM-DD 形式) の入ったファイルのみのリストを作る. \n"
  print "  --debug              : デバッグモード \n"
  print "  --help/-h           : help の出力 \n"
  print "  --path ディレクトリ名 : リストを作りたいファイル達が格納されているディレクトリ. \n"
  print "                         デフォルトはカレントディレクトリ. \n"
  print "  --nodir              : ディレクトリをリストに入れない. \n"
  print "  --output/-o ファイル名  : 出力するファイル名. デフォルトは index.htm \n"
  print "  --style   ファイル名 : スタイルシートファイルの指定. デフォルトは ./style.css \n"
  print "\n"
  print "使用例 \n"
  print "  % filelist-html.rb --path /GFD_Dennou_Club/dc-arch/hero html \n"
  print "  % filelist-html.rb --output memo.htm html \n"
  print "  % filelist-html.rb html txt tex \n"
  exit 1
end


require 'getopts'
require 'date'
require 'pathname'
$KCODE = "e"


#
# オプション解析部
#

# 正しいオプションが与えられなかった時
unless getopts(
               "hHo",
               "path:",  "help", "all", "output:", "style:", "debug", "nodir"
               )
  print "#{$0}:illegal options. please exec this program with -h/--help. \n"
  exit 1
end

# 正しいオプションが与えられなかった時, help 出力
if $OPT_help or $OPT_h
  print_help
end

# リストを作るディレクトリパス
if ($OPT_path)
  $path = ($OPT_path)
else
  $path = './'
end

# 出力ファイル名
if $OPT_output
  $outputfile = $OPT_output
elsif $OPT_o
  $outputfile = $OPT_o
else
  $outputfile = 'index.htm'
end

# スタイルシートを作るディレクトリパス
if ($OPT_style)
  $stylefile = ($OPT_style)
else
  $stylefile = './style.cs'
end

# 対象とする拡張子リストの作成
extention = Array.new
if ARGV == [] # 引数が与えられなかった時 デフォルトは html と htm
   extention << 'html'
   extention << 'htm'
   extention << 'txt'
else
  ARGV.each do |i|
    extention << i
  end
end 
puts extention if ($OPT_debug)


#
# リストに載せるファイルのリストを作成
#

$filelist = []
Dir.foreach("#{$path}") { |item|
  if File::ftype(item) == "directory" 
     unless ($OPT_nodir) 
        if ($OPT_all) 
          $filelist.push( item )
        else
          $filelist.push( item ) if File.basename(item) =~/\d\d\d\d-\d\d-\d\d/
        end
     end
  else
    extention.each do |name|
      if File.extname(item) =~ /#{name}$/ 
        if ($OPT_all) 
           $filelist.push( item  )
        else
          $filelist.push( item  ) if File.basename(item) =~/\d\d\d\d-\d\d-\d\d/
        end
      end
      # htm.ja や htm.en を拾うための処理
      if name =~ /html*\..*$/
        if item =~ /#{name}$/ 
          if ($OPT_all)
            $filelist.push( item  )
          else
            $filelist.push( item  ) if File.basename(item) =~/\d\d\d\d-\d\d-\d\d/
          end
        end
      end
    end
 end
}

$filelist.sort!
$filelist.reverse!
puts $filelist if ($OPT_debug)

#
# 諸々の値の設定
#

$title = File.expand_path($path)
$editor = ENV['USER']
$time_update = Time.now.strftime("%Y/%m/%d")
$time_first = Time.now

#
# html ファイルへ書き出す中身の作成
#

# html ヘッダ部分
$htmls = htmlhead
# 本文表題部
$htmls << htmltitle

# ファイルリスト部分
$htmls << "<ul> \n"
$filelist.each do |filename|
  $line = "<li> <a href=\"./#{filename}\"> #{filename} </a> \n"
  $htmls << $line
end
$htmls << "</ul> \n"

# html フッタ部分
$htmls << htmlfoot

#
# html ファイルへ出力
#

if ($OPT_debug)
  puts $htmls
else   # ファイルの書きだし
  outfile = open( $outputfile, "w")
  outfile.print $htmls
  outfile.close
end
