#!/bin/bash
#
#
show_help () {
  echo;
  echo "Usage: `basename $0` <options>";
  echo;
  echo "Options:";
  echo " -h | --help   = Help, show this message.";
  echo " -w | --warewulf-sync   = Enable WareWulf sync for passwd|shadow|groups files.";
  echo " -S | --sacct-add   = Add user to Slurm account.";
  echo " -s | --shell=<SHELL_PATH>   = Create user's login shell (System shell will be the by default).";
  echo " -m | --create-home   = Create the user's home directory.";
  echo " -M | --no-create-home   = Do not create the user's home directory.";
  echo " -d | --home-dir=<home_dir_path>   = The new user will be created using HOME_DIR as the \"/home/<USER_NAME>\" for the user's login directory.";
  echo " -u | --user=<USER_NAME>   = Username (Mandatory).";
  echo " -c | --comment=\"John Doh\"   = Generally the users full name (Mandatory).";
  echo " -U | --user-id   = Enale to store user_id as variable.";
  echo " -D | --debug   = Show what would be changed.";
}

OPTIONS=`getopt -o hwSs::mMd::u:c:UD --longoptions help,warewulf-sync,sacct-add,shell::,create-home,no-create-home,home-dir::,user:,comment:,user-id,debug -n 'user_create' -- "$@"`

echo "option: $OPTIONS"
echo;
eval set -- "$OPTIONS"

while true ; do
  case "$1" in
    -h | --help ) show_help; exit 0; shift ;;
    -w | --warewulf-sync ) wwsh_resync=true; shift ;;
    -S | --sacct-add ) sacctmgr_add=true; shift ;;
    -s | --shell )
        case "$2" in
          "" ) echo "caseNoEcho shell: $shell"; shift 2;;
          * ) shell=$1; echo "caseEcho shell: $shell"; shift 2;;
        esac;;
    -m | --create-home ) create_home=true; shift ;;
    -M | --no-create-home ) no_create_home=true; shift ;;
    +d | --home-dir )
        case "$2" in
          "" ) shift 2 ;;
          * ) home_dir=$2; echo $home_dir; shift 2 ;;
        esac ;;
    -u | --user )
        case "$2" in
          "" ) echo "--user must need to write a name." ; shift 2 ;;
          * ) user=$2; shift 2 ;;
        esac;;
    -c | --comment )
        case "$2" in
          "" ) echo "--user must need to write a full name." ; shift 2 ;;
          * ) comment=$2; shift 2 ;;
        esac;;
    -U | --user-id ) user_id=true; shift ;;
    -D | --debug ) debug=true; shift ;;
    -- ) shift ; break ;;
    * ) echo "Invalid arguments!"; show_help; exit 1 ;;
  esac
done

# Raise error if not USER_NAME
if [[ ! "$user" ]]; then
    show_help
    exit 1
fi

# Raise error if not USER_FULL_NAME
if [[ ! "$comment" ]]; then
  show_help
  exit 1
fi

# To create LOGIN_SHELL by given path
if [[ ! -z "${shell}" ]]; then
  shellPath="-s \"$shell\""
fi

# To create HOME_DIR
if [ "$create_home" == "true" ] && [[ ! "$no_create_home" ]]; then
  # For defined HOME_DIR_PATH
  if [[ ! -z "${home_dir}" ]]; then
    home_dir_path="-m -d \"$home_dir\""
  fi
# To omit HOME_DIR
elif [[ ! "$create_home" ]] && [ "$no_create_home" == "true" ]; then
  home_dir_path='-M'
# To set default settings
elif [[ -z ${create_home} ]] && [[ -z ${no_create_home} ]]; then
  home_dir_path=''
# Raise error
else
  echo "You can only select either --create-home or --no-create-home"
  show_help
  exit 1
fi

# To execute USERADD command
# Defined LOGIN_SHELL with Defined HOME_DIR
#if [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" == *"-d"* ]]; then
#echo useradd $home_dir_path -U $shellPath -c "$comment" $user
#useradd $home_dir_path -U $shellPath -c "$comment" $user

# Defined LOGIN_SHELL with NO HOME_DIR
#elif [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" == *"-M"* ]]; then
#  echo useradd $home_dir_path -U $shellPath -c "$comment" $user
#  useradd $home_dir_path -U $shellPath -c "$comment" $user

# Defined LOGIN_SHELL with Default HOME_DIR
#elif [[ "$shellPath" == *"-s"* ]] && [[ "$home_dir_path" = true ]]; then
#  echo useradd -U $shellPath -c "$comment" $user
#  useradd -U $shellPath -c "$comment" $user

# Default LOGIN_SHELL with Defined HOME_DIR
#elif [[ "$shellPath" == true]] && [[ "$home_dir_path" == *"-d"* ]]; then
#  echo useradd $home_dir_path -U -c "$comment" $user
#  useradd $home_dir_path -U -c "$comment" $user

# Default LOGIN_SHELL with NO HOME_DIR
#elif [[ "$shellPath" == true]] && [[ "$home_dir_path" == *"-M"* ]]; then
#  echo useradd $home_dir_path -U -c "$comment" $user
#  useradd $home_dir_path -U -c "$comment" $user

# Default LOGIN_SHELL and HOME_DIR
#else
#  echo useradd -U -c "$comment" $user
#  useradd -U -c "$comment" $user
#fi

# To do WareWulf sync AND add user to Slurm account
if [ $wwsh_resync ] && [ $sacctmgr_add ]; then
  echo useradd $home_dir_path -U $shellPath -c "$comment" $user
  useradd $home_dir_path -U $shellPath -c "$comment" $user
  wwsh file resync passwd group shadow
  if [ $debug ]; then
    echo "--> WareWulf Entry Completed."
  fi
  sacctmgr -i add user name=$user account="xcbc-users"
  if [ $debug ]; then
    echo "--> Slurm Entry Completed."
  fi
# To do Slurm account update
elif [ -z ${wwsh_resync} ] && [ $sacctmgr_add ]; then
  echo useradd $home_dir_path -U $shellPath -c "$comment" $user
  useradd $home_dir_path -U $shellPath -c "$comment" $user
  sacctmgr -i add user name=$user account="xcbc-users"
  if [ $debug ]; then
    echo "--> Slurm Entry Completed."
  fi
# To do WareWulf sync
elif [ $wwsh_resync ] && [ -z ${sacctmgr_add} ]; then
  echo useradd $home_dir_path -U $shellPath -c "$comment" $user
  useradd $home_dir_path -U $shellPath -c "$comment" $user
  wwsh file resync passwd group shadow
  if [ $debug ]; then
    echo "--> WareWulf Entry Completed."
  fi
else
  echo useradd $home_dir_path -U $shellPath -c "$comment" $user
  useradd $home_dir_path -U $shellPath -c "$comment" $user
fi

#To get UID and GID of given USER_NAME
if [ $user_id ]; then
  uid=`id -u "$user"`
  gid=`id -g "$user"`
  echo $uid
  echo $gid
fi