style(install_klipper_webui.sh): fix some shellsheck warnings

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-01 18:12:41 +02:00
parent a1e7be1960
commit 76ea886b95

View File

@@ -1,3 +1,16 @@
#!/bin/bash
#=======================================================================#
# Copyright (C) 2020 - 2022 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/th33xitus/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
#=======================================================================#
set -e
MAINSAIL_REPO_API="https://api.github.com/repos/mainsail-crew/mainsail/releases" MAINSAIL_REPO_API="https://api.github.com/repos/mainsail-crew/mainsail/releases"
FLUIDD_REPO_API="https://api.github.com/repos/fluidd-core/fluidd/releases" FLUIDD_REPO_API="https://api.github.com/repos/fluidd-core/fluidd/releases"
@@ -38,7 +51,7 @@ get_user_selection_mjpg-streamer(){
echo -e "| Install MJGP-Streamer for webcam support? |" echo -e "| Install MJGP-Streamer for webcam support? |"
bottom_border bottom_border
read -p "${cyan}###### Install MJPG-Streamer? (Y/n):${default} " yn read -p "${cyan}###### Install MJPG-Streamer? (Y/n):${default} " yn
case "$yn" in case "${yn}" in
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" echo -e "###### > Yes"
INSTALL_MJPG="true" INSTALL_MJPG="true"
@@ -75,7 +88,7 @@ get_user_selection_kiauh_macros(){
echo -e "| add the recommended example macros to your config. |" echo -e "| add the recommended example macros to your config. |"
bottom_border bottom_border
read -p "${cyan}###### Add the recommended macros? (Y/n):${default} " yn read -p "${cyan}###### Add the recommended macros? (Y/n):${default} " yn
case "$yn" in case "${yn}" in
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" echo -e "###### > Yes"
ADD_KIAUH_MACROS="true" ADD_KIAUH_MACROS="true"
@@ -108,18 +121,18 @@ install_webui(){
[ "$1" == "fluidd" ] && IF_NAME1="Fluidd" && IF_NAME2="Fluidd " [ "$1" == "fluidd" ] && IF_NAME1="Fluidd" && IF_NAME2="Fluidd "
### exit mainsail/fluidd setup if moonraker not found ### exit mainsail/fluidd setup if moonraker not found
if [ $moonraker_chk_ok = "false" ]; then if [ "${moonraker_chk_ok}" = "false" ]; then
ERROR_MSG="Moonraker service not found!\n Please install Moonraker first!" ERROR_MSG="Moonraker service not found!\n Please install Moonraker first!"
print_msg && clear_msg && return 0 print_msg && clear_msg && return 0
fi fi
status_msg "Initializing $IF_NAME1 installation ..." status_msg "Initializing ${IF_NAME1} installation ..."
### check for other enabled web interfaces ### check for other enabled web interfaces
unset SET_LISTEN_PORT unset SET_LISTEN_PORT
detect_enabled_sites detect_enabled_sites
### check if another site already listens to port 80 ### check if another site already listens to port 80
$1_port_check "${1}"_port_check
### ask user to install mjpg-streamer ### ask user to install mjpg-streamer
if ! ls /etc/systemd/system/webcamd.service 2>/dev/null 1>&2; then if ! ls /etc/systemd/system/webcamd.service 2>/dev/null 1>&2; then
@@ -127,46 +140,46 @@ install_webui(){
fi fi
### ask user to install the recommended webinterface macros ### ask user to install the recommended webinterface macros
if ! ls "$klipper_cfg_loc"/kiauh_macros.cfg 2>/dev/null 1>&2 || ! ls "$klipper_cfg_loc"/printer_*/kiauh_macros.cfg 2>/dev/null 1>&2; then if ! ls "${klipper_cfg_loc}"/kiauh_macros.cfg 2>/dev/null 1>&2 || ! ls "${klipper_cfg_loc}"/printer_*/kiauh_macros.cfg 2>/dev/null 1>&2; then
get_user_selection_kiauh_macros "$IF_NAME2" get_user_selection_kiauh_macros "${IF_NAME2}"
fi fi
### create /etc/nginx/conf.d/upstreams.conf ### create /etc/nginx/conf.d/upstreams.conf
set_upstream_nginx_cfg set_upstream_nginx_cfg
### create /etc/nginx/sites-available/<interface config> ### create /etc/nginx/sites-available/<interface config>
set_nginx_cfg "$1" set_nginx_cfg "${1}"
### symlink nginx log ### symlink nginx log
symlink_webui_nginx_log "$1" symlink_webui_nginx_log "${1}"
### copy the kiauh_macros.cfg to the config location ### copy the kiauh_macros.cfg to the config location
install_kiauh_macros install_kiauh_macros
### install mainsail/fluidd ### install mainsail/fluidd
$1_setup "${1}"_setup
### install mjpg-streamer ### install mjpg-streamer
[ "$INSTALL_MJPG" = "true" ] && install_mjpg-streamer [ "${INSTALL_MJPG}" = "true" ] && install_mjpg-streamer
fetch_webui_ports #WIP fetch_webui_ports #WIP
### confirm message ### confirm message
CONFIRM_MSG="$IF_NAME1 has been set up!" CONFIRM_MSG="${IF_NAME1} has been set up!"
print_msg && clear_msg print_msg && clear_msg
} }
symlink_webui_nginx_log(){ symlink_webui_nginx_log(){
LPATH="${HOME}/klipper_logs" LPATH="${HOME}/klipper_logs"
UI_ACCESS_LOG="/var/log/nginx/$1-access.log" UI_ACCESS_LOG="/var/log/nginx/${1}-access.log"
UI_ERROR_LOG="/var/log/nginx/$1-error.log" UI_ERROR_LOG="/var/log/nginx/${1}-error.log"
[ ! -d "$LPATH" ] && mkdir -p "$LPATH" [ ! -d "${LPATH}" ] && mkdir -p "${LPATH}"
if [ -f "$UI_ACCESS_LOG" ] && [ ! -L "$LPATH/$1-access.log" ]; then if [ -f "${UI_ACCESS_LOG}" ] && [ ! -L "${LPATH}/${1}-access.log" ]; then
status_msg "Creating symlink for $UI_ACCESS_LOG ..." status_msg "Creating symlink for ${UI_ACCESS_LOG} ..."
ln -s "$UI_ACCESS_LOG" "$LPATH" ln -s "${UI_ACCESS_LOG}" "${LPATH}"
ok_msg "OK!" ok_msg "OK!"
fi fi
if [ -f "$UI_ERROR_LOG" ] && [ ! -L "$LPATH/$1-error.log" ]; then if [ -f "${UI_ERROR_LOG}" ] && [ ! -L "${LPATH}/${1}-error.log" ]; then
status_msg "Creating symlink for $UI_ERROR_LOG ..." status_msg "Creating symlink for ${UI_ERROR_LOG} ..."
ln -s "$UI_ERROR_LOG" "$LPATH" ln -s "${UI_ERROR_LOG}" "${LPATH}"
ok_msg "OK!" ok_msg "OK!"
fi fi
} }
@@ -174,30 +187,30 @@ symlink_webui_nginx_log(){
install_kiauh_macros(){ install_kiauh_macros(){
source_kiauh_ini source_kiauh_ini
### copy kiauh_macros.cfg ### copy kiauh_macros.cfg
if [ "$ADD_KIAUH_MACROS" = "true" ]; then if [ "${ADD_KIAUH_MACROS}" = "true" ]; then
### create a backup of the config folder ### create a backup of the config folder
backup_klipper_config_dir backup_klipper_config_dir
### handle multi printer.cfg ### handle multi printer.cfg
if ls "$klipper_cfg_loc"/printer_* 2>/dev/null 1>&2; then if ls "${klipper_cfg_loc}"/printer_* 2>/dev/null 1>&2; then
for config in $(find $klipper_cfg_loc/printer_*/printer.cfg); do for config in $(find ${klipper_cfg_loc}/printer_*/printer.cfg); do
path=$(echo "$config" | rev | cut -d"/" -f2- | rev) path=$(echo "${config}" | rev | cut -d"/" -f2- | rev)
if [ ! -f "$path/kiauh_macros.cfg" ]; then if [ ! -f "${path}/kiauh_macros.cfg" ]; then
### copy kiauh_macros.cfg to config location ### copy kiauh_macros.cfg to config location
status_msg "Creating macro config file ..." status_msg "Creating macro config file ..."
cp "${SRCDIR}/kiauh/resources/kiauh_macros.cfg" "$path" cp "${SRCDIR}/kiauh/resources/kiauh_macros.cfg" "${path}"
### write the include to the very first line of the printer.cfg ### write the include to the very first line of the printer.cfg
sed -i "1 i [include kiauh_macros.cfg]" "$path/printer.cfg" sed -i "1 i [include kiauh_macros.cfg]" "${path}/printer.cfg"
ok_msg "$path/kiauh_macros.cfg created!" ok_msg "${path}/kiauh_macros.cfg created!"
fi fi
done done
### handle single printer.cfg ### handle single printer.cfg
elif [ -f "$klipper_cfg_loc/printer.cfg" ] && [ ! -f "$klipper_cfg_loc/kiauh_macros.cfg" ]; then elif [ -f "${klipper_cfg_loc}/printer.cfg" ] && [ ! -f "${klipper_cfg_loc}/kiauh_macros.cfg" ]; then
### copy kiauh_macros.cfg to config location ### copy kiauh_macros.cfg to config location
status_msg "Creating macro config file ..." status_msg "Creating macro config file ..."
cp "${SRCDIR}/kiauh/resources/kiauh_macros.cfg" "$klipper_cfg_loc" cp "${SRCDIR}/kiauh/resources/kiauh_macros.cfg" "${klipper_cfg_loc}"
### write the include to the very first line of the printer.cfg ### write the include to the very first line of the printer.cfg
sed -i "1 i [include kiauh_macros.cfg]" "$klipper_cfg_loc/printer.cfg" sed -i "1 i [include kiauh_macros.cfg]" "${klipper_cfg_loc}/printer.cfg"
ok_msg "$klipper_cfg_loc/kiauh_macros.cfg created!" ok_msg "${klipper_cfg_loc}/kiauh_macros.cfg created!"
fi fi
### restart klipper service to parse the modified printer.cfg ### restart klipper service to parse the modified printer.cfg
do_action_service "restart" "klipper" do_action_service "restart" "klipper"
@@ -205,19 +218,19 @@ install_kiauh_macros(){
} }
mainsail_port_check(){ mainsail_port_check(){
if [ "$MAINSAIL_ENABLED" = "false" ]; then if [ "${MAINSAIL_ENABLED}" = "false" ]; then
if [ "$SITE_ENABLED" = "true" ]; then if [ "${SITE_ENABLED}" = "true" ]; then
status_msg "Detected other enabled interfaces:" status_msg "Detected other enabled interfaces:"
[ "$OCTOPRINT_ENABLED" = "true" ] && echo -e " ${cyan}● OctoPrint - Port: $OCTOPRINT_PORT${default}" [ "${OCTOPRINT_ENABLED}" = "true" ] && echo -e " ${cyan}● OctoPrint - Port: ${OCTOPRINT_PORT}${default}"
[ "$FLUIDD_ENABLED" = "true" ] && echo -e " ${cyan}● Fluidd - Port: $FLUIDD_PORT${default}" [ "${FLUIDD_ENABLED}" = "true" ] && echo -e " ${cyan}● Fluidd - Port: ${FLUIDD_PORT}${default}"
[ "$DWC2_ENABLED" = "true" ] && echo -e " ${cyan}● DWC2 - Port: $DWC2_PORT${default}" [ "${DWC2_ENABLED}" = "true" ] && echo -e " ${cyan}● DWC2 - Port: ${DWC2_PORT}${default}"
if [ "$FLUIDD_PORT" = "80" ] || [ "$DWC2_PORT" = "80" ] || [ "$OCTOPRINT_PORT" = "80" ]; then if [ "${FLUIDD_PORT}" = "80" ] || [ "${DWC2_PORT}" = "80" ] || [ "${OCTOPRINT_PORT}" = "80" ]; then
PORT_80_BLOCKED="true" PORT_80_BLOCKED="true"
select_mainsail_port select_mainsail_port
fi fi
else else
DEFAULT_PORT=$(grep listen "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1) DEFAULT_PORT=$(grep listen "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1)
SET_LISTEN_PORT=$DEFAULT_PORT SET_LISTEN_PORT=${DEFAULT_PORT}
fi fi
SET_NGINX_CFG="true" SET_NGINX_CFG="true"
else else
@@ -226,19 +239,19 @@ mainsail_port_check(){
} }
fluidd_port_check(){ fluidd_port_check(){
if [ "$FLUIDD_ENABLED" = "false" ]; then if [ "${FLUIDD_ENABLED}" = "false" ]; then
if [ "$SITE_ENABLED" = "true" ]; then if [ "${SITE_ENABLED}" = "true" ]; then
status_msg "Detected other enabled interfaces:" status_msg "Detected other enabled interfaces:"
[ "$OCTOPRINT_ENABLED" = "true" ] && echo " ${cyan}● OctoPrint - Port: $OCTOPRINT_PORT${default}" [ "${OCTOPRINT_ENABLED}" = "true" ] && echo " ${cyan}● OctoPrint - Port: ${OCTOPRINT_PORT}${default}"
[ "$MAINSAIL_ENABLED" = "true" ] && echo " ${cyan}● Mainsail - Port: $MAINSAIL_PORT${default}" [ "${MAINSAIL_ENABLED}" = "true" ] && echo " ${cyan}● Mainsail - Port: ${MAINSAIL_PORT}${default}"
[ "$DWC2_ENABLED" = "true" ] && echo " ${cyan}● DWC2 - Port: $DWC2_PORT${default}" [ "${DWC2_ENABLED}" = "true" ] && echo " ${cyan}● DWC2 - Port: ${DWC2_PORT}${default}"
if [ "$MAINSAIL_PORT" = "80" ] || [ "$DWC2_PORT" = "80" ] || [ "$OCTOPRINT_PORT" = "80" ]; then if [ "${MAINSAIL_PORT}" = "80" ] || [ "${DWC2_PORT}" = "80" ] || [ "${OCTOPRINT_PORT}" = "80" ]; then
PORT_80_BLOCKED="true" PORT_80_BLOCKED="true"
select_fluidd_port select_fluidd_port
fi fi
else else
DEFAULT_PORT=$(grep listen "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1) DEFAULT_PORT=$(grep listen "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1)
SET_LISTEN_PORT=$DEFAULT_PORT SET_LISTEN_PORT=${DEFAULT_PORT}
fi fi
SET_NGINX_CFG="true" SET_NGINX_CFG="true"
else else
@@ -247,16 +260,16 @@ fluidd_port_check(){
} }
select_mainsail_port(){ select_mainsail_port(){
if [ "$PORT_80_BLOCKED" = "true" ]; then if [ "${PORT_80_BLOCKED}" = "true" ]; then
echo echo
top_border top_border
echo -e "| ${red}!!!WARNING!!!${default} |" echo -e "| ${red}!!!WARNING!!!${default} |"
echo -e "| ${red}You need to choose a different port for Mainsail!${default} |" echo -e "| ${red}You need to choose a different port for Mainsail!${default} |"
echo -e "| ${red}The following web interface is listening at port 80:${default} |" echo -e "| ${red}The following web interface is listening at port 80:${default} |"
blank_line blank_line
[ "$OCTOPRINT_PORT" = "80" ] && echo "| ● OctoPrint |" [ "${OCTOPRINT_PORT}" = "80" ] && echo "| ● OctoPrint |"
[ "$FLUIDD_PORT" = "80" ] && echo "| ● Fluidd |" [ "${FLUIDD_PORT}" = "80" ] && echo "| ● Fluidd |"
[ "$DWC2_PORT" = "80" ] && echo "| ● DWC2 |" [ "${DWC2_PORT}" = "80" ] && echo "| ● DWC2 |"
blank_line blank_line
echo -e "| Make sure you don't choose a port which was already |" echo -e "| Make sure you don't choose a port which was already |"
echo -e "| assigned to one of the other webinterfaces and do ${red}NOT${default} |" echo -e "| assigned to one of the other webinterfaces and do ${red}NOT${default} |"
@@ -267,9 +280,9 @@ select_mainsail_port(){
bottom_border bottom_border
while true; do while true; do
read -p "${cyan}Please enter a new Port:${default} " NEW_PORT read -p "${cyan}Please enter a new Port:${default} " NEW_PORT
if [ "$NEW_PORT" != "$FLUIDD_PORT" ] && [ "$NEW_PORT" != "$DWC2_PORT" ] && [ "$NEW_PORT" != "$OCTOPRINT_PORT" ]; then if [ "${NEW_PORT}" != "${FLUIDD_PORT}" ] && [ "${NEW_PORT}" != "${DWC2_PORT}" ] && [ "${NEW_PORT}" != "${OCTOPRINT_PORT}" ]; then
echo "Setting port $NEW_PORT for Mainsail!" echo "Setting port ${NEW_PORT} for Mainsail!"
SET_LISTEN_PORT=$NEW_PORT SET_LISTEN_PORT=${NEW_PORT}
break break
else else
echo "That port is already taken! Select a different one!" echo "That port is already taken! Select a different one!"
@@ -279,16 +292,16 @@ select_mainsail_port(){
} }
select_fluidd_port(){ select_fluidd_port(){
if [ "$PORT_80_BLOCKED" = "true" ]; then if [ "${PORT_80_BLOCKED}" = "true" ]; then
echo echo
top_border top_border
echo -e "| ${red}!!!WARNING!!!${default} |" echo -e "| ${red}!!!WARNING!!!${default} |"
echo -e "| ${red}You need to choose a different port for Fluidd!${default} |" echo -e "| ${red}You need to choose a different port for Fluidd!${default} |"
echo -e "| ${red}The following web interface is listening at port 80:${default} |" echo -e "| ${red}The following web interface is listening at port 80:${default} |"
blank_line blank_line
[ "$OCTOPRINT_PORT" = "80" ] && echo "| ● OctoPrint |" [ "${OCTOPRINT_PORT}" = "80" ] && echo "| ● OctoPrint |"
[ "$MAINSAIL_PORT" = "80" ] && echo "| ● Mainsail |" [ "${MAINSAIL_PORT}" = "80" ] && echo "| ● Mainsail |"
[ "$DWC2_PORT" = "80" ] && echo "| ● DWC2 |" [ "${DWC2_PORT}" = "80" ] && echo "| ● DWC2 |"
blank_line blank_line
echo -e "| Make sure you don't choose a port which was already |" echo -e "| Make sure you don't choose a port which was already |"
echo -e "| assigned to one of the other webinterfaces and do ${red}NOT${default} |" echo -e "| assigned to one of the other webinterfaces and do ${red}NOT${default} |"
@@ -299,9 +312,9 @@ select_fluidd_port(){
bottom_border bottom_border
while true; do while true; do
read -p "${cyan}Please enter a new Port:${default} " NEW_PORT read -p "${cyan}Please enter a new Port:${default} " NEW_PORT
if [ "$NEW_PORT" != "$MAINSAIL_PORT" ] && [ "$NEW_PORT" != "$DWC2_PORT" ] && [ "$NEW_PORT" != "$OCTOPRINT_PORT" ]; then if [ "${NEW_PORT}" != "${MAINSAIL_PORT}" ] && [ "${NEW_PORT}" != "${DWC2_PORT}" ] && [ "${NEW_PORT}" != "${OCTOPRINT_PORT}" ]; then
echo "Setting port $NEW_PORT for Fluidd!" echo "Setting port ${NEW_PORT} for Fluidd!"
SET_LISTEN_PORT=$NEW_PORT SET_LISTEN_PORT=${NEW_PORT}
break break
else else
echo "That port is already taken! Select a different one!" echo "That port is already taken! Select a different one!"
@@ -311,22 +324,22 @@ select_fluidd_port(){
} }
get_mainsail_ver(){ get_mainsail_ver(){
MAINSAIL_VERSION=$(curl -s $MAINSAIL_REPO_API | grep tag_name | cut -d'"' -f4 | head -1) MAINSAIL_VERSION=$(curl -s "${MAINSAIL_REPO_API}" | grep tag_name | cut -d'"' -f4 | head -1)
} }
get_fluidd_ver(){ get_fluidd_ver(){
FLUIDD_VERSION=$(curl -s $FLUIDD_REPO_API | grep tag_name | cut -d'"' -f4 | head -1) FLUIDD_VERSION=$(curl -s "${FLUIDD_REPO_API}" | grep tag_name | cut -d'"' -f4 | head -1)
} }
mainsail_setup(){ mainsail_setup(){
### get mainsail download url ### get mainsail download url
MAINSAIL_DL_URL=$(curl -s $MAINSAIL_REPO_API | grep browser_download_url | cut -d'"' -f4 | head -1) MAINSAIL_DL_URL=$(curl -s "${MAINSAIL_REPO_API}" | grep browser_download_url | cut -d'"' -f4 | head -1)
### remove existing and create fresh mainsail folder, then download mainsail ### remove existing and create fresh mainsail folder, then download mainsail
[ -d "$MAINSAIL_DIR" ] && rm -rf "$MAINSAIL_DIR" [ -d "${MAINSAIL_DIR}" ] && rm -rf "${MAINSAIL_DIR}"
mkdir "$MAINSAIL_DIR" && cd $MAINSAIL_DIR mkdir "${MAINSAIL_DIR}" && cd "${MAINSAIL_DIR}"
status_msg "Downloading Mainsail $MAINSAIL_VERSION ..." status_msg "Downloading Mainsail ${MAINSAIL_VERSION} ..."
wget "$MAINSAIL_DL_URL" && ok_msg "Download complete!" wget "${MAINSAIL_DL_URL}" && ok_msg "Download complete!"
### extract archive ### extract archive
status_msg "Extracting archive ..." status_msg "Extracting archive ..."
@@ -343,19 +356,19 @@ mainsail_setup(){
} }
enable_mainsail_remotemode(){ enable_mainsail_remotemode(){
rm -f "$MAINSAIL_DIR/config.json" rm -f "${MAINSAIL_DIR}/config.json"
echo -e "{\n \"remoteMode\":true\n}" >> "$MAINSAIL_DIR/config.json" echo -e "{\n \"remoteMode\":true\n}" >> "${MAINSAIL_DIR}/config.json"
} }
fluidd_setup(){ fluidd_setup(){
### get fluidd download url ### get fluidd download url
FLUIDD_DL_URL=$(curl -s $FLUIDD_REPO_API | grep browser_download_url | cut -d'"' -f4 | head -1) FLUIDD_DL_URL=$(curl -s "${FLUIDD_REPO_API}" | grep browser_download_url | cut -d'"' -f4 | head -1)
### remove existing and create fresh fluidd folder, then download fluidd ### remove existing and create fresh fluidd folder, then download fluidd
[ -d "$FLUIDD_DIR" ] && rm -rf "$FLUIDD_DIR" [ -d "${FLUIDD_DIR}" ] && rm -rf "${FLUIDD_DIR}"
mkdir "$FLUIDD_DIR" && cd $FLUIDD_DIR mkdir "${FLUIDD_DIR}" && cd "${FLUIDD_DIR}"
status_msg "Downloading Fluidd $FLUIDD_VERSION ..." status_msg "Downloading Fluidd ${FLUIDD_VERSION} ..."
wget "$FLUIDD_DL_URL" && ok_msg "Download complete!" wget "${FLUIDD_DL_URL}" && ok_msg "Download complete!"
### extract archive ### extract archive
status_msg "Extracting archive ..." status_msg "Extracting archive ..."
@@ -369,19 +382,19 @@ fluidd_setup(){
set_upstream_nginx_cfg(){ set_upstream_nginx_cfg(){
get_date get_date
### backup existing nginx configs ### backup existing nginx configs
[ ! -d "$BACKUP_DIR/nginx_cfg" ] && mkdir -p "$BACKUP_DIR/nginx_cfg" [ ! -d "${BACKUP_DIR}/nginx_cfg" ] && mkdir -p "${BACKUP_DIR}/nginx_cfg"
[ -f "$NGINX_CONFD/upstreams.conf" ] && sudo mv "$NGINX_CONFD/upstreams.conf" "$BACKUP_DIR/nginx_cfg/${current_date}_upstreams.conf" [ -f "${NGINX_CONFD}/upstreams.conf" ] && sudo mv "${NGINX_CONFD}/upstreams.conf" "${BACKUP_DIR}/nginx_cfg/${current_date}_upstreams.conf"
[ -f "$NGINX_CONFD/common_vars.conf" ] && sudo mv "$NGINX_CONFD/common_vars.conf" "$BACKUP_DIR/nginx_cfg/${current_date}_common_vars.conf" [ -f "${NGINX_CONFD}/common_vars.conf" ] && sudo mv "${NGINX_CONFD}/common_vars.conf" "${BACKUP_DIR}/nginx_cfg/${current_date}_common_vars.conf"
### transfer ownership of backed up files from root to ${USER} ### transfer ownership of backed up files from root to ${USER}
for log in $(ls "$BACKUP_DIR/nginx_cfg"); do for log in $(ls "$BACKUP_DIR/nginx_cfg"); do
sudo chown "${USER}" "$BACKUP_DIR/nginx_cfg/$log" sudo chown "${USER}" "${BACKUP_DIR}/nginx_cfg/$log"
done done
### copy nginx configs to target destination ### copy nginx configs to target destination
if [ ! -f "$NGINX_CONFD/upstreams.conf" ]; then if [ ! -f "${NGINX_CONFD}/upstreams.conf" ]; then
sudo cp "${SRCDIR}/kiauh/resources/upstreams.conf" "$NGINX_CONFD" sudo cp "${SRCDIR}/kiauh/resources/upstreams.conf" "${NGINX_CONFD}"
fi fi
if [ ! -f "$NGINX_CONFD/common_vars.conf" ]; then if [ ! -f "${NGINX_CONFD}/common_vars.conf" ]; then
sudo cp "${SRCDIR}/kiauh/resources/common_vars.conf" "$NGINX_CONFD" sudo cp "${SRCDIR}/kiauh/resources/common_vars.conf" "${NGINX_CONFD}"
fi fi
} }
@@ -391,15 +404,15 @@ fetch_webui_ports(){
WEBIFS=(mainsail fluidd octoprint dwc2) WEBIFS=(mainsail fluidd octoprint dwc2)
for interface in "${WEBIFS[@]}"; do for interface in "${WEBIFS[@]}"; do
if [ -f "/etc/nginx/sites-available/${interface}" ]; then if [ -f "/etc/nginx/sites-available/${interface}" ]; then
port=$(grep -E "listen" "/etc/nginx/sites-available/$interface" | head -1 | sed 's/^\s*//' | sed 's/;$//' | cut -d" " -f2) port=$(grep -E "listen" "/etc/nginx/sites-available/${interface}" | head -1 | sed 's/^\s*//' | sed 's/;$//' | cut -d" " -f2)
if [ ! -n "$(grep -E "${interface}_port" "$INI_FILE")" ]; then if [ ! -n "$(grep -E "${interface}_port" "${INI_FILE}")" ]; then
sed -i '$a'"${interface}_port=${port}" "$INI_FILE" sed -i '$a'"${interface}_port=${port}" "${INI_FILE}"
else else
sed -i "/^${interface}_port/d" "$INI_FILE" sed -i "/^${interface}_port/d" "${INI_FILE}"
sed -i '$a'"${interface}_port=${port}" "$INI_FILE" sed -i '$a'"${interface}_port=${port}" "${INI_FILE}"
fi fi
else else
sed -i "/^${interface}_port/d" "$INI_FILE" sed -i "/^${interface}_port/d" "${INI_FILE}"
fi fi
done done
} }
@@ -412,55 +425,55 @@ match_nginx_configs(){
fluidd_nginx_cfg="/etc/nginx/sites-available/fluidd" fluidd_nginx_cfg="/etc/nginx/sites-available/fluidd"
upstreams_webcams=$(grep -E "mjpgstreamer" /etc/nginx/conf.d/upstreams.conf | wc -l) upstreams_webcams=$(grep -E "mjpgstreamer" /etc/nginx/conf.d/upstreams.conf | wc -l)
status_msg "Checking validity of NGINX configurations ..." status_msg "Checking validity of NGINX configurations ..."
if [ -e "$mainsail_nginx_cfg" ]; then if [ -e "${mainsail_nginx_cfg}" ]; then
mainsail_webcams=$(grep -E "mjpgstreamer" "$mainsail_nginx_cfg" | wc -l) mainsail_webcams=$(grep -E "mjpgstreamer" "${mainsail_nginx_cfg}" | wc -l)
fi fi
if [ -e "$fluidd_nginx_cfg" ]; then if [ -e "${fluidd_nginx_cfg}" ]; then
fluidd_webcams=$(grep -E "mjpgstreamer" "$fluidd_nginx_cfg" | wc -l) fluidd_webcams=$(grep -E "mjpgstreamer" "${fluidd_nginx_cfg}" | wc -l)
fi fi
### check for outdated upstreams.conf ### check for outdated upstreams.conf
if [[ "$upstreams_webcams" -lt "$mainsail_webcams" ]] || [[ "$upstreams_webcams" -lt "$fluidd_webcams" ]]; then if [[ "${upstreams_webcams}" -lt "${mainsail_webcams}" ]] || [[ "${upstreams_webcams}" -lt "${fluidd_webcams}" ]]; then
status_msg "Outdated upstreams.conf found! Updating ..." status_msg "Outdated upstreams.conf found! Updating ..."
sudo rm -f "$NGINX_CONFD/upstreams.conf" sudo rm -f "${NGINX_CONFD}/upstreams.conf"
sudo rm -f "$NGINX_CONFD/common_vars.conf" sudo rm -f "${NGINX_CONFD}/common_vars.conf"
set_upstream_nginx_cfg set_upstream_nginx_cfg
cfg_updated="true" cfg_updated="true"
fi fi
### check for outdated mainsail config ### check for outdated mainsail config
if [ -e "$mainsail_nginx_cfg" ]; then if [ -e "${mainsail_nginx_cfg}" ]; then
if [[ "$upstreams_webcams" -gt "$mainsail_webcams" ]]; then if [[ "${upstreams_webcams}" -gt "${mainsail_webcams}" ]]; then
status_msg "Outdated Mainsail config found! Updating ..." status_msg "Outdated Mainsail config found! Updating ..."
sudo rm -f "$mainsail_nginx_cfg" sudo rm -f "${mainsail_nginx_cfg}"
sudo cp "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" "$mainsail_nginx_cfg" sudo cp "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" "${mainsail_nginx_cfg}"
sudo sed -i "s/<<UI>>/mainsail/g" "$mainsail_nginx_cfg" sudo sed -i "s/<<UI>>/mainsail/g" "${mainsail_nginx_cfg}"
sudo sed -i "/root/s/pi/${USER}/" "$mainsail_nginx_cfg" sudo sed -i "/root/s/pi/${USER}/" "${mainsail_nginx_cfg}"
sudo sed -i "s/listen\s[0-9]*;/listen $mainsail_port;/" "$mainsail_nginx_cfg" sudo sed -i "s/listen\s[0-9]*;/listen ${mainsail_port};/" "${mainsail_nginx_cfg}"
sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:$mainsail_port;/" "$mainsail_nginx_cfg" sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${mainsail_port};/" "${mainsail_nginx_cfg}"
cfg_updated="true" && ok_msg "Done!" cfg_updated="true" && ok_msg "Done!"
fi fi
fi fi
### check for outdated fluidd config ### check for outdated fluidd config
if [ -e "$fluidd_nginx_cfg" ]; then if [ -e "${fluidd_nginx_cfg}" ]; then
if [[ "$upstreams_webcams" -gt "$fluidd_webcams" ]]; then if [[ "${upstreams_webcams}" -gt "${fluidd_webcams}" ]]; then
status_msg "Outdated Fluidd config found! Updating ..." status_msg "Outdated Fluidd config found! Updating ..."
sudo rm -f "$fluidd_nginx_cfg" sudo rm -f "${fluidd_nginx_cfg}"
sudo cp "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" "$fluidd_nginx_cfg" sudo cp "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" "${fluidd_nginx_cfg}"
sudo sed -i "s/<<UI>>/fluidd/g" "$fluidd_nginx_cfg" sudo sed -i "s/<<UI>>/fluidd/g" "${fluidd_nginx_cfg}"
sudo sed -i "/root/s/pi/${USER}/" "$fluidd_nginx_cfg" sudo sed -i "/root/s/pi/${USER}/" "${fluidd_nginx_cfg}"
sudo sed -i "s/listen\s[0-9]*;/listen $fluidd_port;/" "$fluidd_nginx_cfg" sudo sed -i "s/listen\s[0-9]*;/listen ${fluidd_port};/" "${fluidd_nginx_cfg}"
sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:$fluidd_port;/" "$fluidd_nginx_cfg" sudo sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${fluidd_port};/" "${fluidd_nginx_cfg}"
cfg_updated="true" && ok_msg "Done!" cfg_updated="true" && ok_msg "Done!"
fi fi
fi fi
### only restart nginx if configs were updated ### only restart nginx if configs were updated
if [ "$cfg_updated" == "true" ]; then if [ "${cfg_updated}" == "true" ]; then
restart_nginx && unset cfg_updated restart_nginx && unset cfg_updated
fi fi
} }
process_octoprint_dialog(){ process_octoprint_dialog(){
#ask user to disable octoprint when its service was found #ask user to disable octoprint when its service was found
if [ "$OCTOPRINT_ENABLED" = "true" ]; then if [ "${OCTOPRINT_ENABLED}" = "true" ]; then
while true; do while true; do
echo echo
top_border top_border
@@ -471,7 +484,7 @@ process_octoprint_dialog(){
echo -e "| pected behavior of the Klipper Webinterfaces. |" echo -e "| pected behavior of the Klipper Webinterfaces. |"
bottom_border bottom_border
read -p "${cyan}###### Do you want to disable OctoPrint now? (Y/n):${default} " yn read -p "${cyan}###### Do you want to disable OctoPrint now? (Y/n):${default} " yn
case "$yn" in case "${yn}" in
Y|y|Yes|yes|"") Y|y|Yes|yes|"")
echo -e "###### > Yes" echo -e "###### > Yes"
status_msg "Stopping OctoPrint ..." status_msg "Stopping OctoPrint ..."
@@ -492,19 +505,19 @@ process_octoprint_dialog(){
process_disruptive_services(){ process_disruptive_services(){
#handle haproxy service #handle haproxy service
if [ "$DISABLE_HAPROXY" = "true" ] || [ "$REMOVE_HAPROXY" = "true" ]; then if [ "${DISABLE_HAPROXY}" = "true" ] || [ "${REMOVE_HAPROXY}" = "true" ]; then
if systemctl is-active haproxy -q; then if systemctl is-active haproxy -q; then
status_msg "Stopping haproxy service ..." status_msg "Stopping haproxy service ..."
sudo systemctl stop haproxy && ok_msg "Service stopped!" sudo systemctl stop haproxy && ok_msg "Service stopped!"
fi fi
### disable haproxy ### disable haproxy
if [ "$DISABLE_HAPROXY" = "true" ]; then if [ "${DISABLE_HAPROXY}" = "true" ]; then
status_msg "Disabling haproxy ..." status_msg "Disabling haproxy ..."
sudo systemctl disable haproxy && ok_msg "Haproxy service disabled!" sudo systemctl disable haproxy && ok_msg "Haproxy service disabled!"
### remove haproxy ### remove haproxy
if [ "$REMOVE_HAPROXY" = "true" ]; then if [ "${REMOVE_HAPROXY}" = "true" ]; then
status_msg "Removing haproxy ..." status_msg "Removing haproxy ..."
sudo apt-get remove haproxy -y && sudo update-rc.d -f haproxy remove && ok_msg "Haproxy removed!" sudo apt-get remove haproxy -y && sudo update-rc.d -f haproxy remove && ok_msg "Haproxy removed!"
fi fi
@@ -512,19 +525,19 @@ process_disruptive_services(){
fi fi
### handle lighttpd service ### handle lighttpd service
if [ "$DISABLE_LIGHTTPD" = "true" ] || [ "$REMOVE_LIGHTTPD" = "true" ]; then if [ "${DISABLE_LIGHTTPD}" = "true" ] || [ "${REMOVE_LIGHTTPD}" = "true" ]; then
if systemctl is-active lighttpd -q; then if systemctl is-active lighttpd -q; then
status_msg "Stopping lighttpd service ..." status_msg "Stopping lighttpd service ..."
sudo systemctl stop lighttpd && ok_msg "Service stopped!" sudo systemctl stop lighttpd && ok_msg "Service stopped!"
fi fi
### disable lighttpd ### disable lighttpd
if [ "$DISABLE_LIGHTTPD" = "true" ]; then if [ "${DISABLE_LIGHTTPD}" = "true" ]; then
status_msg "Disabling lighttpd ..." status_msg "Disabling lighttpd ..."
sudo systemctl disable lighttpd && ok_msg "Lighttpd service disabled!" sudo systemctl disable lighttpd && ok_msg "Lighttpd service disabled!"
### remove lighttpd ### remove lighttpd
if [ "$REMOVE_LIGHTTPD" = "true" ]; then if [ "${REMOVE_LIGHTTPD}" = "true" ]; then
status_msg "Removing lighttpd ..." status_msg "Removing lighttpd ..."
sudo apt-get remove lighttpd -y && sudo update-rc.d -f lighttpd remove && ok_msg "Lighttpd removed!" sudo apt-get remove lighttpd -y && sudo update-rc.d -f lighttpd remove && ok_msg "Lighttpd removed!"
fi fi
@@ -532,19 +545,19 @@ process_disruptive_services(){
fi fi
### handle apache2 service ### handle apache2 service
if [ "$DISABLE_APACHE2" = "true" ] || [ "$REMOVE_APACHE2" = "true" ]; then if [ "${DISABLE_APACHE2}" = "true" ] || [ "${REMOVE_APACHE2}" = "true" ]; then
if systemctl is-active apache2 -q; then if systemctl is-active apache2 -q; then
status_msg "Stopping apache2 service ..." status_msg "Stopping apache2 service ..."
sudo systemctl stop apache2 && ok_msg "Service stopped!" sudo systemctl stop apache2 && ok_msg "Service stopped!"
fi fi
### disable lighttpd ### disable lighttpd
if [ "$DISABLE_APACHE2" = "true" ]; then if [ "${DISABLE_APACHE2}" = "true" ]; then
status_msg "Disabling lighttpd ..." status_msg "Disabling lighttpd ..."
sudo systemctl disable apache2 && ok_msg "Apache2 service disabled!" sudo systemctl disable apache2 && ok_msg "Apache2 service disabled!"
### remove lighttpd ### remove lighttpd
if [ "$REMOVE_APACHE2" = "true" ]; then if [ "${REMOVE_APACHE2}" = "true" ]; then
status_msg "Removing apache2 ..." status_msg "Removing apache2 ..."
sudo apt-get remove apache2 -y && sudo update-rc.d -f apache2 remove && ok_msg "Apache2 removed!" sudo apt-get remove apache2 -y && sudo update-rc.d -f apache2 remove && ok_msg "Apache2 removed!"
fi fi
@@ -554,19 +567,19 @@ process_disruptive_services(){
process_services_dialog(){ process_services_dialog(){
#notify user about haproxy or lighttpd services found and possible issues #notify user about haproxy or lighttpd services found and possible issues
if [ "$HAPROXY_FOUND" = "true" ] || [ "$LIGHTTPD_FOUND" = "true" ] || [ "$APACHE2_FOUND" = "true" ]; then if [ "${HAPROXY_FOUND}" = "true" ] || [ "${LIGHTTPD_FOUND}" = "true" ] || [ "${APACHE2_FOUND}" = "true" ]; then
while true; do while true; do
echo echo
top_border top_border
echo -e "| ${red}Possibly disruptive/incompatible services found!${default} |" echo -e "| ${red}Possibly disruptive/incompatible services found!${default} |"
hr hr
if [ "$HAPROXY_FOUND" = "true" ]; then if [ "${HAPROXY_FOUND}" = "true" ]; then
echo -e "| ● haproxy |" echo -e "| ● haproxy |"
fi fi
if [ "$LIGHTTPD_FOUND" = "true" ]; then if [ "${LIGHTTPD_FOUND}" = "true" ]; then
echo -e "| ● lighttpd |" echo -e "| ● lighttpd |"
fi fi
if [ "$APACHE2_FOUND" = "true" ]; then if [ "${APACHE2_FOUND}" = "true" ]; then
echo -e "| ● apache2 |" echo -e "| ● apache2 |"
fi fi
hr hr
@@ -578,7 +591,7 @@ process_services_dialog(){
echo -e "| ${red}3) Skip this step (not recommended)${default} |" echo -e "| ${red}3) Skip this step (not recommended)${default} |"
bottom_border bottom_border
read -p "${cyan}###### Please choose:${default} " action read -p "${cyan}###### Please choose:${default} " action
case "$action" in case "${action}" in
1) 1)
echo -e "###### > Remove packages" echo -e "###### > Remove packages"
REMOVE_HAPROXY="true" REMOVE_HAPROXY="true"