#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
use File::Basename;

#=============================================================================================
# メイン
#=============================================================================================
my $search;

$search = $ARGV[1];

print "dir : ", $ARGV[0],"\t", "search : ", $search, "\n";

find(\&file_process, $ARGV[0]);


#=============================================================================================
# ファイル毎の処理 処理するファイルは，*.HTML *.PHP
##	open(IN, $test_file) or die "IN file can not be opened. file: $file\n";
#=============================================================================================
sub file_process{

    my $file;
    my ($base, $dir, $ext);
    my $test_file;
    my $write_file_name;
    my $line;

    $file = $File::Find::name;                           # ファイル名の取得
    ($base, $dir, $ext) = fileparse($file, qr/\..+$/);   #　ファイル名, ディレクトリー, 拡張子
    $test_file = $base.$ext;
    $write_file_name = 0;

    if($ext eq ".html" || $ext eq ".HTML" || $ext eq ".PHP" || $ext eq ".php"){
	if(!open(IN, $test_file)){
	    printf "IN file can not be opened. file: %s\n", $file;
	    return;
	}

	$line=0;

	while(<IN>){
	    chomp;
	    $line++;
	    if(/$search/){
		if($write_file_name == 0){
		    printf("%s\n", $file);
		    $write_file_name = 1;
		}
		printf("\t%04d\t%s\n", $line, $_);
	    }
	    
	}
	if($write_file_name == 1){
	    printf("\n");
	}
    }
}
