# 
#
# acfsdriverstate.pl
# 
# Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      acfsdriverstate.pl -  report whether or not USM drivers
#                            are installed or loade or
#
#    DESCRIPTION
#      acfsdriverstate installed
#        prints "true" or "false"
#        returns 0 if true, 1 if false
#      acfsdriverstate loaded
#        prints "true" or "false"
#        returns 0 if true, 1 if false
#      acfsdriverstate verion
#        prints the driver versions (if installed)
#      acfsdriverstate supported
#        prints "Supported" of "Not Supported"
#        returns 0 or 1, respectively
#
#    NOTES
#
# 

use strict;
use File::Find;
use Getopt::Std;
use acfslib;
use osds_acfsdriverstate;

sub main
{
  my ($ret);
  my ($state);
  my ($sub_command);
  my ($acfs_remote);
  # user options
  my (%opt);                 # user options hash - used by getopts()
  # user options.
  my (%flags) = ( installed => 'smd',
                  loaded  => 'smd',
                  supported  => 'smd',
                  version  => 's',
                );


  $COMMAND = shift(@ARGV);
  $sub_command = shift(@ARGV);

  if (!defined($sub_command))
  {
    usage();
    exit USM_FAIL;
  }

  if ($sub_command eq "-h")
  {
    usage();
    exit USM_SUCCESS;
  }
  elsif (!(($sub_command eq "installed") || ($sub_command eq "supported") ||
       ($sub_command eq "loaded") ||
       ($sub_command eq "version")))
  {
    #If we are here. This is an invalid option.
    #Removing leading "-" in $sub_command. I'm getting the following error:
    # (Bad argc for usm:acfs-532)
    $sub_command =~ s/^[-]+//;
    if (!($sub_command eq "help"))
    {
      lib_error_print(532, "invalid option: %s", $sub_command);
    }
    usage();
    exit USM_FAIL;
  }

  # parse user options
  %opt=();
  getopts($flags{$sub_command}, \%opt) or usage(), exit USM_FAIL;
  if ($opt{'s'})
  {
    # do not generate console output - default is not silent.
    $SILENT = 1;
  }
  if ($opt{'d'})
  {
      $acfs_remote = "DOMAINSERVICES";
  }
  elsif ($opt{'m'})
  {
      $acfs_remote = "MEMBER";
  }

  if ($sub_command eq "supported")
  {
    if ((lib_usm_supported()) &&
        (osds_usm_compatible($sub_command) == USM_SUPPORTED))
    {
      if(!defined($acfs_remote))
      {
      lib_inform_print_noalert(9200, "Supported");
      $state = USM_SUCCESS;
      }
      elsif (defined($acfs_remote))  
      {
        lib_acfs_remote_supported($acfs_remote);
        if($acfslib::acfsr{'ACFS Remote'} eq 'False')
        {
          lib_inform_print(9840,"ACFS Remote supported: %s","False");  
          $state = USM_FAIL;
        }
        else
        {
          lib_inform_print(9840,"ACFS Remote supported: %s","True");
          lib_inform_print(9841,"iSCSI supported: %s",$acfslib::acfsr{'iSCSI'});
          #lib_inform_print(9842,"xen blkfrnt/blkback supported: %s","True");
          $state = USM_SUCCESS;
        }
      }
    }
    else
    {
      if(!defined($acfs_remote))
      {    
        lib_inform_print_noalert(9201, "Not Supported");
      }
      else
      {
        lib_inform_print_noalert(9840,"ACFS Remote supported: %s","False");  
      }
      $state = USM_FAIL;
    }
    exit $state;
  } 

  $state = USM_FAIL;

  if ($sub_command eq "installed")
  {
    if (lib_check_drivers_installed())
    {
      if(!defined($acfs_remote))
      {    
        $state = USM_SUCCESS;
        print_status($state);
      }
      elsif(defined($acfs_remote))
      {
        lib_acfs_remote_installed($acfs_remote);
        if($acfslib::acfsr{'ACFS Remote'} ne 'True')
        {
          lib_inform_print(9843,"ACFS Remote installed: %s","False");  
          $state = USM_FAIL;
        }
        else
        {
          lib_inform_print(9843,"ACFS Remote installed: %s","True");
          lib_inform_print(9844,"iSCSI installed: %s",$acfslib::acfsr{'iSCSI'});
          $state = USM_SUCCESS;
        }
      }
    }
    else
    {
      if(!defined($acfs_remote))
      {    
        print_status($state);
      }
      else
      {
        lib_inform_print(9843,"ACFS Remote installed: %s","False");  
      }

    }
  }
  elsif ($sub_command eq "loaded")
  {
    if (lib_check_drivers_loaded()) 
    {
      if (!defined($acfs_remote))
      {
        $state = USM_SUCCESS;
        print_status($state);
      }
      elsif(defined($acfs_remote))
      {
        lib_acfs_remote_loaded($acfs_remote);
        if($acfslib::acfsr{'ACFS Remote'} ne 'True')
        {
          lib_inform_print(9846,"ACFS Remote loaded: %s","False");  
          $state = USM_FAIL;
        }
        else
        {
          lib_inform_print(9846,"ACFS Remote loaded: %s","True");
          lib_inform_print(9847,"iSCSI loaded: %s",$acfslib::acfsr{'iSCSI'});
          #lib_inform_print(9848,"xen blkfrnt/blkback loaded: %s","True");
          $state = USM_SUCCESS;
        }
      } # End defined acfs_remote
    } # End drivers loaded
    else
    {
      if(!defined($acfs_remote))
      {    
        print_status($state);
      }
      else
      {
        lib_inform_print(9846,"ACFS Remote loaded: %s","False");  
      }

    }
  } # End subcommand loaded
  elsif ($sub_command eq "version")
  {
    if (lib_check_drivers_installed())
    {
      my ($str) = osds_version();
      print_version($str);
      if (defined($str))
      {
        $state = USM_SUCCESS;
      }
    }
    else
    {
      lib_error_print_noalert(9129, "ADVM/ACFS not installed");
      $state = USM_FAIL;
    }
  } 
  else
  {
    usage();
    $state = USM_FAIL;
  }

  exit $state;
}

# common print routines ensure that all platforms generate the same output
sub print_status()
{
  my ($ret) = shift;

  if ($ret == USM_SUCCESS)
  {
    lib_inform_print_noalert(9203, "true");
  }
  else
  {
    lib_inform_print_noalert(9204, "false");
  }
}

sub print_version()
{
  my ($str) = @_;

  if (!defined($str))
  {
    lib_inform_print_noalert(9129, "ADVM/ACFS not installed");
  }
  else
  {
    my @ver=split(/\//, $str);
    chomp($ver[1]);
    lib_inform_print_noalert(9325, "    Driver OS kernel version = %s.", $ver[0]);
    lib_inform_print_noalert(9326, "    Driver Oracle version = %s.", $ver[1]);
    lib_inform_print_noalert(9212, "    Driver build version = %s.", $ver[2]);   
  }
}

sub usage
{
  lib_error_print_noalert(9211,
    "usage: %s [-h] [-orahome <home_path>] {installed [-d|-m] | loaded [-d|-m] | version | supported [-d|-m] } [-s]", $COMMAND);
  lib_error_print_noalert(9838,"    -m: Member Cluster check");
  lib_error_print_noalert(9839,"    -d: Domain Services Cluster check");
}

main();
