#!/usr/local/bin/perl
# 
#
# acfshanfs.pl
# 
# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      acfshanfs.pl - addnode/uninstall/support for NFSv4 Locking
#
#    DESCRIPTION
#      acfshanfs addnode
#                {-nfsv4lock} {-volume <enabled volume device to use>} [-force]
#          Enable the installation of nfsv4 locking in the current node
#
#      acfshanfs uninstall {-nfsv4lock}
#          Shutdown HANFS in the node
#
#      acfshanfs supported {-nfsv4lock}
#          Report whether NFSv4 locking is supported.
#
#      acfshanfs installed {-nfsv4lock}
#          Report whether NFSv4 locking is installed.
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#

use strict;
use File::Find;
use Getopt::Long;
Getopt::Long::Configure(qw(posix_default auto_abbrev no_ignore_case));
use acfslib;
use osds_acfslib;
use osds_acfshanfs;
use osds_acfsroot;

sub main
{
  my ($ret);
  my ($state);
  my ($sub_command);
  # flag options
  my $nfsv4lock = 0;
  my $volume    = "";
  my $force     = 0;
  my $help      = 0;
  # user options
  my (%opt);                 # user options hash - used by GetOptions()
  # user options.
  my (%flags) = ( addnode       => {"nfsv4lock"     => \$nfsv4lock,
                                    "volume=s"      => \$volume,
                                    "force"         => \$force,
                                    "help"          => \$help},
                  uninstall     => {"nfsv4lock"     => \$nfsv4lock,
                                    "help"          => \$help},
                  supported     => {"nfsv4lock"     => \$nfsv4lock,
                                    "help"          => \$help},
                  installed     => {"nfsv4lock"     => \$nfsv4lock,
                                    "help"          => \$help}
                );

  $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 "addnode") || 
           ($sub_command eq "uninstall") ||
           ($sub_command eq "supported") ||
           ($sub_command eq "installed")))
  {
    $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=();
  GetOptions(\%opt, %{$flags{$sub_command}}) or usage(), exit USM_FAIL;

  if ($help)
  {
    usage();
    exit USM_SUCCESS;
  }
  # For now, this option is mandatory
  elsif (!$nfsv4lock)
  {
    usage();
    exit USM_FAIL;
  }

  my $supported = lib_usm_supported();
  my $type = lib_osds_get_os_type();
  $state = USM_FAIL;

  if ($sub_command eq "supported")
  {
    if ($supported &&
        machine_is_RH() &&
        ($type =~ /EL6/ || $type =~ /EL7/))
    {
      lib_inform_print_noalert(9200, "Supported");
      $state = USM_SUCCESS;
    }
    else
    {
      lib_inform_print_noalert(9201, "Not Supported");
      $state = USM_FAIL;
    }
    exit $state;
  } 
  elsif ($sub_command eq "installed")
  {
    if ($supported &&
        lib_check_drivers_loaded() &&
        lib_check_nfs_path())
    {
      $state = USM_SUCCESS;
    }
    print_status($state);
    exit $state;
  }

  # verify root access
  if (!lib_am_root())
  {
    lib_error_print(9130, "Root access required");
    exit USM_FAIL;
  }

  if ($sub_command eq "addnode")
  {
    if (!$volume)
    {
      usage();
      exit USM_FAIL;
    }
    if ($supported &&
        lib_check_drivers_loaded() &&
        (lib_install_nfs_path($force, $volume) == USM_SUCCESS))
    {
      $state = USM_SUCCESS;
    }
    print_status($state);
  } 
  elsif ($sub_command eq "uninstall")
  {
    if (lib_check_drivers_loaded() &&
        (lib_uninstall_nfs_path() == USM_SUCCESS))
    {
      $state = USM_SUCCESS;
    }
    print_status($state);
  } 
  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 usage
{
  lib_error_print_noalert(9600, 
    "Usage: %s [-h] {-nfsv4lock} {-volume <enabled_volume_device>} [-force]", $COMMAND." addnode");
  lib_error_print_noalert(9601,"Usage: %s [-h] {-nfsv4lock}", $COMMAND." uninstall");
  lib_error_print_noalert(9601,"Usage: %s [-h] {-nfsv4lock}", $COMMAND." supported");
  lib_error_print_noalert(9601,"Usage: %s [-h] {-nfsv4lock}", $COMMAND." installed");
}

main();
