#!/usr/bin/perl
#
# Copyright (c) 2016 Micro Focus.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Micro Focus.
#
# To contact Micro Focus about this file by physical or electronic mail,
# you may find current contact information at www.microfocus.com
#
#  Author:  Micro Focus
#

use strict;
use warnings;
use File::Basename;

if( $ARGV[1] =~ /nss\s+\/pooldeact=(\S+)/ )
{
    my $pool = $1;
    if( open( IN_HANDLE, "<$ARGV[0]" ))
    {
	my $line;
	foreach $line (<IN_HANDLE>)
	{
		#print $line;
		if( $line =~ /^[^\#]*ncpcon\s+dismount/ )
		{
			print "\nManual ncpcon command present, hence skipping the automatic dismount\n";
			exit 0;
		}
		if ( $line =~ /^[^\#]*nss\s+\/volumed(e|ea|eac|eact|eacti|eactiv|eactiva|eactivat|eactivate)=/i )
		{
			print "\nManual volumedeact command present, hence skipping the automatic volume deactivate\n";
			exit 0;
		}
	}
        close( IN_HANDLE );
        #if( $content !~ /^[^\#]*ncpcon\s+dismount/ && $content !~ /^[^\#]*nss\s+\/volumedeact=/ )
        {
            my $filename = fileparse($ARGV[0], "unload");
            my $script = "/var/opt/novell/ncs/${filename}load";
            	
            {
                #print "\nneed to process pool '$pool' from script '$script'.\n";
                if( open( IN_HANDLE, "<$script" ))
                {
                    my $content = join( "", <IN_HANDLE> );
                    close( IN_HANDLE );

                    #print "$content\n";
                    while( $content =~ /nss\s+\/poolact=(\S+)/g )
                    {
                        my $after = $';
                        my $loadpl = $1;

                        while( $loadpl =~ /^\$[\{]?([^\}\s]+)[\}]?/ )
                        {
                            if( $content =~ /$1=[\"\']?([^\"\'\s]+)[\"\']?/ )
                            {
                                $loadpl = $1;
                            }
                            else
                            {
                                last;
                            }
                        }

                        #print "load pool = '$loadpl'\n";
                        if( $pool eq $loadpl )
                        {
                            if( $after =~ /nss\s+\/poolact=(\S+)/ )
                            {
                                $after =$`;
                            }

			my @lines = split /\n/, $after;
			foreach my $line (@lines)
			{
                            if( $line =~ /^\s*exit_on_error\s+ncpcon\s+mount\s+([^\s=]+)=/g )
                            {
                               my $volName = $1;

				#print $volName;
                               while( $volName =~ /^\$[\{]?([^\}\s]+)[\}]?/ )
                               {
                                   if( $content =~ /$1=[\"\']?([^\"\'\s]+)[\"\']?/ )
                                   {
                                       $volName = $1;
                                   }
                                   else
                                   {
                                       last;
                                   }
                               }

                               my $dismount_cmd = "ncpcon dismount $volName; date; nss /volumedeact=$volName; date"; 
                               print "\nDismounting NCP and NSS volume '$volName' ($dismount_cmd).\n";
                               system( $dismount_cmd );
                            }
			}
                        }
                    }
                }
            }
        }
    }
}
