#!/bin/sh

echo
echo 'Copyright (c) 2009,2010, Delphix, All Rights Reserved.'
echo

# Set the PATH
PATH=${PWD}:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:${PATH}; export PATH

# Function to read a Y/N option from the user
yes_or_no() {
    option=$1
    while [ "${option}" != "Y" ] && [ "${option}" != "N" ] && [ "${option}" != "y" ] && [ "${option}" != "n" ]; do
        echo "Continue ? (Y/N):"
        read option
    done

    if [ "${option}" = "N" ] || [ "${option}" = "n" ]; then
        exit 0
    fi
}

# The current user ID
CURRENT_USER=`id | awk -F'=' '{print $2}' | cut -d'(' -f2 | cut -d')' -f1`

echo
echo "This script will create a local Delphix OS user to be used for managing this host from a Delphix Engine."
echo "The current user \"${CURRENT_USER}\" should have the privilege to create a OS user on this machine."

echo "Continue ? (Y/N):"
read option
yes_or_no ${option}

# Default OS user name
USERNAME="delphix"

echo
echo "Enter the name of the Delphix OS user to be created [default : delphix]:"
read username
if [ "${username}" != "" ]; then
    USERNAME=${username}
fi

echo
echo "The Delphix OS user \"${USERNAME}\" should be part of all OS groups which own Oracle homes (on this host) intended to be used with Delphix."
echo "Please enter the primary and supplementary groups for the user \"${USERNAME}\"."
echo

echo "Primary Group for user \"${USERNAME}\":"
read pgroup
while [ "${pgroup}" = "" ]; do
    echo "Primary Group for user \"${USERNAME}\":"
    read pgroup
done

echo "Supplementary Groups for user \"${USERNAME}\" (comma-separated):"
read sgroups
while [ "${sgroups}" = "" ]; do
    echo "Supplementary Groups for user \"${USERNAME}\" (comma-separated):"
    read sgroups
done

echo
echo "Home directory for the user \"${USERNAME}\":"
read homedir
while [ "${homedir}" = "" ]; do
    echo "Home directory for the user \"${USERNAME}\":"
    read homedir
done

# Validate if the parent directory for the home exists
PARENT_DIR=`dirname ${homedir} 2>/dev/null`
if [ ! -d ${PARENT_DIR} ]; then
    echo "Parent directory \"${PARENT_DIR}\" for the home doesn't exist and will be created."
    echo "Continue ? (Y/N):"
    read option
    yes_or_no ${option}
fi

# When here, we are ready to create the user. Confirm with user
echo
echo "Here are the details of the user to be created:"
echo "Username : ${USERNAME}"
echo "Primary Group : ${pgroup}"
echo "Supplementary Group(s) : ${sgroups}"
echo "Home Directory : ${homedir}"
echo
echo "Continue ? (Y/N):"
read option
yes_or_no ${option}

# Create the home directory
mkdir -p ${homedir}
if [ $? -ne 0 ]; then
    echo "Failed to create home directory \"${homedir}\". Please review errors and retry."
    exit 1
fi

# Fire the command to create the user
useradd -d ${homedir} -g ${pgroup} -G "${sgroups}" ${USERNAME}
if [ $? -ne 0 ]; then
    echo "Failed to create user \"${USERNAME}\". Please review errors and retry."
    exit 1
fi

echo "Created user \"${USERNAME}\"."

# Set the password of the new user
echo
echo "Setting password for user \"${USERNAME}\". Enter the password when prompted."
passwd ${USERNAME}
if [ $? -ne 0 ]; then
    echo "Failed to set password for user \"${USERNAME}\". Please review errors and set the password manually."
    exit 1
fi

echo "Set password for user \"${USERNAME}\"."

# Attempt to do a SSH to localhost with the new user. The aim is to catch stuff
# like expired passwords for new users on certain platforms like AIX
echo
echo "Attempting to ssh to localhost using the new user \"${USERNAME}\"..."
ssh ${USERNAME}@127.0.0.1 << __EOF__
exit
__EOF__
if [ $? -ne 0 ]; then
    echo "Failed to login to localhost as user \"${USERNAME}\". Please review errors and fix them manually."
    exit 1
fi

echo "Creating \"toolkit\" sub-directory within \"${homedir}\"..."
mkdir ${homedir}/toolkit
if [ $? -ne 0 ]; then
    echo "\"mkdir ${homedir}/toolkit\" failed; aborting..."
    exit 1
fi

echo "Setting permissions on \"toolkit\" sub-directory within \"${homedir}\"..."
chmod 770 ${homedir}/toolkit
if [ $? -ne 0 ]; then
    echo "\"chmod 770 ${homedir}/toolkit\" failed; aborting..."
    exit 1
fi

echo "Setting permissions on \"${homedir}\" home directory for \"${USERNAME}\"..."
chmod 755 ${homedir}
if [ $? -ne 0 ]; then
    echo "\"chmod 755 ${homedir}\" failed; aborting..."
    exit 1
fi

echo "Setting ownership of \"${homedir}\" home directory to \"${USERNAME}\" account and primary group \"${pgroup}\"..."
chown -R ${USERNAME}:${pgroup} ${homedir}
if [ $? -ne 0 ]; then
    echo "\"chown -R ${USERNAME}:${pgroup} ${homedir}\" failed; aborting..."
    exit 1
fi

exit 0
