#!/bin/sh -e

##########################################################################
#   Script description:
#       Recreate a user but with a new UID/GID
#
#   Arguments:
#       User-name
#       Old password file
#       Old shadow password file
#       Old group file
#       Old pw-age directory
#       Old home directory
#
#   History:
#   Date        Name        Modification
#   2018-11-22  J Bacon     Begin
##########################################################################

usage()
{
    printf "Usage: $0 user-name old-password-file old-shadow-file old-group-file old-pw-age-dir old-home-dir\n"
    exit 1
}


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

if [ $# != 6 ]; then
    usage
fi

auto-root-check $0

user_name=$1
old_password_file=$2
old_shadow_file=$3
old_group_file=$4
old_pw_age_dir=$5
old_dir=$6

if ! fgrep $user_name $old_password_file; then
    printf "$user_name is not in $old_password_file.\n"
    exit 1
fi

# Check for trailing / on old_dir
if [ -d $old_dir ]; then
    save_cwd=$(pwd)
    cd $old_dir
    old_dir=$(pwd)
    cd $save_cwd
else
    printf "$old_dir is not a directory.\n"
    read -p "Continue? y/[n] " continue
    if [ 0$continue != 0y ]; then
	exit
    fi
fi

# Check for existence of all groups
primary_gid=$(awk -F : -v user_name=$user_name '$1 == user_name { print $4 }' $old_password_file)
primary_group=$(awk -F : -v gid=$primary_gid '$3 == gid { print $1 }' $old_group_file)
printf "Primary group was $primary_group.\n"
other_groups=$(awk -F : -v user_name=$user_name '$4 ~ user_name { print $1 }' $old_group_file)
other_gids=$(awk -F : -v user_name=$user_name '$4 ~ user_name { print $3 }' $old_group_file)
echo $old_user_id $old_group_id $other_groups $other_gids
for group in $other_groups; do
    if [ -z $(awk -F : -v group=$group '$1 == group { print $1 }' /etc/group) ]; then
	gid=$(awk -F : -v group=$group '$1 == group { print $3 }' $old_group_file)
	cat << EOM

$user_name is a member of $group, which does not exist.

Files owned by group $gid will not be updated.

EOM
	read -p 'Continue? y/[n] ' continue
	if [ 0$continue != 0y ]; then
	    exit
	fi
    fi
done

# Add new account
if ! awk -F : '{ print $1 }' /etc/passwd | fgrep -w $user_name; then
    NOPW=true
    export NOPW
    cluster-adduser $user_name
else
    printf "User $user_name already exists.\n"
fi

# Restore old password
printf "Restore local password? (Not needed if using LDAP/AD) [y]/n "
read restore_pw
if [ 0$restore_pw != 0n ]; then
    cluster-transfer-pw $user_name $old_shadow_file $old_pw_age_dir
else
    cluster-lock-local-pw $user_name
fi

# Copy files to new home dir
if [ -d $old_dir ]; then
    new_home=$(awk -F : -v user_name=$user_name '$1 == user_name { print $6 }' /etc/passwd)

    # Fix ownership of files
    auto-restore-ownership $user_name $old_password_file $old_group_file $old_dir
    
    mv $old_dir $new_home/Pre-upgrade
fi

cat << EOM

Fixing ownership over NFS will be very slow from the head node for users
with many files on the file servers.

Log into the user's file server(s) and manually run

    auto-change-ownership

and

    auto-change-group-ownership
    
EOM
