#!/usr/bin/perl
use strict;
my %dnscache;
my %realdnscache;
my $hostname = `/bin/hostname`;
chomp($hostname);
my @p = split('\.',$hostname);
my $ext = pop @p;
my $company = pop @p;
my $shorthost = shift @p;

my $now = time;
my $cmd = "/bin/mv /etc/tgt/targets.conf /etc/tgt/targets.conf.$now";
system($cmd);
$cmd = "/bin/mv /etc/scst.conf /etc/scst.conf.$now";
system($cmd);

my %hash;
open(HASH, "</etc/tgt/hash");
while(<HASH>){
  chomp;
  my @p = split('\s+');
  $hash{$p[0]} = $p[1];
}
close(HASH);

my %mounts;
open(FSTAB, "</etc/fstab");
foreach my $line (<FSTAB>) {
    $mounts{$1} = 1 if ($line =~ m/^LABEL=([\w\d\.]+)\s/);
}

my $backnetip = `/sbin/ip addr show |/bin/grep '172\.16' |/usr/bin/tr '/' ' ' |/bin/awk '{print \$2}'`;
chomp $backnetip;


my $data = {};

#lvm partitions
foreach my $dev(`/bin/ls /dev/volume*/* /dev/backup/* 2>/dev/null`){
  chomp($dev);
  next if $dev =~ m/^\./;
  my @parts = split('/', $dev);
  next unless $parts[3] =~ m/^(\w+)\.(\d+\w{0,3})/;
  my $host = $1;
  my $part = $2;
  if ($mounts{"$host.$part"}) {
    next;
  }
  $data->{$host}->{$dev} = "$host.$part";
}

open(TARGET, ">/etc/tgt/targets.conf");
   
print TARGET "default-driver iscsi\n";

my $disk = 1;
my $diskentries = "";
my $targetentries = "";

foreach my $host (sort keys %$data){
  next unless defined $hash{$host};
  my $ip = getip($host);
  my $pass = $hash{$host};
  my @luns;

  foreach my $part (sort keys %{$data->{$host}}){
    my $shortpart = $data->{$host}->{$part};
      print TARGET "<target iqn.2010-01.$ext.$company.$shorthost:$shortpart>\n";
      print TARGET "\tbacking-store $part\n";
      print TARGET "\tincominguser $host $pass\n";
      print TARGET "\tinitiator-address $ip\n";
      print TARGET "</target>\n";
      $diskentries .= "\tDEVICE $shortpart \{\n";
      $diskentries .= "\t\tfilename ".$part."\n";
      $diskentries .= "\t\tnv_cache 1\n";
      $diskentries .= "\t\}\n";
      push(@luns, $shortpart);
      $disk++;
  }
  $targetentries .= "\tTARGET iqn.2010-01.$ext.$company.$shorthost:$host \{\n";
  my $lunnum = 0;
  foreach my $lun (sort @luns){
      $targetentries .= "\t\tLUN $lunnum $lun\n";
      $lunnum++;
  };
  $targetentries .= "\t\tenabled 1\n";
  $targetentries .= "\t\tallowed_portal $backnetip\n";
  $targetentries .= "\t\tIncomingUser \"$host $pass\"\n";
  $targetentries .= "\t\}\n";
}

close(TARGET);

open(SCST, ">/etc/scst.conf");
print SCST "HANDLER vdisk_fileio \{\n";
print SCST $diskentries;
print SCST "\}\n";
print SCST "TARGET_DRIVER iscsi \{\n";
print SCST "\tenabled 1\n";
print SCST $targetentries;
print SCST "\}";
close SCST;

#print "$diskentries\n$targetentries\n";

sub getip {
  my $host = shift;
  my $ip;
  return $dnscache{$host} if defined($dnscache{$host});
  if($company =~ m/beta/){
    $ip = `/usr/bin/wget --timeout=10 -t 1 -q -O - http://backupmaster.bluehost.com/cgi-bin/newbacknetip?hostname=$host\\&beta=1`;
  }else{
    $ip = `/usr/bin/wget --timeout=10 -t 1 -q -O - http://backupmaster.bluehost.com/cgi-bin/newbacknetip?hostname=$host.$company.$ext`;
  }
  chomp($ip);
  my $ipfromcache = `/bin/grep $host /etc/hostcache |/usr/bin/head -1| /bin/awk '{print \$1}'` if -e "/etc/hostcache";
  return "0.0.0.0" if($ip eq "");
  if($ipfromcache){
    chomp($ipfromcache);
    $ip = $ipfromcache if ($ip eq "");
  }
  unless($ipfromcache){
    `/bin/echo $ip $host >>/etc/hostcache`;
  }
  $dnscache{$host} = $ip;
  return $ip;
}

