#!/bin/sh -e

##########################################################################
#   Script description:
#       Sync users from head node
#       
#       FIXME: Factor out OS dependencies to auto-admin
#
#   History:
#   Date        Name        Modification
#   2014-03-26  root        Begin
##########################################################################

usage()
{
    printf "Usage: $0 node username [username ...]\n"
    exit 1
}


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

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

node=$1
shift

LOCALBASE=$(cluster-localbase)

for user_name in $@; do
    # Get user info from /etc/passwd
    # uid, gid, comment, home, shell
    user_id=`awk -F : -v user_name=$user_name '$1 == user_name { print $3 }' /etc/passwd`
    group_id=`awk -F : -v user_name=$user_name '$1 == user_name { print $4 }' /etc/passwd`
    gecos=`awk -F : -v user_name=$user_name '$1 == user_name { print $5 }' /etc/passwd`
    home=`awk -F : -v user_name=$user_name '$1 == user_name { print $6 }' /etc/passwd`
    shell=`awk -F : -v user_name=$user_name '$1 == user_name { print $7 }' /etc/passwd`
    #printf "$user_id $group_id $gecos $home $shell\n"
    
    # Create group first if necessary
    group_name=`awk -F : -v group_id=$group_id '$3 == group_id { print $1 }' /etc/group`
    #printf "$group_name\n"
    #printf "$node ${user_name}:$group_name...\n"
    
    case `auto-ostype` in
    RHEL)
	groups="`groups $user_name | awk -F ': ' '{print $2}' | cut -s -d ' ' -f 2- | tr ' ' ','`"
	;;
    
    FreeBSD)
	groups="`groups $user_name | cut -s -d ' ' -f 2- | tr ' ' ','`"
	;;
    
    *)
	printf "$0: Not supported on $(auto-ostype).\n"
	exit 1
	;;

    esac
    
    remote_os=`ssh $node auto-ostype`
    case $remote_os in
    RHEL)
	# -f = return success even if the group already exists
	ssh $node groupadd -f -g $group_id $group_name
    
	# Create user
	if [ -z "$groups" ]; then
	    ssh $node useradd -c "\"$gecos\"" -d $home -g $group_id -M \
		-s $shell -u $user_id $user_name
	else
	    # Sync supplementary groups first
	    node-sync-groups $node $groups
	    ssh $node useradd -c "\"$gecos\"" -d $home -g $group_id -M \
		-s $shell -u $user_id -G "$groups" $user_name
	fi
	;;
    
    FreeBSD)
	if ! ssh $node pw groupadd $group_name -g $group_id; then
	    printf 'Group add failed.\n'
	fi
	
	remote_shell=$shell
	case $shell in
	/bin/sh|/bin/csh|/bin/tcsh)
	    ;;
	*)
	    if echo $shell | grep '^/bin/'; then
		remote_shell=${LOCALBASE}$shell
	    fi
	    ;;
	esac
	
	# Create user
	if [ -z "$groups" ]; then
	    ssh $node pw useradd $user_name -c "\"$gecos\"" -d $home -m -g \
		$group_id -s $remote_shell -u $user_id
	else
	    # Sync supplementary groups first
	    node-sync-groups $node $groups
	    ssh $node pw useradd $user_name -c "\"$gecos\"" -d $home -m -g \
		$group_id -s $remote_shell -u $user_id -G "$groups"
	fi
	;;
    
    *)
	printf "$0: Not supported on $remote_os.\n"
	exit 1
	;;

    esac
done
