#!/usr/bin/perl
use strict;


print "this shouldn't be used anymore!\n";
exit;

use Data::Dumper;
my $DEBUG = 1;
my $RUNCMD = 1;

#get chassis letter
my $chassis = promptUser("what chassis letter are you adding to this backup box?", "a");
$chassis = uc($chassis);

runcmd("partprobe");
print "sleeping for 5 for partprobe to settle\n";
sleep 5;
runcmd('pvscan >/dev/null 2>&1');

#if we have a volgroup by that letter, something must be wrong
my %alreadyallocated;
foreach my $dev (`pvs`){
  print $dev;
  if($dev =~ m/volume$chassis/){
    print "we already have a $chassis setup!\n";
    exit;
  }
  if($dev =~ m#(/dev/sd.)#){
    $alreadyallocated{$1} = 1;
  }
}

#create arrays
runcmd("/bin/create-lsi-backup-arrays");
print "sleeping 10 so things settle properly\n";
sleep 10;

#ignore any drive that has a partition
foreach my $dev (`ls /dev/sd??`){
  chomp $dev;
  $alreadyallocated{substr($dev,0,8)} = 1;
}

#fine out which devices are left that we can use
my @newdev;
foreach my $dev (`ls /dev/sd?`){
  chomp($dev);
  my $partitionfound = 0;
  next if $alreadyallocated{$dev};
  push(@newdev, $dev);
}
sleep 5 if (@newdev > 0);

#nuke partition on the drive if there was one
foreach my $dev(@newdev){
  runcmd("dd if=/dev/zero of=$dev bs=512 count=1");
}
runcmd("partprobe");
print "sleeping 5 for partprobe to settle\n";
sleep 5;

#pvcreate
foreach my $dev(@newdev){
  runcmd("pvcreate $dev");
}

#vgcreate
if(@newdev == 0){
  print "failure?  I don't have any partitions to work with?\n";
  exit;
}

sleep 5;
runcmd("vgcreate volume$chassis ".join(" ", @newdev));


sub runcmd {
  my $cmd = shift;
  print "$cmd\n" if $DEBUG;
  system($cmd) if $RUNCMD;
}

sub promptUser {
   my $promptString = shift;
   my $default = shift;
   print $promptString, " ( $default ) : ";
   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
   chomp;
   $_ = $default if($_ eq "");
   return $_;
}


