#!/usr/bin/perl

use utf8;
use open ":utf8";

package filereader;

$equips_f = "equipments.txt";
$labs_f = "laboratories.txt";


# getCGIDir and getHTMLDir are functions for portablity
#
#

sub getCGIDir
{
    return "/cgi-bin/test";
}

sub getHTMLDir
{
    return "/test";
}

# sub getTime
#
#

sub getTime
{
    my $refYear = $_[0];
    my $refMonth = $_[1];
    my $refDay = $_[2];
    my $refHour = $_[3];
    my $refMin = $_[4];
    my $refSec = $_[5];

    my ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst);
    ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime;

    if($refYear){
		$$refYear = $year + 1900;
    }
    if($refMonth){
		$$refMonth = $month + 1;
    }
    if($refDay){
		$$refDay = $mday;
    }
    if($refHour){
		$$refHour = $hour;
    }
    if($refMin){
		$$refMin = $min;
    }
    if($refSec){
		$$refSec = $sec;
    }
}

# sub getStrTime
#
#

sub getStrTime
{
    my ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst);
    ($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdst) = localtime;
    $year += 1900;
    $month++;
    return "$year/$month/$mday/$hour:$min:$sec";
}


# sub getRoomList
#
#

sub getRoomList
{
	my $refNames = $_[0];
	my $refDirs = $_[1];
	
    my $end = 0;
    my $count = 0;
	my @dummy;
	my $name;
	my $dir;

	if(!$refNames){
		$refNames = \@dummy;
	}
	if(!$refDirs){
		$refDirs = \@dummy;
	}

    if(open(EQUIPS, $equips_f)){
		flock(EQUIPS, 1);
        while(<EQUIPS>){
            chomp;
            if($_ eq "#rooms_begin"){
                while(<EQUIPS>){
                    chomp;
                    if($_ eq "#rooms_end"){
                        $end = 1;
                        last;
                    }
                    else{
                        ($name, $dir) = split(/\t/, $_);
						$refNames->[$count] = $name;
                        $refDirs->[$count] = $dir;
                        $count++;
                    }
                }
            }
            if($end){
                last;
            }
        }
        close(EQUIPS);
    }
}


# sub getEquipList
#
#

sub getEquipList
{
    my $room = $_[0];
	my $refNames = $_[1];
    my $refDirs = $_[2];
	
    my $count = 0;
    my $end = 0;
    my $dir;
	my $name;
    my @dummy;
    if(!$refDirs){
		$refDirs = \@dummy;
    }
    if(!$refNames){
		$refNames = \@dummy;
    }
    if(open(EQUIPS, $equips_f)){
		flock(EQUIPS, 1);
        while (<EQUIPS>) {
            chomp;
            if($_ eq "#begin=$room"){
				while(<EQUIPS>){
					chomp;
					if($_ eq "#end"){
						$end = 1;
						last;
					}
					else{
						($dir, $name) = split(/\t/, $_);
						$refDirs->[$count] = $dir;
						$refNames->[$count] = $name;
						$count++;
                    }
				}
            }
            if($end){
				last;
            }
		}
        close(EQUIPS);
    }
}


# sub getRoom
#
#

sub getRoom
{
    my $room = $_[0];
	my $refName = $_[1];
    my $refDir = $_[2];
	
    my $count = 0;
    my $end = 0;
    my $dummy;
    if(!$refName){
		$refName = \$dummy;
    }
	if(!$refDir){
		$refDir = \$dummy;
	}

    if (open(EQUIPS, $equips_f)) {
		flock(EQUIPS, 1);
        while(<EQUIPS>){
			chomp;
			if($_ eq "#rooms_begin"){
				while(<EQUIPS>){
					chomp;
					if($_ eq "#rooms_end"){
						$end = 1;
						last;
					}
					elsif($count == $room){
						($$refName, $$refDir) = split(/\t/, $_);
						$end = 1;
						last;
					}
					$count++;
				}
			}
			if($end){
				last;
			}
		}
		close(EQUIPS);
    }
}


# sub getEquip
#
#

sub getEquip
{
    my $room = $_[0];
    my $equip = $_[1];
	my $refName = $_[2];
    my $refDir = $_[3];
	
    my $count = 0;
    my $end = 0;
    my $dirName = 0;
    my $dummy;
    if(!$refName){
		$refName = \$dummy;
    }
	if(!$refDir){
		$refDir = \$dummy;
	}

    if (open(EQUIPS, $equips_f)){
		flock(EQUIPS, 1);
        while (<EQUIPS>){
			chomp;
			if($_ eq "#begin=$room"){
				while(<EQUIPS>){
					chomp;
					if($_ eq "#end"){
						$end = 1;
						last;
					}
					elsif($count == $equip){
						($$refDir, $$refName) = split(/\t/, $_);
						$end = 1;
						last;
					}
					$count++;
				}
			}
			if($end){
				last;
			}
		}
		close(EQUIPS);
    }
    return $end;
}



# getPathList
#
#

sub getPathList
{
	my $refList = $_[0];
	
	my @roomDirs;
	my $name;
	my $dir;
	my $room = 0;
	my $count = 0;

	if(open(EQUIPS, $equips_f)){
		flock(EQUIPS, 1);
		while(<EQUIPS>){
			chomp;
			if($_ eq "#rooms_begin"){
				my $i = 0;
				while(<EQUIPS>){
					chomp;
					if($_ eq "#rooms_end"){
						last;
					}
					else{
						($name, $dir) = split(/\t/, $_);
						$roomDirs[$i] = $dir;
						last;
					}
					$i++;
				}
			}
			elsif($_ eq "#begin=$room"){
				while(<EQUIPS>){
					chomp;
					if($_ eq "#end"){
						last;
					}
					else{
						($dir, $name) = split(/\t/, $_);
						$refList->[$count] = "$roomDirs[$room]/$dir";
						$count++;
                    }
				}
				$room++;
            }
		}
		close(EQUIPS);
	}
}


# sub getLabInfo
#
#

sub getLabInfo
{
    my $phone = $_[0];
    my $refName = $_[1];
    my $refShort = $_[2];

	my ($p, $n, $s);
	
    if(open(LABS, $labs_f)){
		flock(LABS, 1);
		while(<LABS>){
			chomp;
			($p, $n, $s) = split(/\t/, $_, 3);
			if($p == $phone){
				$$refName = $n;
				$$refShort = $s;
				close(LABS);
				return 1;
			}
		}
		close(LABS);
    }
    $$refName = "？" . $phone;
    $$refShort = "？" . $phone;
    return 0;

}

1;
