#!/usr/bin/perl -w

#------------------------------------------------------------
#This action creates the home directory for new users
#
#Command format:
#
# user-create-home event username data:
#
#  event    : calling event name
#  username : username that exists in AD
#
#Copyright 2016 Koozali Foundation, Inc.
#08/19/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::AD::User;
use esmith::util;

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

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

#Sanity Check
die "Username not found in action arguments\n"
  unless ($userName);
die "Username $userName not found in Active Directory.\n"
  unless ($ad->doesUserExist($userName));

my $homeDir = '/home/e-smith/files/users/' . $userName;
system ("mkdir -p $homeDir\/home");
die "Unable to create home directory for $userName\n" if ($? == -1);
chmod 0700, $homeDir;  
chmod 0755, "$homeDir\/home";

esmith::util::chownFile($userName, 'root', $homeDir) ||
    die "Couldn't change ownership of $homeDir\n";
esmith::util::chownFile($userName, 'root', "$homeDir\/home") ||
    die "Couldn't change ownership of $homeDir\n";



exit(0); 
