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

use strict;
use File::Find;
use Getopt::Std;
use acfslib;
use okalib;
use osds_okadriverstate;

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

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

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

  if (!(($sub_command eq "installed") || ($sub_command eq "supported") ||
       ($sub_command eq "loaded") ||
       ($sub_command eq "version")))
  {
    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 ($sub_command eq "supported")
  {
    if (lib_oka_supported())
    {
      lib_inform_print_noalert(9200, "Supported");
      $state = USM_SUCCESS;
    }
    else
    {
      lib_inform_print_noalert(9201, "Not Supported");
      $state = USM_FAIL;
    }
    exit $state;
  } 

  $state = USM_FAIL;

  if ($sub_command eq "installed")
  {
    if (lib_check_oka_drivers_installed())
    {
      $state = USM_SUCCESS;
    }
    print_oka_status($state);
  }
  elsif ($sub_command eq "loaded")
  {
    if (lib_check_oka_drivers_loaded())
    {
      $state = USM_SUCCESS;
    }
    print_oka_status($state);
  } 
  elsif ($sub_command eq "version")
  {
    if (lib_check_oka_drivers_installed())
    {
      my ($str) = osds_oka_version();
      print_oka_version($str);
      if (defined($str))
      {
        $state = USM_SUCCESS;
      }
    }
    else
    {
      lib_error_print_noalert(642, "OKA 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_oka_status()
{
  my ($ret) = shift;

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

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

  if (!defined($str))
  {
    lib_inform_print_noalert(642, "OKA 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]);
  }
}

sub usage
{
  lib_error_print_noalert(651,
    "usage: %s {installed | loaded | version | supported} [-s]", $COMMAND);
}

main();
