#!/bin/bash
# info: adding user favourites
# options: USER SYSTEM OBJECT
#
# The function adds object to users favourites


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

# Argument definition
user=$1
system=$(echo "$2" |tr '[:lower:]' '[:upper:]')
object=$3
email=$3
id=$3

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf


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

check_args '3' "$#" 'USER SYSTEM OBJECT'
is_format_valid 'user' 'system'
case $system in 
    MAIL_ACC)   is_format_valid 'email' ;;
    CRON)       is_format_valid 'id' ;;
    DNS_REC)    is_format_valid 'id' ;;
    *)          is_format_valid 'object'
esac
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

# Checking system
case $system in
    USER)       check='ok' ;;
    WEB)        check='ok' ;;
    DNS)        check='ok' ;;
    DNS_REC)    check='ok' ;;
    MAIL)       check='ok' ;;
    MAIL_ACC)   check='ok' ;;
    DB)         check='ok' ;;
    CRON)       check='ok' ;;
    BACKUP)     check='ok' ;;
    IP)         check='ok' ;;
    PACKAGE)    check='ok' ;;
    FIREWALL)   check='ok' ;;
    *)      check_args '2' '0' 'USER SYSTEM OBJECT'
esac


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Flushing vars
USER=''
WEB=''
DNS=''
DNS_REC=''
MAIL=''
MAIL_ACC=''
DB=''
CRON=''
BACKUP=''
IP=''
PACKAGE=''
FIREWALL=''

# Creating config just in case
touch $USER_DATA/favourites.conf

# Reading current values
source $USER_DATA/favourites.conf

# Assigning current system value
eval value=\$$system

# Checking if object is new
check_fav=$(echo "$value" |tr ',' '\n'| grep "^$object$")
if [ ! -z "$check_fav" ]; then
    exit 0
fi

# Adding object to favorites
if [ -z "$value" ]; then
    value="$object"
else
    value="$value,$object"
fi

# Updating sytem
eval $system=$value

# Updating user favorites
echo "USER='$USER'
WEB='$WEB'
DNS='$DNS'
DNS_REC='$DNS_REC'
MAIL='$MAIL'
MAIL_ACC='$MAIL_ACC'
DB='$DB'
CRON='$CRON'
BACKUP='$BACKUP'
IP='$IP'
PACKAGE='$PACKAGE'
FIREWALL='$FIREWALL'" > $USER_DATA/favourites.conf

# Changing file permission
chmod 640 $USER_DATA/favourites.conf

#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Logging
log_history "added starred $object in $system listing"
log_event "$OK" "$ARGUMENTS"

exit
