#!/usr/bin/perl -w

#------------------------------------------------------------
#This action deletes a group from the Active Directory
#
#Command format:
#
# user-delete-AD event group_name:  
#
#  event       : calling event name
#  group_name  : group name that exists in Active Directory
#
#Copyright 2016 Koozali Foundation, Inc.
#08/17/2016: G.Zartman <gzartman@koozali.org>
#
#The code contained herein can be distributed under the same
#license as Perl
#
#------------------------------------------------------------
package esmith::thisaction;

use strict;
use warnings;
use esmith::ConfigDB;
use esmith::AD::User;

#Pull arguments
my $event       = $ARGV [0] || '';
my $groupName    = $ARGV [1] || '';


my $ad = esmith::AD::User->new;

#Verify a group name was passed to the action and it exists in AD
die "Groupname not found in action arguments\n"
  unless ($groupName);
die "Groupname $groupName does not exist in the Active Directory.\n"
  unless ($ad->doesUserExist($groupName));

##Delete AD User
my $adPassword = $ad->getADPass();
my $deleteGroup = "/usr/bin/samba-tool group delete $groupName " .
                   "-U ad_admin\%$adPassword";

system ($deleteGroup);
die ("Unable to delete group $groupName from Active Directory\n") if ($? == -1);

exit(0);
