#!/bin/bash
# info: delete user package
# options: PACKAGE
#
# The function for deleting user package. It does not allow to delete package
# if it is in use.


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

# Argument definition
package=$1

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

# Functions
is_package_in_use() {
    check_package=$(grep "PACKAGE='$package'" $USER_DATA/*/user.conf)
    if [ ! -z "$check_package" ]; then
        echo "Error: package $package is in use"
        log_event "$E_INUSE" "$ARGUMENTS"
        exit $E_INUSE
    fi
}

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

check_args '1' "$#" 'PACKAGE'
is_format_valid 'package'
is_package_valid
is_package_in_use

# Perform verification if read-only mode is enabled
check_hestia_demo_mode


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

# Deleting user package
rm -f $HESTIA/data/packages/$package.pkg


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

log_history "deleted user package $package" '' 'admin'
log_event "$OK" "$ARGUMENTS"

exit
