soapdish/scripts/system/system-functions

217 lines
4.5 KiB
Plaintext
Raw Normal View History

2021-03-07 07:47:06 +00:00
#!/bin/bash
# Functions
#
#
# FUNCTION: Git Keep (touch routine)
# Touch .gitkeep files to create folders where they might not exist
#
#
function git_keep_touch_routine() {
sudo touch \
"$APPLICATION_ROOT/audio_queued/.gitkeep" \
"$APPLICATION_ROOT/config/.gitkeep" \
"$APPLICATION_ROOT/config/trash/.gitkeep" \
"$APPLICATION_ROOT/playlists/.gitkeep" \
"$APPLICATION_ROOT/var/log/.gitkeep" \
}
#
# FUNCTION: Verify Dependencies
# Check to make sure that all dependencies are installed, else quit
#
#
function check_for_missing_dependencies() {
# Dependencies to check for ($DEPENDENCIES defined in system-declarations)
MISSING_DEPENDENCIES=0
# Iterate and check for missing
for DEPENDENCY_NAME in "${DEPENDENCIES[@]}"; do :
# If missing, then flag as missing exists
if ! command -v "$DEPENDENCY_NAME" &> /dev/null; then
MISSING_DEPENDENCIES=1
fi
done
# if missing exist, list and exit
if [[ $MISSING_DEPENDENCIES == 1 ]]; then
echo -e "The following missing dependencies are required for soapdish (v${VERSION}):"
for DEPENDENCY_NAME in "${DEPENDENCIES[@]}"; do :
if ! command -v "$DEPENDENCY_NAME" &> /dev/null; then
printf "%s$DEPENDENCY_NAME "
fi
done
echo -e "\nPlease install and try again."
# Exit
exit
fi
}
#
# FUNCTION: Verify Config File Identifier Validity
# Check to make sure that all dependencies are installed, else quit
#
#
function verify_config_file_identifier_validity() {
echo -e "Verifying config file identifier validity (WIP)"
# Get value of identifier and act accordingly (if blank or not)
CURRENT_CONFIG_FILE_IDENTIFIER_VALUE=$(<$CONFIG_FILE_IDENTIFIER_PATH)
if [[ $CURRENT_CONFIG_FILE_IDENTIFIER_VALUE != "" ]]
then
echo -e "Config File Identifier Value == $CURRENT_CONFIG_FILE_IDENTIFIER_VALUE"
echo -e "Here is where you compare flag to each config file"
echo -e "constuct array of all config names"
echo -e "cycle through to verify flag matches"
echo -e "if no match, then wipe flag"
echo -e "make sure flag in main menu updates AFTER you wipe flag"
echo -e "(run this function before anythign else runs)"
fi
}
#
# FUNCTION: Render Horizontal Rule
# Renders an 80-character horizontal rule
#
#
function render_horizontal_rule() {
# # HR style alternaties (uncomment desired style)
# HR_PIECE="\u2504"
# HR_PIECE="\u2508"
HR_PIECE="\u2500"
# HR_PIECE=""
# Render
for i in {1..80}
do
printf "$HR_PIECE"
done
echo ""
}
#
# FUNCTION: Generate Random Hex Value
# Renders an a hex string of $1 character length
#
#
function generate_random_hex_value {
HEX_NUMBER_COMPILED=$(tr -dc 'A-F0-9' < /dev/urandom | head -c$1)
echo "$HEX_NUMBER_COMPILED"
}
#
#
# FUNCTION: Render Mini Horizontal Rule
# Renders an 20-character horizontal rule
#
#
function render_mini_horizontal_rule() {
# # HR style alternaties (uncomment desired style)
# HR_PIECE="\u2504"
# HR_PIECE="\u2508"
HR_PIECE="\u2500"
# HR_PIECE=""
# Render
for i in {1..20}
do
printf "$HR_PIECE"
done
echo ""
}
#
#
# FUNCTION: Reset Permissions
# Recursively resets permissions within application
#
#
function reset_permissions() {
# Specify target directory ($DIR declared in system-declarations), and reset permissions
DIR=${APPLICATION_ROOT}
CURRENT_CHOWN_USER=$(whoami)
sudo chown -R "$CURRENT_CHOWN_USER" "$DIR"
sudo chmod 755 -R "$DIR"
}
#
#
# FUNCTIONS: Alert Messages
# Styles input string according to message type (red:error, yellow:warning, green:alert)
#
#
function error_message() {
echo -e "${COLOR_RED}$1${COLOR_DEFAULT}"
}
function warning_message() {
echo -e "${COLOR_YELLOW}$1${COLOR_DEFAULT}"
}
function confirmation_message() {
echo -e "${COLOR_GREEN}$1${COLOR_DEFAULT}"
}
#
#
# FUNCTIONS: Encode URL
# Converts string to url-friendly (encoded) string
#
#
function url_encode_string () {
tab="`echo -en "\x9"`"
i="$@"
i=${i//%/%25} ; i=${i//' '/%20} ; i=${i//$tab/%09}
i=${i//!/%21} ; i=${i//'"'/%22} ; i=${i//#/%23}
i=${i//\$/%24} ; i=${i//\&/%26} ; i=${i//\'/%27}
i=${i//(/%28} ; i=${i//)/%29} ; i=${i//\*/%2a}
i=${i//+/%2b} ; i=${i//,/%2c} ; i=${i//-/%2d}
i=${i//\./%2e} ; i=${i//\//%2f} ; i=${i//:/%3a}
i=${i//;/%3b} ; i=${i//</%3c} ; i=${i//=/%3d}
i=${i//>/%3e} ; i=${i//\?/%3f} ; i=${i//@/%40}
i=${i//\[/%5b} ; i=${i//\\/%5c} ; i=${i//\]/%5d}
i=${i//\^/%5e} ; i=${i//_/%5f} ; i=${i//\`/%60}
i=${i//\{/%7b} ; i=${i//|/%7c} ; i=${i//\}/%7d}
i=${i//\~/%7e}
echo "$i"
i=""
}