diff --git a/kiauh.sh b/kiauh.sh index 1ef8233..119e7dd 100755 --- a/kiauh.sh +++ b/kiauh.sh @@ -12,32 +12,11 @@ set -e clear -### set color variables -green=$(echo -en "\e[92m") -yellow=$(echo -en "\e[93m") -red=$(echo -en "\e[91m") -cyan=$(echo -en "\e[96m") -default=$(echo -en "\e[39m") -white=$(echo -en "\e[39m") - ### sourcing all additional scripts SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )" for script in "${SRCDIR}/kiauh/scripts/"*.sh; do . "${script}"; done for script in "${SRCDIR}/kiauh/scripts/ui/"*.sh; do . "${script}"; done -#nginx -NGINX_SA=/etc/nginx/sites-available -NGINX_SE=/etc/nginx/sites-enabled -NGINX_CONFD=/etc/nginx/conf.d -#misc -INI_FILE=${HOME}/.kiauh.ini - -### set github repos -DMBUTYUGIN_REPO=https://github.com/dmbutyugin/klipper.git -#branches -BRANCH_SCURVE_SMOOTHING=dmbutyugin/scurve-smoothing -BRANCH_SCURVE_SHAPING=dmbutyugin/scurve-shaping - #===================================================# #=================== UPDATE KIAUH ==================# #===================================================# @@ -70,77 +49,39 @@ function kiauh_update_avail(){ } ### format some default message types -select_msg() { - echo -e "${white}>>>>>> $1" -} -warn_msg(){ - echo -e "${red}>>>>>> $1${white}" -} -status_msg(){ - echo; echo -e "${yellow}###### $1${white}" -} -ok_msg(){ - echo -e "${green}>>>>>> $1${white}" -} -error_msg(){ - echo -e "${red}>>>>>> $1${white}" -} -abort_msg(){ - echo -e "${red}<<<<<< $1${white}" -} -title_msg(){ - echo -e "${cyan}$1${white}" -} get_date(){ current_date=$(date +"%y%m%d-%H%M") export current_date } -print_unkown_cmd(){ - ERROR_MSG="Invalid command!" -} -invalid_option(){ - ERROR_MSG="Invalid command!" -} -print_msg(){ - if [ -n "${ERROR_MSG}" ]; then - echo -e "${red}" - echo -e "#########################################################" - echo -e " ${ERROR_MSG} " - echo -e "#########################################################" - echo -e "${white}" - fi - if [ -n "${CONFIRM_MSG}" ]; then - echo -e "${green}" - echo -e "#########################################################" - echo -e " ${CONFIRM_MSG} " - echo -e "#########################################################" - echo -e "${white}" - fi -} +#print_unkown_cmd(){ +# ERROR_MSG="Invalid command!" +#} +#invalid_option(){ +# ERROR_MSG="Invalid command!" +#} -print_error(){ - [ -z "${1}" ] && return - echo -e "${red}" - echo -e "#########################################################" - echo -e " ${1} " - echo -e "#########################################################" - echo -e "${white}" -} - -print_confirm(){ - [ -z "${1}" ] && return - echo -e "${green}" - echo -e "#########################################################" - echo -e " ${1} " - echo -e "#########################################################" - echo -e "${white}" -} - -clear_msg(){ - unset CONFIRM_MSG - unset ERROR_MSG -} +#print_msg(){ +# if [ -n "${ERROR_MSG}" ]; then +# echo -e "${red}" +# echo -e "#########################################################" +# echo -e " ${ERROR_MSG} " +# echo -e "#########################################################" +# echo -e "${white}" +# fi +# if [ -n "${CONFIRM_MSG}" ]; then +# echo -e "${green}" +# echo -e "#########################################################" +# echo -e " ${CONFIRM_MSG} " +# echo -e "#########################################################" +# echo -e "${white}" +# fi +#} +# +#clear_msg(){ +# unset CONFIRM_MSG +# unset ERROR_MSG +#} check_euid init_ini diff --git a/scripts/nginx.sh b/scripts/nginx.sh index 3d015e6..587c251 100644 --- a/scripts/nginx.sh +++ b/scripts/nginx.sh @@ -11,6 +11,11 @@ set -e +### global variables +NGINX_SA="/etc/nginx/sites-available" +NGINX_SE="/etc/nginx/sites-enabled" +NGINX_CONFD="/etc/nginx/conf.d" + #===================================================# #=================== REMOVE NGINX ==================# #===================================================# diff --git a/scripts/utilities.sh b/scripts/utilities.sh index 46426ce..88d985b 100644 --- a/scripts/utilities.sh +++ b/scripts/utilities.sh @@ -11,12 +11,16 @@ set -e -### base variables +### global variables SYSTEMD="/etc/systemd/system" +INI_FILE="${HOME}/.kiauh.ini" LOGFILE="/tmp/kiauh.log" -# setting up some frequently used functions -check_euid(){ +#================================================# +#=================== STARTUP ====================# +#================================================# + +function check_euid(){ if [ "${EUID}" -eq 0 ] then echo -e "${red}" @@ -28,13 +32,121 @@ check_euid(){ fi } +#================================================# +#============= MESSAGE FORMATTING ===============# +#================================================# +green=$(echo -en "\e[92m") +yellow=$(echo -en "\e[93m") +red=$(echo -en "\e[91m") +cyan=$(echo -en "\e[96m") +white=$(echo -en "\e[39m") + +function select_msg() { + echo -e "${white}>>>>>> $1" +} +function warn_msg(){ + echo -e "${red}>>>>>> $1${white}" +} +function status_msg(){ + echo; echo -e "${yellow}###### $1${white}" +} +function ok_msg(){ + echo -e "${green}>>>>>> $1${white}" +} +function error_msg(){ + echo -e "${red}>>>>>> $1${white}" +} +function abort_msg(){ + echo -e "${red}<<<<<< $1${white}" +} +function title_msg(){ + echo -e "${cyan}$1${white}" +} + +function print_error(){ + [ -z "${1}" ] && return + echo -e "${red}" + echo -e "#########################################################" + echo -e " ${1} " + echo -e "#########################################################" + echo -e "${white}" +} + +function print_confirm(){ + [ -z "${1}" ] && return + echo -e "${green}" + echo -e "#########################################################" + echo -e " ${1} " + echo -e "#########################################################" + echo -e "${white}" +} + +#================================================# +#=================== LOGGING ====================# +#================================================# + function timestamp() { date +"[%F %T]" } -function log() { +function log_info() { local message="${1}" - echo -e "$(timestamp) ${message}" | tr -s " " >> "${LOGFILE}" + echo -e "$(timestamp) ${message}" | tr -s " " >> "${LOGFILE}" +} + +function log_warning() { + local message="${1}" + echo -e "$(timestamp) ${message}" | tr -s " " >> "${LOGFILE}" +} + +function log_error() { + local message="${1}" + echo -e "$(timestamp) ${message}" | tr -s " " >> "${LOGFILE}" +} + +#================================================# +#=============== KIAUH SETTINGS =================# +#================================================# + +function read_kiauh_ini(){ + if [ ! -f "${INI_FILE}" ]; then + log_error "Reading from .kiauh.ini failed! File not found!" + return + fi + log_info "Reading from .kiauh.ini" + source "${INI_FILE}" +} + +function init_ini(){ + ### remove pre-version 4 ini files + if [ -f "${INI_FILE}" ] && ! grep -Eq "^KIAUH v4\.0\.0$" "${INI_FILE}"; then + rm "${INI_FILE}" + fi + ### initialize ini file + if [ ! -f "${INI_FILE}" ]; then + { + echo -e "File creation date: $(date)" + echo -e "#=================================================#" + echo -e "# KIAUH - Klipper Installation And Update Helper #" + echo -e "# https://github.com/th33xitus/kiauh #" + echo -e "# DO NOT edit this file! #" + echo -e "#=================================================#" + echo -e "KIAUH v4.0.0" + } >> "${INI_FILE}" + fi + if ! grep -Eq "^backup_before_update=." "${INI_FILE}"; then + echo -e "\nbackup_before_update=false\c" >> "${INI_FILE}" + fi + if ! grep -Eq "^logupload_accepted=." "${INI_FILE}"; then + echo -e "\nlogupload_accepted=false\c" >> "${INI_FILE}" + fi + if ! grep -Eq "^custom_klipper_cfg_loc=" "${INI_FILE}"; then + echo -e "\ncustom_klipper_cfg_loc=\c" >> "${INI_FILE}" + fi + if ! grep -Eq "^mainsail_always_install_latest=" "${INI_FILE}"; then + echo -e "\nmainsail_always_install_latest=false\c" >> "${INI_FILE}" + fi + fetch_webui_ports } check_klipper_cfg_path(){ @@ -166,10 +278,6 @@ set_klipper_cfg_path(){ do_action_service "restart" "moonraker" } -source_kiauh_ini(){ - source $INI_FILE -} - do_action_service(){ shopt -s extglob # enable extended globbing SERVICES="${SYSTEMD}/$2?(-*([0-9])).service" @@ -487,33 +595,3 @@ function fetch_webui_ports(){ fi done } - -init_ini(){ - ### copy an existing kiauh.ini to its new location to keep all possible saved values - if [ -f "${SRCDIR}/kiauh/kiauh.ini" ] && [ ! -f "${INI_FILE}" ]; then - cp "${SRCDIR}/kiauh/kiauh.ini" "${INI_FILE}" - fi - if [ ! -f "${INI_FILE}" ]; then - echo -e "#don't edit this file if you don't know what you are doing...\c" > "${INI_FILE}" - fi - if [ ! $(grep -E "^backup_before_update=." "${INI_FILE}") ]; then - echo -e "\nbackup_before_update=false\c" >> "${INI_FILE}" - fi - if [ ! $(grep -E "^previous_origin_state=[[:alnum:]]" "${INI_FILE}") ]; then - echo -e "\nprevious_origin_state=0\c" >> "${INI_FILE}" - fi - if [ ! $(grep -E "^previous_smoothing_state=[[:alnum:]]" "${INI_FILE}") ]; then - echo -e "\nprevious_smoothing_state=0\c" >> "${INI_FILE}" - fi - if [ ! $(grep -E "^previous_shaping_state=[[:alnum:]]" "${INI_FILE}") ]; then - echo -e "\nprevious_shaping_state=0\c" >> "${INI_FILE}" - fi - if [ ! $(grep -E "^logupload_accepted=." "${INI_FILE}") ]; then - echo -e "\nlogupload_accepted=false\c" >> "${INI_FILE}" - fi - ###add empty klipper config path if missing - if [ ! $(grep -E "^klipper_cfg_loc=" "${INI_FILE}") ]; then - echo -e "\nklipper_cfg_loc=\c" >> "${INI_FILE}" - fi - fetch_webui_ports -}