#!/bin/sh -e

##########################################################################
#   Script description:
#       Change the primary group for a user
#       
#   History:
#   Date        Name        Modification
#   2017-03-31  Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0 username new_group|new-gid [directory ...]\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ `id -un` != 'root' ]; then
    printf "Only root can run $0.\n"
    exit 1
fi

if [ $# -lt 2 ]; then
    usage
fi

user_name=$1
new_group=$2
shift
shift

# Verify that all dirs exits before changing anything!
dirs="$@"
for dir in $dirs; do
    if [ ! -e $dir ]; then
	printf "$0: ${dir}: No such file or directory.\n"
	exit 1
    fi
done

# Verify that user exists
old_gid=`awk -F : -v user_name=$user_name '$1 == user_name { print $4 }' /etc/passwd`
if [ 0$old_gid = 0 ]; then
    printf "$0: $user_name: No such user.\n"
    exit 1
fi

if awk -F : '{ print $1 }' /etc/group | fgrep -q $new_group; then
    new_gid=`awk -F : -v group_name=$new_group '$1 == group_name { print $3 }' /etc/group`
elif awk -F : '{ print $3 }' /etc/group | fgrep -q $new_group; then
    new_gid=$new_group
else
    printf "$0: Error: $new_group is not an existing group.\n"
    exit 1
fi

if [ $old_gid = $new_gid ]; then
    printf "$0: $new_group is already the primary group for $user_name.\n"
    exit
fi

# Verify that new gid exists: Let usermod handle this, or we'll have to
# check for both group name and gid.

# Call me from cluster-change-gid
case $(auto-ostype) in
FreeBSD)
    pw usermod $user_name -g $new_group
    ;;

NetBSD|RHEL)
    usermod -g $new_group $user_name
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac

# Change group ownership on files under specified directories
if [ 0"$dirs" != 0 ]; then
    auto-change-group-ownership $user_name $old_gid $new_group $dirs
fi
