#!/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 "/eq_resv/cgi-bin/resv";
}

sub getHTMLDir
{
    return "/eq_resv/html/reserve";
}

# 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 read_rooms
#
#

sub read_rooms
{
    my $end = 0;
    my $count = 0;
    my $refRooms = $_[0];

    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/, $_);
			$refRooms->[$count] = $name;
			$count++;
		    }
		}
	    }
	    if($end){
		last;
	    }
	}
	close(EQUIPS);
    }
}

# sub readRoomDirs
#
#

sub readRoomDirs
{
    my $end = 0;
    my $count = 0;
    my $refRooms = $_[0];

    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/, $_);
                        $refRooms->[$count] = $dir;
                        $count++;
                    }
                }
            }
            if($end){
                last;
            }
        }
        close(EQUIPS);
    }
}

# sub read_equips
#
#

sub read_equips
{
    my $room = $_[0];
    my $count = 0;
    my $end = 0;
    my $refEquips = $_[1];

    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{
			($shortName, $equip) = split(/\t/, $_);
			$refEquips->[$count] = $equip;
			$count++;
		    }
		}
	    }
	    if($end){
		last;
	    }
	}
	close(EQUIPS);
    }
}


# sub readEquipDirs
#
#

sub readEquipDirs
{
    my $room = $_[0];
    my $count = 0;
    my $end = 0;
    my $dir;
    my $name;
    my $refDirs = $_[1];
    my $refNames = $_[2];
    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 get_roomDir
#
#

sub get_roomDir
{
    my $room = $_[0];
    my $refRoomDir = $_[1];
    my $refRoomName = $_[2];
    my $count = 0;
    my $end = 0;
    my $dummy;
    if(!$refRoomName){
	$refRoomName = \$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){
			($$refRoomName, $$refRoomDir) = split(/\t/, $_);
			$end = 1;
			last;
		    }
		    $count++;
		}
	    }
	    if($end){
		last;
	    }
	}
	close(EQUIPS);
    }
}



# sub get_reserveDir
#
#

sub get_reserveDir
{
    my $room = $_[0];
    my $equip = $_[1];
    my $refEquipDir = $_[2];
    my $refEquipName = $_[3];
    my $count = 0;
    my $end = 0;
    my $dirName = 0;
    my $dummy;
    if(!$refEquipName){
	$refEquipName = \$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){
			($$refEquipDir, $$refEquipName) = split(/\t/, $_);
			$end = 1;
			last;
		    }
		    $count++;
		}
	    }
	    if($end){
		last;
	    }
	}
	close(EQUIPS);
    }
    return $end;
}


# sub getLabInfo
#
#

sub getLabInfo
{
    my $phone = $_[0];
    my $refName = $_[1];
    my $refShort = $_[2];
    
    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;
