#!/usr/bin/perl -w

use Socket;
use strict;
my $fh;

my $socketfile = $ARGV[0] || "/var/run/tuer/door_cmd.socket";
sleep(1) while (! -S $socketfile);
my $socketaddr = sockaddr_un($socketfile);

my $keys;
my %good;

open $keys,'/flash/phone';
while (<$keys>)
{
	chomp;
	if ($_ =~ /^(\S+)\s+(.+)$/)
	{
		$good{$1}=$2;
 	}
}
my $id = $ARGV[2];
my $action = $ARGV[1];
if ($good{$id})
{
	if ($action == 1591)
        {
	  send_to_fifo("close Phone ".$good{$id});
        } elsif ($action == 1590) {
	  send_to_fifo("open Phone ".$good{$id});
        } elsif ($action == 1592) {
	  send_to_fifo("reset Phone ".$good{$id});
        } else {
          send_to_fifo("log invalid action $action, but valid Phone $id");
        }
} else {
	send_to_fifo("log invalid Phone $id");
}
exit 0;

sub send_to_fifo
{
	socket(my $conn, PF_UNIX, SOCK_STREAM,0) || die "socket: $!";
	connect($conn, $socketaddr) || die "socket connect: $!";
	print $conn shift(@_)."\n";
	close($conn);
}

