#!/usr/bin/perl -T -I/h/COE/lib -w -U

#- the -U flag is used to overcome insecure warning on mkdir

use strict;
use Coe;

my $VERSION="1.1.4.1";

if (@ARGV == 0) {
	&useage;
} 

sub useage { print "Useage: $0 {--username username} {--fullname fullname} ".
                    "{--uid uid} {--group group} {--password password}" .
                    "{--shell shell} {--home home} {--scope scope}\n";exit 1; }

my $arg1;
my $flag="false";
my $fullname="dummy";
my $home="dummy";
my $group="dummy";
my $other_groups="dummy";
my $password="dummy";
my $scope="local";
my $shell="/bin/csh";
my $uid="dummy";
my $username="dummy";
my $gid;
my $dir;
my $dir_mode;
my $profile;
my $pscope;

my @command;
my @p;

while (@ARGV > 0 ){
	$arg1=cleanString(shift,1);
	if ("$arg1" eq "-h") {
   		print "HELP\n";
   		exit;
	} elsif ("$arg1" eq "-H") {
   		print "HELP\n";
   		exit;
	} elsif ("$arg1" eq "-V") {
   		print "VERSION: $VERSION\n";
   		exit;
	} elsif ("$arg1" eq "--username") {
		$username=cleanString(shift,1);
			print "username: $username\n";
	} elsif ("$arg1" eq "--fullname") {
		$fullname=cleanString(shift,1);
			print "fullname: $fullname\n";
	} elsif ("$arg1" eq "--uid") {
		$uid=cleanString(shift,1);
			print "uid: $uid\n";
	} elsif ("$arg1" eq "--group") {
		$group=cleanString(shift,1);
			print "group: $group\n";
	} elsif ("$arg1" eq "--password") {
                # there are some special chars illegal here
		$password=cleanString(shift,3);
			print "password: $password\n";
	} elsif ("$arg1" eq "--scope") {
		$scope=cleanString(shift,1);
			print "scope: $scope\n";
	} elsif ("$arg1" eq "--shell") {
		$shell=cleanString(shift,3);
			print "shell: $shell\n";
	} elsif ("$arg1" eq "--home") {
		$home=cleanString(shift,3);
			print "home: $home\n";
	} elsif ("$arg1" eq "--other_groups") {
		# comma delimited, no white space
		$other_groups=cleanString(shift,1);
	}
}

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# do simple sanity checks
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

if ("$username" eq "dummy" ||
    "$uid" eq "dummy" ||
    "$group" eq "dummy")
   { &useage; }

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
# define some stuff
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|

if ("$home" eq "dummy") {
	$home=coeFileConv "COE_USER/$scope/$username/Scripts";
}

#if ("$password" eq "dummy") {
#	$password="coe4321";
#}

#if ("$profile" =~ /:/) { 
        #$profile=$_;
	#@p=split(":");
	#$pscope=$p[1];
#}

#- identify the mandatory arguments
if ("$fullname" ne "dummy") { 
	@command=("/usr/bin/sudo", "/usr/sbin/useradd", "-u", $uid, 
		"-g", $group, "-d", $home, "-s", $shell, "-c", $fullname);
} else {
	@command=("/usr/bin/sudo", "/usr/sbin/useradd", "-u", $uid, 
		"-g", $group, "-d", $home, "-s", $shell, "-c", $username);
}

#- add the optional arg and the final arg which is new user name
if ("$other_groups" ne "dummy") {
	$command[12]="-G";
	$command[13]=$other_groups;
	$command[14]=$username;
} else {
	$command[12]=$username;
}

#foreach (<@command>) {print "$_ ";}
#print "\n";

coeRun (@command);

# only make directores for users with shells
if ("$shell" ne "/bin/false" && "$username" ne "") {
	$gid=cleanString(getgrnam($group),3);
	#$gid=$group;
        $dir_mode=0750;

	$dir=coeFileConv "COE_USER/$scope/$username";
	#mkdir ($dir, $dir_mode);
	mkdir ($dir);
	$dir=coeFileConv "COE_USER/$scope/$username/Scripts";
	mkdir ($dir);
	$dir=coeFileConv "COE_USER/$scope/$username/data";
	mkdir ($dir);

	$dir=coeFileConv "COE_USER/$scope/$username";
	coeRun("/usr/bin/sudo", "/bin/chmod", "-R", 
		"750",  "$dir");
	coeRun("/usr/bin/sudo", "/bin/chown", "-R", 
		"$username:$group", "$dir");

}

# set the password
if ("$password" ne "dummy") {
        coeRun("COE_COMP/OS/bin/OS_autopasswd", "$username", "$password");
}