#!/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 $VESTA/func/main.sh
source $VESTA/conf/vesta.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


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

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


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

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

exit