#!/bin/bash
# info: change user random key
# options: USER [HASH]
#
# The function changes user's RKEY value thats has been used for security value to be used forgot password function only.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
user=$1
hash=$2

echo $rkeyhash;
# Importing system enviroment  as we run this script
# mostly by cron wich not read it by itself
source /etc/profile

# Includes
source $HESTIA/func/main.sh
source $HESTIA/conf/hestia.conf

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'USER [HASH]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

if [ ! -z "$hash" ]; then
    password=$hash
    is_password_valid
    hash=$password;
else
    hash=$(generate_password)
fi
#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

d=$(date +%s)

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Changing RKEY value
update_user_value "$user" '$RKEY' "$hash"

#check if RKEYEXP exists
if [ -z "$(grep RKEYEXP $USER_DATA/user.conf)" ]; then
    sed -i "s/^RKEY/RKEYEXP='$d'\nRKEY/g" $USER_DATA/user.conf
else
    update_user_value "$user" '$RKEYEXP' "$d"
fi

# Logging
log_history "forgot password request"
log_event "$OK" "$ARGUMENTS"

exit
