252 lines
5.3 KiB
Bash
Executable File
252 lines
5.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# mtsc-common | MatterLinux common script functions
|
|
# MatterLinux 2023-2024 (https://matterlinux.xyz)
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
$(return 0 2>/dev/null)
|
|
|
|
if [ $? -eq 1 ]; then
|
|
echo "This script contains common functions for MatterLinux tools/scripts"
|
|
echo "You are not supposed to directly run it, now exiting to prevent any errors"
|
|
exit 1
|
|
fi
|
|
|
|
#############
|
|
## version ##
|
|
#############
|
|
MTSC_VERSION="MTSC_VERSION_HOLDER"
|
|
|
|
############################
|
|
## logging functions/vars ##
|
|
############################
|
|
BOLD="\e[1m"
|
|
RESET="\e[0m"
|
|
YELLOW="\e[33m"
|
|
GREEN="\e[32m"
|
|
BLUE="\e[34m"
|
|
GRAY="\e[37m"
|
|
RED="\e[31m"
|
|
|
|
NORMAL_PREFIX=">>>"
|
|
INDENT_PREFIX=" |"
|
|
|
|
LOG_PREFIX="$NORMAL_PREFIX"
|
|
|
|
echo_color() {
|
|
echo -e "${1}"
|
|
}
|
|
|
|
success() {
|
|
if [ "$LOG_PREFIX" == "$NORMAL_PREFIX" ]; then
|
|
echo_color "${BOLD}${GREEN}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
|
else
|
|
echo_color "${BOLD}${GREEN}${LOG_PREFIX}${RESET} $1"
|
|
fi
|
|
}
|
|
|
|
info() {
|
|
if [ "$LOG_PREFIX" == "$NORMAL_PREFIX" ]; then
|
|
echo_color "${BOLD}${BLUE}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
|
else
|
|
echo_color "${BOLD}${BLUE}${LOG_PREFIX}${RESET} $1"
|
|
fi
|
|
}
|
|
|
|
warn() {
|
|
if [ "$LOG_PREFIX" == "$NORMAL_PREFIX" ]; then
|
|
echo_color "${BOLD}${YELLOW}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
|
else
|
|
echo_color "${BOLD}${YELLOW}${LOG_PREFIX}${RESET} $1"
|
|
fi
|
|
}
|
|
|
|
error() {
|
|
if [ "$LOG_PREFIX" == "$NORMAL_PREFIX" ]; then
|
|
echo_color "${BOLD}${RED}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
|
else
|
|
echo_color "${BOLD}${RED}${LOG_PREFIX}${RESET} $1"
|
|
fi
|
|
}
|
|
|
|
print() {
|
|
echo_color "${BOLD}${GRAY}$1${RESET}"
|
|
}
|
|
|
|
set_indent() {
|
|
LOG_PREFIX="$INDENT_PREFIX"
|
|
}
|
|
|
|
unset_indent() {
|
|
LOG_PREFIX="$NORMAL_PREFIX"
|
|
}
|
|
|
|
####################
|
|
## util functions ##
|
|
####################
|
|
check_ret() {
|
|
if [ $? -ne 0 ]; then
|
|
if [ ! -z "$1" ]; then
|
|
error "$1"
|
|
fi
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# integer-to-yes-or-no
|
|
itoyn() {
|
|
if [ $1 -eq 0 ]; then
|
|
echo "${RED}no${RESET}"
|
|
else
|
|
echo "${GREEN}yes${RESET}"
|
|
fi
|
|
}
|
|
|
|
# unsets all the pool script vars/functions
|
|
clean_pool_vars() {
|
|
unset NAME
|
|
unset MAINTAINER
|
|
unset PUBKEY
|
|
unset SRCDIR
|
|
}
|
|
|
|
# checks if all the required pool script vars/functions are set
|
|
check_pool_vars() {
|
|
if [ ! -n "$NAME" ]; then
|
|
error "Failed to load the pool script"
|
|
set_indent
|
|
|
|
error "Required pool variable is not set: \"\$NAME\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif [ ! -n "$MAINTAINER" ]; then
|
|
error "Failed to load the pool script"
|
|
set_indent
|
|
|
|
error "Required pool variable is not set: \"\$MAINTAINER\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif [ ! -n "$PUBKEY" ]; then
|
|
error "Failed to load the pool script"
|
|
set_indent
|
|
|
|
error "Required pool variable is not set: \"\$PUBKEY\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif [ ! -n "$SRCDIR" ]; then
|
|
error "Failed to load the pool script"
|
|
set_indent
|
|
|
|
error "Required pool variable is not set: \"\$SRCDIR\""
|
|
unset_indent
|
|
|
|
return 1
|
|
fi
|
|
|
|
case "${NAME}" in
|
|
*" "*)
|
|
error "Pool name contains an invalid character: \" \""
|
|
unset_indent
|
|
return 1 ;;
|
|
esac
|
|
|
|
return 0
|
|
}
|
|
|
|
# unsets all the package script vars/functions
|
|
clean_pkg_vars() {
|
|
unset NAME
|
|
unset DESC
|
|
unset VERSION
|
|
unset FILES
|
|
unset HASHES
|
|
unset DEPENDS
|
|
unset BUILD
|
|
unset PACKAGE
|
|
unset INSTALL
|
|
}
|
|
|
|
# checks if all the required package script vars/functions are set
|
|
check_pkg_vars() {
|
|
if [ ! -n "$NAME" ]; then
|
|
error "Failed to load the package script"
|
|
set_indent
|
|
|
|
error "Required package variable is not set: \"\$NAME\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif [ ! -n "$VERSION" ]; then
|
|
error "Failed to load the package script"
|
|
set_indent
|
|
|
|
error "Required package variable is not set: \"\$VERSION\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif [ ! -n "$DESC" ]; then
|
|
error "Failed to load the package script"
|
|
set_indent
|
|
|
|
error "Required package variable is not set: \"\$DESC\""
|
|
unset_indent
|
|
|
|
return 1
|
|
elif ! type PACKAGE &>/dev/null; then
|
|
error "Failed to load the package script"
|
|
set_indent
|
|
|
|
error "Required \"PACKAGE()\" function is not defined"
|
|
unset_indent
|
|
|
|
return 1
|
|
fi
|
|
|
|
case "${NAME}" in
|
|
*_*)
|
|
error "Package name contains an invalid character: \"_\""
|
|
unset_indent
|
|
return 1 ;;
|
|
*" "*)
|
|
error "Package name contains an invalid character: \" \""
|
|
unset_indent
|
|
return 1 ;;
|
|
esac
|
|
|
|
case "${VERSION}" in
|
|
*_*)
|
|
error "Package version contains an invalid character: \"_\""
|
|
unset_indent
|
|
return 1 ;;
|
|
*" "*)
|
|
error "Package version contains an invalid character: \" \""
|
|
unset_indent
|
|
return 1 ;;
|
|
esac
|
|
|
|
return 0
|
|
}
|
|
|
|
# check if a package should be ignored
|
|
should_ignore(){
|
|
for i in "${IGNORE[@]}"; do
|
|
[ "${i}" == "${1}" ] && return 0
|
|
done
|
|
return 1
|
|
}
|