#!/usr/local/bin/perl
#
# Another Jungle XML-RPC, of course, this is not for production ;-)
#
# Copyright (C) 2005 Alexandre Dulaunoy <adulau@foo.be>
#
# This program is  free software; you can redistribute  it and/or modify
# it under the  terms of the GNU General Public  License as published by
# the Free Software Foundation; either  version 2 of the License, or (at
# your option) any later version.
#
# This program  is distributed in the  hope that it will  be useful, but
# WITHOUT   ANY  WARRANTY;   without  even   the  implied   warranty  of
# MERCHANTABILITY  or FITNESS  FOR A  PARTICULAR PURPOSE.   See  the GNU
# General Public License for more details.
#
# You  should have received  a copy  of the  GNU General  Public License
# along  with  this  program;  if   not,  write  to  the  Free  Software
# Foundation, Inc., 59 Temple Place  - Suite 330, Boston, MA 02111-1307,
# USA.
#

use strict;
use RPC::XML::Server;
use POSIX;

my $VERSION = "0.0.1";
my $WORKDIR = ".jungle";

$| = 1;

my $method_JungleEcho = RPC::XML::Procedure->new (
  {
    name => 'Jungle.Echo',
    code => \&JungleEcho,
    signature => ['string string']
  }
);

my $method_JungleService = RPC::XML::Procedure->new (
  {
    name => 'Jungle.CheckService',
    code => \&CheckService,
    signature => ['string string']
  }
);

my $method_JungleRequest = RPC::XML::Procedure->new (
  {
    name => 'Jungle.GetFlag',
    code => \&GetFlag,
    signature => ['string string']
  }
);


my $srv = RPC::XML::Server->new(
  host => '127.0.0.1',
  port => '8080'
);


$srv->add_method($method_JungleEcho);
$srv->add_method($method_JungleRequest);
$srv->add_method($method_JungleService);

$srv->server_loop;

sub JungleEcho {
  my $data = shift;
  return $data;
}

sub CheckService {
  my $data = shift;
  my $result = `ps auxwww | grep $data`;
  return $result;
}

sub GetFlag {
  my $data = shift;
  my $result;

  open (FLAG, "$WORKDIR/$data");
  while (<FLAG>) {
	$result .= $_;
  }
  close (FLAG);
  return $result;
}

