From d085fdf20f582e90560fe0c4d20d66fbee7974da Mon Sep 17 00:00:00 2001 From: th33xitus Date: Tue, 17 May 2022 21:06:04 +0200 Subject: [PATCH] style(mainsail.sh): improve code-style and fix some shellcheck warnings Signed-off-by: Dominik Willner th33xitus@gmail.com --- scripts/mainsail.sh | 132 +++++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 52 deletions(-) diff --git a/scripts/mainsail.sh b/scripts/mainsail.sh index a6b5654..9fe3ea3 100644 --- a/scripts/mainsail.sh +++ b/scripts/mainsail.sh @@ -17,10 +17,11 @@ set -e function install_mainsail() { ### exit early if moonraker not found - if [ -z "$(moonraker_systemd)" ]; then + if [[ -z $(moonraker_systemd) ]]; then local error="Moonraker not installed! Please install Moonraker first!" print_error "${error}" && return fi + ### checking dependencies local dep=(wget nginx) dependency_check "${dep[@]}" @@ -41,7 +42,7 @@ function install_mainsail() { ### ask user to install mjpg-streamer local install_mjpg_streamer - if [ ! -f "${SYSTEMD}/webcamd.service" ]; then + if [[ ! -f "${SYSTEMD}/webcamd.service" ]]; then while true; do echo top_border @@ -81,7 +82,7 @@ function install_mainsail() { patch_mainsail_update_manager ### install mjpg-streamer - [ "${install_mjpg_streamer}" = "true" ] && install_mjpg-streamer + [[ ${install_mjpg_streamer} == "true" ]] && install_mjpg-streamer fetch_webui_ports #WIP @@ -122,29 +123,30 @@ function install_mainsail_macros() { } function download_mainsail_macros() { - log_info "executing: download_mainsail_macros" local ms_cfg="https://raw.githubusercontent.com/mainsail-crew/MainsailOS/master/src/modules/mainsail/filesystem/home/pi/klipper_config/mainsail.cfg" - local configs + local configs path configs=$(find "${KLIPPER_CONFIG}" -type f -name "printer.cfg" | sort) - if [ -n "${configs}" ]; then + + if [[ -n ${configs} ]]; then ### create a backup of the config folder backup_klipper_config_dir for config in ${configs}; do path=$(echo "${config}" | rev | cut -d"/" -f2- | rev) - if [ ! -f "${path}/mainsail.cfg" ]; then + if [[ ! -f "${path}/mainsail.cfg" ]]; then status_msg "Downloading mainsail.cfg to ${path} ..." log_info "downloading mainsail.cfg to: ${path}" wget "${ms_cfg}" -O "${path}/mainsail.cfg" + ### replace user 'pi' with current username to prevent issues in cases where the user is not called 'pi' log_info "modify mainsail.cfg" sed -i "/^path: \/home\/pi\/gcode_files/ s/\/home\/pi/\/home\/${USER}/" "${path}/mainsail.cfg" - ### write the include to the very first line of the printer.cfg + + ### write include to the very first line of the printer.cfg if ! grep -Eq "^[include mainsail.cfg]$" "${path}/printer.cfg"; then log_info "modify printer.cfg" sed -i "1 i [include mainsail.cfg]" "${path}/printer.cfg" fi - ok_msg "Done!" fi done @@ -157,21 +159,22 @@ function download_mainsail_macros() { function mainsail_setup() { local url url=$(get_mainsail_download_url) + status_msg "Downloading Mainsail ..." - if [ -d "${MAINSAIL_DIR}" ]; then + + if [[ -d ${MAINSAIL_DIR} ]]; then rm -rf "${MAINSAIL_DIR}" fi + mkdir "${MAINSAIL_DIR}" && cd "${MAINSAIL_DIR}" wget "${url}" && ok_msg "Download complete!" - status_msg "Extracting archive ..." unzip -q -o ./*.zip && ok_msg "Done!" - status_msg "Remove downloaded archive ..." rm -rf ./*.zip && ok_msg "Done!" ### check for moonraker multi-instance and if multi-instance was found, enable mainsails remoteMode - if [ "$(moonraker_systemd | wc -w)" -gt 1 ]; then + if [[ $(moonraker_systemd | wc -w) -gt 1 ]]; then enable_mainsail_remotemode fi } @@ -181,17 +184,18 @@ function mainsail_setup() { #===================================================# function remove_mainsail_dir() { - [ ! -d "${MAINSAIL_DIR}" ] && return + [[ ! -d ${MAINSAIL_DIR} ]] && return + status_msg "Removing Mainsail directory ..." rm -rf "${MAINSAIL_DIR}" && ok_msg "Directory removed!" } function remove_mainsail_config() { - if [ -e "/etc/nginx/sites-available/mainsail" ]; then + if [[ -e "/etc/nginx/sites-available/mainsail" ]]; then status_msg "Removing Mainsail configuration for Nginx ..." sudo rm "/etc/nginx/sites-available/mainsail" && ok_msg "File removed!" fi - if [ -L "/etc/nginx/sites-enabled/mainsail" ]; then + if [[ -L "/etc/nginx/sites-enabled/mainsail" ]]; then status_msg "Removing Mainsail Symlink for Nginx ..." sudo rm "/etc/nginx/sites-enabled/mainsail" && ok_msg "File removed!" fi @@ -200,7 +204,8 @@ function remove_mainsail_config() { function remove_mainsail_logs() { local files files=$(find /var/log/nginx -name "mainsail*") - if [ -n "${files}" ]; then + + if [[ -n ${files} ]]; then for file in ${files}; do status_msg "Removing ${file} ..." sudo rm -f "${file}" @@ -212,6 +217,7 @@ function remove_mainsail_logs() { function remove_mainsail_log_symlinks() { local files files=$(find "${KLIPPER_LOGS}" -name "mainsail*" 2> /dev/null | sort) + if [[ -n ${files} ]]; then for file in ${files}; do status_msg "Removing ${file} ..." @@ -256,12 +262,12 @@ function get_mainsail_status() { ### count+1 for each found data-item from array local filecount=0 for data in "${data_arr[@]}"; do - [ -e "${data}" ] && filecount=$(("${filecount}" + 1)) + [[ -e ${data} ]] && filecount=$(( filecount + 1 )) done - if (( filecount == ${#data_arr[*]})); then + if (( filecount == ${#data_arr[*]} )); then status="Installed!" - elif ((filecount == 0)); then + elif (( filecount == 0 )); then status="Not installed!" else status="Incomplete!" @@ -270,15 +276,17 @@ function get_mainsail_status() { } function get_local_mainsail_version() { + [[ ! -f "${MAINSAIL_DIR}/.version" ]] && return + local version - [ ! -f "${MAINSAIL_DIR}/.version" ] && return version=$(head -n 1 "${MAINSAIL_DIR}/.version") echo "${version}" } function get_remote_mainsail_version() { - local version [[ ! $(dpkg-query -f'${Status}' --show curl 2>/dev/null) = *\ installed ]] && return + + local version version=$(get_mainsail_download_url | rev | cut -d"/" -f2 | rev) echo "${version}" } @@ -288,7 +296,8 @@ function compare_mainsail_versions() { local versions local_ver remote_ver local_ver="$(get_local_mainsail_version)" remote_ver="$(get_remote_mainsail_version)" - if [ "${local_ver}" != "${remote_ver}" ]; then + + if [[ ${local_ver} != "${remote_ver}" ]]; then versions="${yellow}$(printf " %-14s" "${local_ver}")${white}" versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" # add mainsail to the update all array for the update all function in the updater @@ -298,6 +307,7 @@ function compare_mainsail_versions() { versions+="|${green}$(printf " %-13s" "${remote_ver}")${white}" MAINSAIL_UPDATE_AVAIL="false" fi + echo "${versions}" } @@ -307,8 +317,9 @@ function compare_mainsail_versions() { function print_theme_list() { local i=0 theme_list=${1} + while IFS="," read -r col1 col2 col3 col4; do - if [ "${col1}" != "name" ]; then + if [[ ${col1} != "name" ]]; then printf "| ${i}) %-50s|\n" "[${col1}]" fi i=$((i+1)) @@ -316,7 +327,7 @@ function print_theme_list() { } function ms_theme_installer_menu() { - local theme_author theme_repo theme_name theme_note theme_url + local theme_list theme_author theme_repo theme_name theme_note theme_url local theme_csv_url="https://raw.githubusercontent.com/mainsail-crew/docs/master/_data/themes.csv" theme_list=$(curl -s -L "${theme_csv_url}") @@ -344,14 +355,14 @@ function ms_theme_installer_menu() { while true; do read -p "${cyan}Install theme:${white} " option - if ((option > 0 && option < ${#theme_name[@]})); then + if (( option > 0 && option < ${#theme_name[@]} )); then theme_url="https://github.com/${theme_author[${option}]}/${theme_repo[${option}]}" ms_theme_install "${theme_url}" "${theme_name[${option}]}" "${theme_note[${option}]}" break - elif [[ "${option}" == "R" || "${option}" == "r" ]]; then + elif [[ ${option} == "R" || ${option} == "r" ]]; then ms_theme_delete break - elif [[ "${option}" == "B" || "${option}" == "b" ]]; then + elif [[ ${option} == "B" || ${option} == "b" ]]; then advanced_menu break else @@ -364,27 +375,28 @@ function ms_theme_installer_menu() { function ms_theme_install() { local path moonraker_count theme_url=${1} theme_name theme_note theme_name=${2} theme_note=${3} - - ### check and select printer if there is more than 1 path="$(get_klipper_cfg_dir)" moonraker_count=$(moonraker_systemd | wc -w) - if ((moonraker_count > 1)); then + + if (( moonraker_count > 1 )); then top_border echo -e "| More than one printer was found on this system! | " echo -e "| Please select the printer to which you want to | " echo -e "| apply the previously selected action. | " bottom_border + #TODO: list printers depending on moonraker instance names for the user to select from read -p "${cyan}Select printer:${white} " printer path="${path}/printer_${printer}" fi - [ ! -d "${path}" ] && mkdir -p "${path}" - [ -d "${path}/.theme" ] && rm -rf "${path}/.theme" + [[ ! -d ${path} ]] && mkdir -p "${path}" + [[ -d "${path}/.theme" ]] && rm -rf "${path}/.theme" + status_msg "Installing ${theme_name[${option}]} ..." cd "${path}" && if git clone "${theme_url}" ".theme"; then ok_msg "Theme installation complete!" - [ -n "${theme_note}" ] && echo "${yellow}###### Theme Info: ${theme_note}${white}" + [[ -n ${theme_note} ]] && echo "${yellow}###### Theme Info: ${theme_note}${white}" ok_msg "Please remember to delete your browser cache!\n" else error_msg "Theme installation failed!\n" @@ -392,19 +404,22 @@ function ms_theme_install() { } function ms_theme_delete() { - local path + local path moonraker_count path="$(get_klipper_cfg_dir)" moonraker_count=$(moonraker_systemd | wc -w) - if ((moonraker_count > 1)); then + + if (( moonraker_count > 1 )); then top_border echo -e "| More than one printer was found on this system! | " echo -e "| Please select the printer to which you want to | " echo -e "| apply the previously selected action. | " bottom_border + #TODO: list all found .theme folders and let the user select read -p "${cyan}Select printer:${white} " printer path="${path}/printer_${printer}" fi - if [ -d "${path}/.theme" ]; then + + if [[ -d "${path}/.theme" ]]; then status_msg "Removing Theme ..." rm -rf "${path}/.theme" && ok_msg "Theme removed!\n" else @@ -417,7 +432,7 @@ function ms_theme_delete() { #================================================# function get_mainsail_download_url() { - local latest_tag latest_url stable_tag stable_url url + local tags latest_tag latest_url stable_tag stable_url url tags=$(curl -s "${MAINSAIL_TAGS}" | grep "name" | cut -d'"' -f4) ### latest download url including pre-releases (alpha, beta, rc) @@ -429,7 +444,7 @@ function get_mainsail_download_url() { stable_url="https://github.com/mainsail-crew/mainsail/releases/download/${stable_tag}/mainsail.zip" read_kiauh_ini "${FUNCNAME[0]}" - if [ "${mainsail_install_unstable}" == "true" ]; then + if [[ ${mainsail_install_unstable} == "true" ]]; then url="${latest_url}" echo "${url}" else @@ -439,12 +454,18 @@ function get_mainsail_download_url() { } function mainsail_port_check() { - if [ "${MAINSAIL_ENABLED}" = "false" ]; then - if [ "${SITE_ENABLED}" = "true" ]; then + if [[ ${MAINSAIL_ENABLED} == "false" ]]; then + + if [[ ${SITE_ENABLED} == "true" ]]; then status_msg "Detected other enabled interfaces:" - [ "${OCTOPRINT_ENABLED}" = "true" ] && echo -e " ${cyan}● OctoPrint - Port: ${OCTOPRINT_PORT}${white}" - [ "${FLUIDD_ENABLED}" = "true" ] && echo -e " ${cyan}● Fluidd - Port: ${FLUIDD_PORT}${white}" - if [ "${FLUIDD_PORT}" = "80" ] || [ "${OCTOPRINT_PORT}" = "80" ]; then + + [[ ${OCTOPRINT_ENABLED} == "true" ]] && \ + echo -e " ${cyan}● OctoPrint - Port: ${OCTOPRINT_PORT}${white}" + + [[ ${FLUIDD_ENABLED} == "true" ]] && \ + echo -e " ${cyan}● Fluidd - Port: ${FLUIDD_PORT}${white}" + + if [[ ${FLUIDD_PORT} == "80" ]] || [[ ${OCTOPRINT_PORT} == "80" ]]; then PORT_80_BLOCKED="true" select_mainsail_port fi @@ -453,21 +474,23 @@ function mainsail_port_check() { SET_LISTEN_PORT=${DEFAULT_PORT} fi SET_NGINX_CFG="true" + else SET_NGINX_CFG="false" fi } function select_mainsail_port() { - if [ "${PORT_80_BLOCKED}" = "true" ]; then + local new_port + if [[ ${PORT_80_BLOCKED} == "true" ]]; then echo top_border echo -e "| ${red}!!!WARNING!!!${white} |" echo -e "| ${red}You need to choose a different port for Mainsail!${white} |" echo -e "| ${red}The following web interface is listening at port 80:${white} |" blank_line - [ "${OCTOPRINT_PORT}" = "80" ] && echo "| ● OctoPrint |" - [ "${FLUIDD_PORT}" = "80" ] && echo "| ● Fluidd |" + [[ ${OCTOPRINT_PORT} == "80" ]] && echo "| ● OctoPrint |" + [[ ${FLUIDD_PORT} == "80" ]] && echo "| ● Fluidd |" blank_line 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${white} |" @@ -477,10 +500,11 @@ function select_mainsail_port() { echo -e "| input. So make sure to choose a valid port! |" bottom_border while true; do - read -p "${cyan}Please enter a new Port:${white} " NEW_PORT - if [ "${NEW_PORT}" != "${FLUIDD_PORT}" ] && [ "${NEW_PORT}" != "${OCTOPRINT_PORT}" ]; then - echo "Setting port ${NEW_PORT} for Mainsail!" - SET_LISTEN_PORT=${NEW_PORT} + #TODO implement regex input validation for numbers only + read -p "${cyan}Please enter a new Port:${white} " new_port + if [[ ${new_port} != "${FLUIDD_PORT}" ]] && [[ ${new_port} != "${OCTOPRINT_PORT}" ]]; then + echo "Setting port ${new_port} for Mainsail!" + SET_LISTEN_PORT=${new_port} break else echo "That port is already taken! Select a different one!" @@ -490,7 +514,8 @@ function select_mainsail_port() { } function enable_mainsail_remotemode() { - [ ! -f "${MAINSAIL_DIR}/config.json" ] && return + [[ ! -f "${MAINSAIL_DIR}/config.json" ]] && return + rm -f "${MAINSAIL_DIR}/config.json" echo -e "{\n \"remoteMode\":true\n}" >> "${MAINSAIL_DIR}/config.json" } @@ -498,10 +523,12 @@ function enable_mainsail_remotemode() { function patch_mainsail_update_manager() { local moonraker_configs moonraker_configs=$(find "$(get_klipper_cfg_dir)" -type f -name "moonraker.conf" | sort) + for conf in ${moonraker_configs}; do if ! grep -Eq "[update_manager mainsail]" "${conf}"; then ### add new line to conf if it doesn't end with one [[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}" + ### add Mainsails update manager section to moonraker.conf status_msg "Adding Mainsail to update manager in file:\n ${conf}" /bin/sh -c "cat >> ${conf}" << MOONRAKER_CONF @@ -512,6 +539,7 @@ channel: stable repo: mainsail-crew/mainsail path: ~/mainsail MOONRAKER_CONF + fi done } \ No newline at end of file