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

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 $chownstr;
my $flag="false";
my $fullname="dummy";
my $gid="dummy";
my $group="dummy";
my $home="dummy";
my $myargs;
my $other_groups="dummy";
my $password="dummy";
my $path;
my $pscope="local";
my $profile="local";
my $scope="local";
my $shell="/bin/csh";
my $uid="dummy";
my $username="dummy";
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);
	} elsif ("$arg1" eq "--fullname") {
		$fullname=cleanString(shift,1);
	} elsif ("$arg1" eq "--uid") {
		$uid=cleanString(shift,1);
	} elsif ("$arg1" eq "--group") {
		$group=cleanString(shift,1);
	} elsif ("$arg1" eq "--password") {
		$password=cleanString(shift,1);
	} elsif ("$arg1" eq "--scope") {
		$scope=cleanString(shift,1);
	} elsif ("$arg1" eq "--shell") {
		$shell=cleanString(shift,3);
	} elsif ("$arg1" eq "--home") {
		$home=cleanString(shift,3);
	} 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];
}

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

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); 

#20020429rb: carried over from OS_add_user
# only make directores for users with shells
#if ("$shell" ne "/bin/false") {
	#$gid=getgrnam($group);
	#$gid=$group;
        #$dir_mode=0750;

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

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

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