#!/bin/sh

# Change the "lager" user UID and GID

# Retrieve new ids to apply
USERNAME="lager"
NEWUID=$1
NEWGID=$1
if [ $# -eq 2 ]
then
    NEWGID=$2
elif [ $# -ne 1 ]
then
    echo "Usage: change-uid NEWUID [NEWGID]"
    echo "If NEWGID is not provided, its value will be the same as NEWUID"
    exit 1
fi

# Retrieve old ids
OLDUID=`id -u ${USERNAME}`
OLDGID=`id -g ${USERNAME}`

# Change the user ids
usermod -u ${NEWUID} ${USERNAME}
groupmod -g ${NEWGID} ${USERNAME}

# Change the files ownership
find / -not \( -path /proc -prune \) -user ${OLDUID} -exec chown -h ${NEWUID} {} \;
find / -not \( -path /proc -prune \) -group ${OLDGID} -exec chgrp -h ${NEWGID} {} \;

echo "UID and GID changed from ${OLDUID}:${OLDGID} to ${NEWUID}:${NEWGID} for \"${USERNAME}\""
exit 0
