#!/usr/bin/perl

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

require "filereader.pl";

$outDir = "tmp";
$fileEquip = "equipments.txt";
$fileLab = "laboratories.txt";
$fileLabOut = "$outDir/laboratories.txt";
$fileNoUse = "$outDir/notused.txt";

@rooms = ();

$date = $ARGV[0];
chomp($date);
($startYear, $startMonth) = split(/\//, $date, 2);
if(!$startYear || !$startMonth){
    exit(1);
}

$date = $ARGV[1];
chomp($date);
($endYear, $endMonth) = split(/\//, $date, 2);
if(!$endYear || !$endMonth){
    exit(1);
}

@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]/";
#	$equipNames[$k] =~ s/\s/_/g;
	$equipNames[$k] =~ tr/ ()&\//_（）＆／/;
	system(Encode::encode_utf8("mkdir $outDir/$equipNames[$k]"));
	$use = 0;
	for($n = 0; $n < $count; $n++){
	    $filePath = $path . $names[$n] . '.txt';
	    if(open(FILE, $filePath)){
		flock(FILE, 1);
		$out = "$equipNames[$k]\n";
		$out .= "日付\t開始時間\t終了時間\t使用時間\t研究室名\t名前\tパスワード\t予約時刻(IPアドレス)\tコメント\n";
		$use = 1;
		$outPath = "$outDir/$equipNames[$k]/$names[$n]" . ".txt";
		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 = "??$phone";
		    }
		    $out .= "$day\t$stime\t$etime\t$length\t$labname\t$name\t$password\t$other";
		}
		close(FILE);
		if(open(OUT, ">$outPath")){
		    $out =~ s/\n/\r\n/g;
		    print OUT $out;
		    close(OUT);
		}
	    }
	}
	if(!$use){
	    system(Encode::encode_utf8("rmdir $outDir/$equipNames[$k]"));
	    $out = "$equipNames[$k]\r\n";
	    print NOUSE $out;
	}
    }
}

system("zip -r data$startYear$startMonth-$endYear$endMonth.zip $outDir/*");

close(NOUSE);
