2024-08-11 14:45:54 +00:00
|
|
|
#!/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
|
|
|
|
|
2024-08-18 18:49:31 +00:00
|
|
|
#############
|
|
|
|
## version ##
|
|
|
|
#############
|
|
|
|
MTSC_VERSION="MTSC_VERSION_HOLDER"
|
|
|
|
|
2024-08-11 14:45:54 +00:00
|
|
|
############################
|
|
|
|
## 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
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:53:08 +00:00
|
|
|
# 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
|
2024-08-11 14:45:54 +00:00
|
|
|
check_pool_vars() {
|
|
|
|
if [ ! -n "$NAME" ]; then
|
|
|
|
error "Failed to load the pool script"
|
|
|
|
set_indent
|
2024-08-11 22:53:08 +00:00
|
|
|
|
2024-08-11 14:45:54 +00:00
|
|
|
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
|
|
|
|
|
2024-08-11 22:53:08 +00:00
|
|
|
return 1
|
2024-08-11 14:45:54 +00:00
|
|
|
elif [ ! -n "$PUBKEY" ]; then
|
2024-08-11 22:53:08 +00:00
|
|
|
error "Failed to load the pool script"
|
2024-08-11 14:45:54 +00:00
|
|
|
set_indent
|
|
|
|
|
|
|
|
error "Required pool variable is not set: \"\$PUBKEY\""
|
|
|
|
unset_indent
|
|
|
|
|
|
|
|
return 1
|
|
|
|
elif [ ! -n "$SRCDIR" ]; then
|
2024-08-11 22:53:08 +00:00
|
|
|
error "Failed to load the pool script"
|
2024-08-11 14:45:54 +00:00
|
|
|
set_indent
|
|
|
|
|
|
|
|
error "Required pool variable is not set: \"\$SRCDIR\""
|
|
|
|
unset_indent
|
|
|
|
|
|
|
|
return 1
|
|
|
|
fi
|
2024-08-11 22:53:08 +00:00
|
|
|
|
2024-08-11 14:45:54 +00:00
|
|
|
case "${NAME}" in
|
|
|
|
*" "*)
|
|
|
|
error "Pool name contains an invalid character: \" \""
|
|
|
|
unset_indent
|
|
|
|
return 1 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:53:08 +00:00
|
|
|
# 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
|
2024-08-11 14:45:54 +00:00
|
|
|
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
|
|
|
|
|
2024-08-25 11:56:58 +00:00
|
|
|
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
|
|
|
|
|
2024-08-11 14:45:54 +00:00
|
|
|
return 0
|
|
|
|
}
|
2024-08-21 21:08:56 +00:00
|
|
|
|
|
|
|
# check if a package should be ignored
|
|
|
|
should_ignore(){
|
|
|
|
for i in "${IGNORE[@]}"; do
|
|
|
|
[ "${i}" == "${1}" ] && return 0
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|