#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" LOCK_DIR="$(mktemp -d)" log_file="${LOCK_DIR}/${script_name%%.*}.log" backup_date="$(date +%Y-%m-%d-%H%M)" nginx_file_mime_types=/etc/nginx/mime.types # ---------- # Base Function(s) # ---------- clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n " $*\\c" 1>&2 else echo -e -n " $*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ] $*" else echo -e " [ Fatal ] $*" fi echo "" if $terminal ; then echo -e " \033[1mScript terminated\033[m.." else echo -e " Script terminated.." fi echo "" rm -rf $LOCK_DIR exit 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ] $*" else echo " [ Error ] $*" fi echo "" } warn (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarn\033[m ] $*" else echo " [ Warn ] $*" fi echo "" fi } info (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mInfo\033[m ] $*" else echo " [ Info ] $*" fi echo "" fi } ok (){ if $LOGGING || $terminal ; then echo "" if $terminal ; then echo -e " [ \033[32m\033[1mOk\033[m ] $*" else echo " [ Ok ] $*" fi echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" fi } blank_line() { if $terminal ; then echo "" fi } echo_wait(){ if $terminal ; then echo -en "\033[75G[ \033[5m\033[1m...\033[m ]" fi } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" fi restart_nginx_service=false if [[ ! -d "/etc/nginx" ]] ; then fatal "No Nginx installation found!" fi if [[ ! -f "/etc/nginx/mime.types" ]] ; then fatal "File \033[1m/etc/nginx/mime.types\033[m not found!" fi blank_line echononl "Add .mjs to known JavaScripz MIME types .." if $(grep -E -q "^\s*application/javascript\s+.*mjs" ${nginx_file_mime_types} 2> /dev/null) ; then echo_skipped else if $(grep -E -q "^\s*application/javascript\s+" ${nginx_file_mime_types} 2> /dev/null) ; then perl -i -n -p -e "s#^(\s*application/javascript\s+.*);#\1 mjs;#" ${nginx_file_mime_types} 2> ${log_file} if [[ $? -ne 0 ]]; then echo_failed error "$(cat $log_file)" else echo_ok restart_nginx_service=true fi else echo_skipped warn "No line 'application/javascript' found!" echononl "Add entry \033[1mapplication/javascript js mjs\033[m to mime.types." perl -i -n -p -e "s#^(\s*types\s+{)#\1\n\n application/javascript js mjs;\n#" \ ${nginx_file_mime_types} 2> ${log_file} if [[ $? -ne 0 ]]; then echo_failed error "$(cat $log_file)" else echo_ok restart_nginx_service=true fi fi fi blank_line echononl "Restart Nginx WebsService" if $restart_nginx_service ; then systemctl start nginx > "$log_file" 2>&1 if [[ $? -ne 0 ]]; then echo_failed error "$(cat $log_file)" else echo_ok fi else echo_skipped fi clean_up 0