#!/usr/bin/perl
use strict;
use DBI;
use CGI;
use YAML qw(Dump LoadFile);
use Data::Dumper;
my $cgi = new CGI;
print $cgi->header('text/yaml');
my %var = $cgi->Vars;
$Data::Dumper::Sortkeys = 1;


my $remote_ip = $ENV{REMOTE_ADDR};
if( $ENV{REMOTE_ADDR} eq '127.0.0.1' || $ENV{REMOTE_ADDR} eq '74.220.198.194' || -t STDOUT ) {
  $remote_ip = $var{ip} if defined ($var{ip});
}

my $config = LoadFile("/var/www/conf/vpsbackup_master_config.yaml") || die "Failed to load config: $!\n";

#print "remote_ip: $remote_ip <br>\n";

my $data = {};

my $dbh = DBI->connect("DBI:mysql:database=".$config->{vps_backup_db}.";host=localhost",$config->{vps_backup_user}, $config->{vps_backup_pass}) or die("couldnt connect to db $!");

my $query = "select id,hostname,inet_ntoa(publicip) as publicip ,inet_ntoa(privateip) as privateip, type, publickey from backup where publicip = inet_aton(?) or privateip = inet_aton(?);";
my $gwh = $dbh->prepare($query);

$gwh->execute($remote_ip,$remote_ip) || die("Error:".$gwh->errstr."\n");
$data->{self} = $gwh->fetchrow_hashref();
$gwh->finish;
if( ! defined($data->{self}->{id})){
#  print "I have no data for you\n";
  print Dump({data => 'There is none'});
  exit;
}

if($data->{self}->{type} ne 'block'){
  my $bmh = $dbh->prepare(" SELECT backupmap.gateway_port,
                                 gateway.hostname AS gatewayhostname,
                                 inet_ntoa(gateway.publicip) AS gatewaypublicip,
                                 vps.hostname as vpshostname,
                                 vps.size as vpssize,
                                 vps.type as vpstype,
                                 inet_ntoa(vps.privateip) as vpsprivateip
                          FROM backupmap
                          LEFT JOIN vps ON backupmap.vps_id = vps.id
                          LEFT JOIN gateway ON backupmap.gateway_id = gateway.id
                          WHERE backupmap.gateway_id = ?");

  $bmh->execute($data->{self}->{id}) || die("Error:".$bmh->errstr."\n");
  while( my $ref = $bmh->fetchrow_hashref()){
    push(@{$data->{backupmap}},$ref);
  }
  $bmh->finish;
}else{
  my $bmh = $dbh->prepare('SELECT        i.id as instance_id,
              v.id+2000 AS gateway_port,
              "backupgw1.silo1.provo1.unifiedlayer.com" AS gatewayhostname,
              "50.87.240.250" AS gatewaypublicip,
              v.hostname AS vpshostname,
              inet_ntoa(v.privateip) AS vpsprivateip,
              i.uuid AS instanceuuid,
              i.brand AS  instancebrand,
              inet_ntoa(i.primary_ip) AS primaryip,
              i.hal_status AS status
                          FROM instance i
                          LEFT JOIN vps v ON i.vps_id = v.id
                          WHERE i.backup_id = ?;');
  my $vh = $dbh->prepare('SELECT        v.disk_size AS disk_size,
              v.hal_id AS hal_vol_id,
              v.hal_instance_id AS hal_server_id,
              s.hostname AS sanhostname,
              s.id + 3000 AS gateway_port,
              inet_ntoa(s.ip) AS sanprivateip
                          FROM volume v
                          LEFT JOIN san s on v.san_id = s.id
                          WHERE v.instance_id = ?');

  $bmh->execute($data->{self}->{id}) || die("Error:".$bmh->errstr."\n");
  while( my $ref = $bmh->fetchrow_hashref()){
     $vh->execute($ref->{instance_id}) || die("Error: ".$vh->errstr."\n");
     while( my $vref = $vh->fetchrow_hashref()){
       push(@{$ref->{volumes}},$vref);
     }
     $vh->finish;
    push(@{$data->{instances}},$ref);
  }
  $bmh->finish;
}

print Dump($data);



