#!/bin/sh -e

##########################################################################
#   Script description:
#       Change 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 [ $# -lt 2 ]; then
    usage
fi

auto-root-check $0

if ! cluster-check-cron-updates; then
    exit 0
fi

LOCALBASE=$(cluster-localbase)

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

auto-change-primary-group $user_name $new_group $dirs
cluster-run "auto-change-primary-group $user_name $new_group" backup io vis compute
chgrp $new_group $LOCALBASE/etc/spcm/pw-age/$user_name
