#! /usr/bin/perl
# Perl のある場所

# Perl Script, 'Make SiteMap'

# Copyright (C) 2001- koko
# koko@sh.rim.or.jp
# http://www.sh.rim.or.jp/~koko/

# ===== 機能 =====

# このスクリプト ( makemap.pl ) が置いてあるディレクトリに
# Site Map ファイル ( $mapfile で指定したファイル ) が作成される．
# 最初に読み込む Index ファイルはコマンドラインで指定

# --- リンクについて ---
# 相対パスは取得して位置を修正
# 絶対パスのとき
# 配列で指定可能
# http://www.ep.sci.hokudai.ac.jp/~epnetfan/tebiki/ は ./ に置き換え

# ===== 引数設定 =====

# Index File ( コマンドライン引数 )
$idxfile = $ARGV[0];

# 出力ファイル名
$mapdir = '/home/epnetfan/public_html/tebiki/';
$maphtml = 'sitemap.html';
$mapfile = $mapdir.$maphtml;

# 取得したリンク先を見にいくもの
@go_link = ( "http://www.ep.sci.hokudai.ac.jp/~epnetfan/tebiki/" );

# 更新マークをつける日数
$newsub = 'Up!';
# 更新日から $modi までの色
$newcolor = '#FF0000';
$modi     = '10';
# $modi +1 から $secmodi までの色
$secnewcolor = '#0000FF';
$secmodi  = '20';

# 文字コード
$code = 'euc';

# ===== メインルーチン =====

require 'jcode.pl';

# Site Map 作成日の取得
( $mday, $mon, $year ) = ( localtime )[3, 4, 5];
$month = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
		   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' )[$mon];
$time = sprintf ( "%02d %s, %02d", $mday, $month, $year + 1900 );

# 更新期間
$start_secmodi = $modi +1;
$thirdmodi  = $secmodi + 1;

# Index File を開く
open ( HOME, "<$idxfile" ) or die "Index File can not be opened.\n";

# ソース中の改行・タブを削除して 1 行にする．
while ( $line = <HOME> ) {
    $line =~ tr/[\r\n\t]+//d;
    $sou = $sou . $line;
}

# ファイルを閉じる
close ( HOME );

# 漢字コードの統一
&jcode'convert(*sou, $code);

# Index File のタイトル取得
    $sou =~ /<title>([\w\W]+?)<\/title>/i;
    $title = $1;

# 引数を空にする
    $sou = "";

# Index File の相対パス化
$idxfile =~ s/\/home\/epnetfan\/public_html\/tebiki/\//;

# Index File と階層 ( 0 ) を配列に入れて一時保存
push ( @files, "$idxfile" );
push ( @num, "0" );

# リンクの取得
&get_link ( $idxfile, $k );

# Site Map ファイルを作成して開く
open ( MAP, "+>$mapfile" ) or die "Site Map can not be made.\n";

# Site Map のヘッダ
print MAP <<EOD;
<HTML lang=ja>
<HEAD>
<TITLE>Site Map - $title -</TITLE>
<STYLE TYPE="text/css">
<!--
p{line-height:140%}
A:hover{ color:#FFCC00 }
A:link,A:visited,A:active,A:hover{ text-decoration:none }
-->
</STYLE>
</HEAD>
<BODY text="#999999" link="#000000" alink="#000000" vlink="#000000" bgcolor="#FFFFFF">
<FONT color="#000000">
    <FONT color="#FF0000"><BIG>S</BIG></FONT>ite
    <FONT color="#FF0000">M</FONT>ap
</FONT>
<BR>
<BR>

This site is  <A href="$idxfile"><B>'$title'</B></A>
<BR>
<BR>

<TABLE cellspacing=3>
<TR>
<TD>||</TD>
<TD><SMALL><FONT color="$newcolor">$newsub</FONT></SMALL></TD>
<TD><SMALL>: $modi 日以内</SMALL></TD>
<TD>||</TD>
<TD><SMALL><FONT color="$secnewcolor">$newsub</FONT></SMALL></TD>
<TD><SMALL>: $start_secmodi 日から $secmodi 日以内</SMALL></TD>
<TD>||</TD>
<TD><SMALL>（なし）</SMALL></TD>
<TD><SMALL>: $thirdmodi 日以上前</SMALL></TD>
<TD>||</TD>
</TR>
</TABLE>
<BR>
<SMALL>このページの更新日 ($time) を基準としてます.</SMALL>

<BLOCKQUOTE>
<P>
EOD

# リンクの整形
for ( $n = 1; $n < @files; $n++ ) {

    my $m = $n + 1;

    if ( $num[$n] == 1 ) {
	$head = "";
    } elsif ( $num[$n] > $num[$m] ){
	$head = '&nbsp;　　 |' x ( $num[$n] - 2 );
	$head = "$head　　└&nbsp;";
    } else {
	$head = '&nbsp;　　 |' x ( $num[$n] - 2 );
	$head = "$head　　├&nbsp;";
    }

    $templink = $head . $files[$n];

# $mapfile に出力
    print MAP $templink;
}

# Site Map のフッタ
print MAP <<EOD;
</P>
</BLOCKQUOTE>


<SMALL>
This page updated on $time and was made by the perl script
 ( <a href="./makemap.pl">makemap.pl</a> ).
</SMALL>

</BODY>
</HTML>
EOD

exit(0);

# ===== サブルーチン =====

# リンクの取得
sub get_link {

    my $htmlfile = $_[0];

    my $k = $_[1];

# 開くファイルの階層
    $k++;

# ファイルを開く
    open ( FILE, "<$htmlfile" );

    while ( $line = <FILE> ) {
	$line =~ tr/[\r\n]+//d;
	$source = $source . $line;
    }

    close ( FILE );

# 漢字コードの統一
&jcode'convert(*source, $code);

# 取得したソース中からコメントの削除
    $source =~ s/<!--[\w\W]*-->//g;

# 引数を空にして置き換え
    my $sou = $source;
    $source = "";

# リンクの取得
GETLINK: while ( $sou =~ /<a href=\"([\.\/\w:~-]+)\"(\starget=\"_top\")?>([\W\w]+?)<\/a>/gi ){
    my $file = $1;
    my $name = $3;

# LinkName が画像のとき
    if ( $name =~ /<img\ssrc=\"[\w\-\.\/]+\"/ and $name =~ /\salt=\"[\w\-\W]+\"/ ) {
	$name =~ s/.+\salt=\"([\w\W\-]+)\".+/$1/;
	$name = "$name ( Image )";
    } elsif ( $name =~ /<img\ssrc=\"[\w\-\.\/]+\"/ and $name =~ /\salt=\"\"/ ) {
	$name =~ s/<img\ssrc=\"([\w\-\.\/]+)\".*/$1/;
	$name = "Image ( $name )";
    } elsif ( $name =~ /<img\ssrc=\"[\w\-\.\/]+\"/ ) {
	$name =~ s/<img\ssrc=\"([\w\-\.\/]+)\".*/$1/;
	$name = "Image ( $name )";
    }

# タグの削除
    $name =~ s/<[\/a-z]+>//gi;

# 絶対パスのとき相対パスに直す
if ( $file =~ /http:./ ) {
    foreach $link ( @go_link ) {
	$file =~ s|$link||g;
    }
}

if ( $file =~ /http:./ or $file =~ /ftp:./ or $file =~ /~/ ) {
next GETLINK;
}

# 相対パスのとき(ディレクトリ再構成)
	my $curdir = $htmlfile;

# ディレクトリの取り出し
# ディレクトリの指定あり（'./' の場合も含む）
	if ( $curdir =~ m|/|g ) {
	    $curdir =~ s/[\w-]+\.[a-z]+$//;
# ファイル名のみのとき
	} else {
	    $curdir = './';
	}

# カレントディレクトリ以下のとき(./)
# 上がって指定したディレクトリ (../)
	my $i = 0;
        if ( $file =~ m/\.\.\/[\w\.-]+/ ) {
                $file = $file;

	} elsif ( $file =~ m/\.\.\/[\.\/\w-]+/ ) {
	    while ( $file =~ m|../|g ) {
		$file =~ s/\.\.\///;
		$i++;
	    }
	    for ($n = 0; $n < $i; $n++ ) {
		$curdir =~ s/[\w-]+\/$//;
	    }

	} else {
	    $file =~ s/^\.\///;
	}

	$file = $curdir . $file;

# '/' で終わっているとき
	if ( $file =~ /\/$/ ) {
	    $file = "${file}index.html";
	    $file =~ s|index.html|| unless -e $file;
	}

    

# ファイルのステータス
    $mtime = int ( -C $file );
    if ( $mtime <= $modi and -e $file ) {
	$new = "\n <small><font color=\"$newcolor\">$newsub</font></small>";
    } elsif ( $mtime > $modi and $mtime <= $secmodi and -e $file ) {
	$new = "\n <small><font color=\"$secnewcolor\">$newsub</font></small>";
    } else {
	$new = "";
    }

# ファイルが存在しないときは次のリンクを探す
#    next GETLINK unless -e $file;

# リンクの重複を削除
    for ( $n = 0; $n < @files; $n++ ) {
	if ( $files[$n] =~ /$file/ and $k < $num[$n] ) {
	    splice ( @files, $n, 1 );
	    splice ( @num, $n, 1 );
	} elsif ( $files[$n] =~ /$file/ and $k >= $num[$n] ) {
	    next GETLINK;
	}
    }

# 配列に一時保存
    if ( $file =~ /$maphtml/ ) {
	push ( @files, "$name ( This Page )<br>\n" );
	push ( @num, $k );

    } elsif ( $file =~ /\.html$/ or $file =~ /\.htm$/ ) {
	push ( @files, qq|<a href="$file">$name</a>$new<br>\n| );
	push ( @num, $k );
	&get_link ( $file, $k );

    } else {
	push ( @files, qq|<a href="$file">$name</a>$new<br>\n| );
	push ( @num, $k );
    }

# GETLINK の終了
}

# sub get_link の終了
}




