#!/bin/bash
SRC=$1
DEST=$2

if [ $# -ne 2 ]; then
        echo "Usage: cloneuser source_user destination_user"
        exit 1
fi
if ! grep -q $SRC /etc/passwd ;then echo "Source user doesn't exist!"; exit 1; fi
if grep -q $DEST /etc/passwd ;then echo "Destination user already exists!"; exit 1; fi

SRC_GROUPS=`id -Gn ${SRC}`
SRC_SHELL=$(awk -F : -v name=${SRC} '(name == $1) { print $7 }' /etc/passwd)
RAND_PW=`date +%s | sha256sum | base64 | head -c 32 ; echo`

echo "Creating user $DEST with a random password"
useradd --shell ${SRC_SHELL} --password ${RAND_PW} --create-home ${DEST}
for grp in ${SRC_GROUPS};do
        if [ "$grp" != "$SRC" ]; then
                echo "Adding user $DEST to group $grp."
                usermod -a -G $grp ${DEST}
        fi
done
echo "Finished. Please set an actual password for $DEST if you want them to be able to log in."




