#! /usr/bin/perl

#check the distro
$cmd = "cat /etc/*release";
@output = `$cmd`;
my $os;
if (grep /Red Hat Enterprise Linux Server release 5\.\d/, @output) {
  $os = "rh5";
} elsif (grep /Red Hat Enterprise Linux Server release 6\.\d/, @output) {
  $os = "rh6";
} elsif (grep /SUSE Linux Enterprise Server 10/, @output) {
  $os = "sles10";
} elsif (grep /SUSE Linux Enterprise Server 11/, @output) {
  $os = "sles11";
} else {
  print "unknow os\n";
  exit 1;
}

$cmd = "uname -p";
my $arch = `$cmd`;
chomp($arch);

print "The build env is: $os-$arch\n";

# check whether the openssl-devel has been isntalled
my $cmd = "rpm -qa | grep openssl";
my @output = `$cmd`;
if ( ! grep /openssl-devel|libopenssl/, @output ) {
  print "Please installed openssl-devel/libopenssl first. (openssl-devel for rh5/sles10; libopenssl for sles11)\n";
  exit 1;
}

# check the source files
my $pwd = `pwd`;
chomp($pwd);
if ( (! -f "$pwd/ipmitool-1.8.11.tar.gz")
  || (! -f "$pwd/ipmitool.spec")
  || (! -f "$pwd/ipmitool-saneretry.patch")
  || (! -f "$pwd/ipmitool-spdfix.patch")
  || (! -f "$pwd/ipmitool-eventfix.patch")) {
  print "missed some necessary files for building.\n";
  exit 1;
}

my $blddir;
if ($os eq "rh5") {
  $blddir = "/usr/src/redhat";
} elsif ($os eq "rh6") {
  $blddir = "/root/rpmbuild";
} elsif ($os =~ /sles1\d/) {
  $blddir = "/usr/src/packages";
}

#&runcmd("mkdir -p $blddir/SOURCES");
#&runcmd("mkdir -p $blddir/SPECS");
#&runcmd("mkdir -p $blddir/BUILD");
#&runcmd("mkdir -p $blddir/RPMS");

# clean the env
$cmd = "rm -rf $blddir/SOURCES/ipmitool*";
&runcmd($cmd);
$cmd = "rm -rf $blddir/SPECS/ipmitool*";
&runcmd($cmd);
$cmd = "rm -rf $blddir/BUILD/ipmitool*";
&runcmd($cmd);
$cmd = "rm -rf $blddir/RPMS/$arch/ipmitool*";
&runcmd($cmd);

# copy the build files
$cmd = "cp -rf ./ipmitool-1.8.11.tar.gz $blddir/SOURCES/";
&runcmd($cmd);

$cmd = "cp -rf ./*.patch $blddir/SOURCES/";
&runcmd($cmd);

$cmd = "cp -rf ./ipmitool.spec $blddir/SPECS/";
&runcmd($cmd);

$cmd = "rpmbuild -bb $blddir/SPECS/ipmitool.spec";
&runcmd($cmd);

#check whether the ssl has been enabled
my $binfile = "$blddir/BUILD/ipmitool-1.8.11/src/ipmitool";
$cmd = "ldd $binfile";
@output = `$cmd`;
if (! grep /libcrypto.so/, @output) {
  print "The ssl was not enabled.\n";
  exit 1;
}

my $objrpm = "$blddir/RPMS/$arch/ipmitool-xcat-1.8.11-3.$arch.rpm";
my $dstdir = "/tmp/build/$os/$arch";

# check the build result
if (! -f $objrpm) {
  print "The rpm file was not generated successfully\n";
  exit 1;
} else {
  $cmd = "mkdir -p $dstdir";
  &runcmd ($cmd);
  $cmd = "cp -rf  $objrpm $dstdir";
  &runcmd ($cmd);
  print "The obj file has been built successfully, you can get it here: $dstdir\n";
  exit 0;
}







sub runcmd () {
  my $cmd = shift;
  print "++++Trying to run command: [$cmd]\n";
  my @output = `$cmd`;
  if ($?) {
    print "A error happened when running the comamnd [$cmd]\n";
    print "error message: @output\n";
    exit 1;
  }
  if ($verbose) {
    print "run cmd message: @output\n";
  }
}
