#!/usr/bin/perl

use utf8;
#use open ":utf8";
use open IN => ":utf8";
use open OUT => ":encoding(Shift_JIS)";

require "filereader.pl";

$outDir = "/home/webmaster/Desktop/outdata";
$fileEquip = "equipments.txt";
$fileLab = "laboratories.txt";
$fileLabOut = "$outDir/laboratories.txt";
$fileNoUse = "$outDir/notused.txt";

@rooms = ();

print "Enter the begining date (year:month) > ";
$date = <STDIN>;
chomp($date);
($startYear, $startMonth) = split(/:/, $date, 2);
if(!$startYear || !$startMonth){
    print "Syntax ERROR!\n";
    exit(0);
}

print "Enter the last date (year:month) > ";
$date = <STDIN>;
chomp($date);
($endYear, $endMonth) = split(/:/, $date, 2);
if(!$endYear || !$endMonth){
    print "Syntax ERROR!\n";
    exit(0);
}

@names = ();
@date = ();
$count = $endMonth + ($endYear - $startYear) * 12 - $startMonth;
$count++;
$y = $startYear;
$m = $startMonth;
for($i = 0; $i < $count; $i++){
    $names[$i] = "$y$m";
    $date[$i] = "$y/$m";
    $m++;
    if($m > 12){
	$y++;
	$m = 1;
    }
}

if(system("mkdir $outDir")){
    system("rm -rf $outDir/*");
}

%labs = ();
$oldname = "";
if(open(LAB, "$fileLab")){
    flock(LAB, 1);
    @lines = <LAB>;
    close(LAB);
    $labCount = @lines;
    $out = "";
    for($i = 0; $i < $labCount; $i++){
	($phone, $name, $short) = split(/\t/, $lines[$i], 3);
	$labs{$phone} = $name;
	if($name ne $oldname){
	    $out .= "$name\n";
	}
	$oldname = $name;
    }
    if(open(LABOUT, "> $fileLabOut")){
	$out .= "不明\n";
	$out =~ s/\n/\r\n/g;
        print LABOUT $out;
        close(LABOUT);
    }
}


filereader::readRoomDirs(\@rooms);
$roomsCnt = @rooms;

if($startMonth < 10){
    $startMonth = '0' . $startMonth;
}
if($endMonth < 10){
    $endMonth = '0' . $endMonth;
}

if(!open(NOUSE, ">$fileNoUse")){
    print "ERROR: Cannot open$fileNoUse\n";
    return 1;
}

for($i = 0; $i < $roomsCnt; $i++){
    @equipDirs = ();
    @equipNames = ();
    filereader::readEquipDirs($i, \@equipDirs, \@equipNames);
    $equipsCnt = @equipDirs;
    for($k = 0; $k < $equipsCnt; $k++){
	$path = "data/$rooms[$i]/$equipDirs[$k]/";
	$outPath = "$outDir/$equipDirs[$k]$startYear$startMonth-$endYear$endMonth.txt";
	$out = "$equipNames[$k]\n";
	$out .= "年/月\t日\t開始時間\t終了時間\t使用時間\t研究室名\t名前\tパスワード\t予約時刻(IPアドレス)\tコメント\n";
	$use = 0;
	for($n = 0; $n < $count; $n++){
	    $filePath = $path . $names[$n] . '.txt';
	    if(open(FILE, $filePath)){
		flock(FILE, 1);
		$use = 1;
#		print "Reading $filePath\n";
		while(<FILE>){
		    ($day, $stime, $etime, $phone, $name, $password, $mainte, $other) = split(/\t/, $_, 8);
		    if($day < 0){
			next;
		    }
		    if($mainte > 0){
			next;
		    }
		    $length = ($etime - $stime) / 2;
		    $stime /= 2;
		    $etime /= 2;
		    if(exists($labs{$phone})){
			$labname = $labs{$phone};
		    }
		    else{
			$labname = "不明";
		    }
		    $out .= "$date[$n]\t$day\t$stime\t$etime\t$length\t$labname\t$name\t$password\t$other";
		}
		close(FILE);
	    }
	}
	if($use){
	    if(open(OUT, ">$outPath")){
		#$out =~ s/\x0A/\x0D\x0A/g;
		#jcode::convert(\$out, 'sjis', 'euc');
		$out =~ s/\n/\r\n/g;
		print OUT $out;
		close(OUT);
	    }
	}
	else{
	    $out = "$equipNames[$k]\r\n";
	    #$out =~ s/\n/\r\n/g;
	    print NOUSE $out;
	}
    }
}

close(NOUSE);
