diff --git a/scripts/network_functions.sh b/scripts/network_functions.sh index 784bd3b..006c24c 100755 --- a/scripts/network_functions.sh +++ b/scripts/network_functions.sh @@ -1,52 +1,53 @@ function set_nginx_cfg(){ - if [ "$SET_NGINX_CFG" = "true" ]; then + if [ "${SET_NGINX_CFG}" = "true" ]; then + local cfg="${SRCDIR}/kiauh/resources/${1}" #check for dependencies dep=(nginx) - dependency_check + dependency_check "${dep[@]}" #execute operations - status_msg "Creating Nginx configuration for $1 ..." + status_msg "Creating Nginx configuration for ${1} ..." #copy content from resources to the respective nginx config file - cat ${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg > ${SRCDIR}/kiauh/resources/$1 + cat "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" > "${cfg}" ##edit the nginx config file before moving it - sed -i "s/<>/$1/g" ${SRCDIR}/kiauh/resources/$1 - if [ "$SET_LISTEN_PORT" != "$DEFAULT_PORT" ]; then + sed -i "s/<>/${1}/g" "${cfg}" + if [ "${SET_LISTEN_PORT}" != "${DEFAULT_PORT}" ]; then status_msg "Configuring port for $1 ..." #set listen port ipv4 - sed -i "s/listen\s[0-9]*;/listen $SET_LISTEN_PORT;/" ${SRCDIR}/kiauh/resources/$1 + sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg}" #set listen port ipv6 - sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:$SET_LISTEN_PORT;/" ${SRCDIR}/kiauh/resources/$1 + sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg}" fi #set correct user - if [ "$1" = "mainsail" ] || [ "$1" = "fluidd" ]; then - sudo sed -i "/root/s/pi/${USER}/" ${SRCDIR}/kiauh/resources/$1 + if [ "${1}" = "mainsail" ] || [ "${1}" = "fluidd" ]; then + sudo sed -i "/root/s/pi/${USER}/" "${cfg}" fi #moving the config file into correct directory - sudo mv ${SRCDIR}/kiauh/resources/$1 /etc/nginx/sites-available/$1 + sudo mv "${cfg}" "/etc/nginx/sites-available/${1}" ok_msg "Nginx configuration for $1 was set!" - if [ "$SET_LISTEN_PORT" != "" ]; then - ok_msg "$1 listening on port $SET_LISTEN_PORT!" + if [ -n "${SET_LISTEN_PORT}" ]; then + ok_msg "${1} listening on port ${SET_LISTEN_PORT}!" else - ok_msg "$1 listening on default port $DEFAULT_PORT!" + ok_msg "${1} listening on default port ${DEFAULT_PORT}!" fi #remove nginx default config - [ -e /etc/nginx/sites-enabled/default ] && sudo rm /etc/nginx/sites-enabled/default + [ -e "/etc/nginx/sites-enabled/default" ] && sudo rm "/etc/nginx/sites-enabled/default" #create symlink for own sites - [ ! -e /etc/nginx/sites-enabled/$1 ] && sudo ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/ + [ ! -e "/etc/nginx/sites-enabled/${1}" ] && sudo ln -s "/etc/nginx/sites-available/${1}" "/etc/nginx/sites-enabled/" restart_nginx fi } function read_listen_port(){ - LISTEN_PORT=$(grep listen /etc/nginx/sites-enabled/$1 | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1) + LISTEN_PORT=$(grep listen "/etc/nginx/sites-enabled/${1}" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1) } function detect_enabled_sites(){ #check if there is another UI config already installed #and reads the port they are listening on - if [ -e /etc/nginx/sites-enabled/mainsail ]; then + if [ -e "/etc/nginx/sites-enabled/mainsail" ]; then SITE_ENABLED="true" && MAINSAIL_ENABLED="true" read_listen_port "mainsail" - MAINSAIL_PORT=$LISTEN_PORT + MAINSAIL_PORT=${LISTEN_PORT} #echo "debug: Mainsail listens on port: $MAINSAIL_PORT" else MAINSAIL_ENABLED="false" @@ -54,7 +55,7 @@ function detect_enabled_sites(){ if [ -e /etc/nginx/sites-enabled/fluidd ]; then SITE_ENABLED="true" && FLUIDD_ENABLED="true" read_listen_port "fluidd" - FLUIDD_PORT=$LISTEN_PORT + FLUIDD_PORT=${LISTEN_PORT} #echo "debug: Fluidd listens on port: $FLUIDD_PORT" else FLUIDD_ENABLED="false" @@ -62,7 +63,7 @@ function detect_enabled_sites(){ if [ -e /etc/nginx/sites-enabled/octoprint ]; then SITE_ENABLED="true" && OCTOPRINT_ENABLED="true" read_listen_port "octoprint" - OCTOPRINT_PORT=$LISTEN_PORT + OCTOPRINT_PORT=${LISTEN_PORT} #echo "debug: OctoPrint listens on port: $OCTOPRINT_PORT" else OCTOPRINT_ENABLED="false" @@ -81,7 +82,7 @@ function create_custom_hostname(){ bottom_border while true; do read -p "${cyan}###### Do you want to change the hostname? (y/N):${white} " yn - case "$yn" in + case "${yn}" in Y|y|Yes|yes) user_input_hostname break;; @@ -105,13 +106,13 @@ function user_input_hostname(){ bottom_border while true; do read -p "${cyan}###### Please set the new hostname:${white} " NEW_HOSTNAME - if [[ $NEW_HOSTNAME =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then - ok_msg "'$NEW_HOSTNAME' is a valid hostname!" + if [[ ${NEW_HOSTNAME} =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then + ok_msg "'${NEW_HOSTNAME}' is a valid hostname!" HOSTNAME_VALID="true" while true; do echo - read -p "${cyan}###### Do you want '$NEW_HOSTNAME' to be the new hostname? (Y/n):${white} " yn - case "$yn" in + read -p "${cyan}###### Do you want '${NEW_HOSTNAME}' to be the new hostname? (Y/n):${white} " yn + case "${yn}" in Y|y|Yes|yes|"") echo -e "###### > Yes" HOSTENAME_CONFIRM="true" @@ -122,22 +123,21 @@ function user_input_hostname(){ HOSTENAME_CONFIRM="false" break;; *) - print_unkown_cmd - print_msg && clear_msg;; + print_error "Invalid command!";; esac done break else - warn_msg "'$NEW_HOSTNAME' is not a valid hostname!" + warn_msg "'${NEW_HOSTNAME}' is not a valid hostname!" fi done } function set_hostname(){ - if [ "$HOSTNAME_VALID" = "true" ] && [ "$HOSTENAME_CONFIRM" = "true" ]; then + if [ "${HOSTNAME_VALID}" = "true" ] && [ "${HOSTENAME_CONFIRM}" = "true" ]; then #check for dependencies dep=(avahi-daemon) - dependency_check + dependency_check "${dep[@]}" #execute operations #get current hostname and write to variable HOSTNAME=$(hostname) @@ -145,19 +145,19 @@ function set_hostname(){ if [ -f /etc/hosts ]; then status_msg "Creating backup of hosts file ..." get_date - sudo cp /etc/hosts /etc/hosts."$current_date".bak + sudo cp "/etc/hosts /etc/hosts.${current_date}.bak" ok_msg "Backup done!" - ok_msg "File:'/etc/hosts."$current_date".bak'" + ok_msg "File:'/etc/hosts.${current_date}.bak'" else sudo touch /etc/hosts fi #set hostname in /etc/hostname - status_msg "Setting hostname to '$NEW_HOSTNAME' ..." + status_msg "Setting hostname to '${NEW_HOSTNAME}' ..." status_msg "Please wait ..." - sudo hostnamectl set-hostname "$NEW_HOSTNAME" + sudo hostnamectl set-hostname "${NEW_HOSTNAME}" #write new hostname to /etc/hosts status_msg "Writing new hostname to /etc/hosts ..." - echo "127.0.0.1 $NEW_HOSTNAME" | sudo tee -a /etc/hosts &>/dev/null + echo "127.0.0.1 ${NEW_HOSTNAME}" | sudo tee -a /etc/hosts &>/dev/null ok_msg "New hostname successfully configured!" ok_msg "Remember to reboot for the changes to take effect!" fi