#!/usr/bin/bash

YELLOW=$(echo -en '\e[33m')
GREEN=$(echo -en '\e[32;1m')
RESET=$(echo -en '\e[0m')


while [[ "$1" =~ ^-- ]]; do
        case "$1" in
                --banner)
                        BANNER=1
                        ;;

                --detail)
                        DETAIL=1
                        ;;
                *)
                        "Unknown option '$1'"
                        ;;
        esac
        shift
done

# These are option independent
# Create array of this host's IP addresses
readarray -t hostips < <(dig +short $HOSTNAME | grep -vE '[a-zA-Z]' | sort -u)

# Array builder mk3000
declare -A arr=(); while read -r a b; do arr["$a"]="$b"; done < <(httpd -S | grep 'nif_' | grep -v 'system-default-vhost' | awk '/port 443 namevhost/ { print $4,$31 }' | sed -re 's/\"//g' -e 's/:1\)//g')
# Don't proceed if we couldn't read httpd configuration without an error
[[ $? -ne 0 ]] && exit 0

# Detault to banner mode
if [[ -z $DETAIL ]]; then
	BANNER=1
fi

# Banner mode
if [[ -n $BANNER ]]; then
	COLUMNS=$(tput cols)
	TITLE="[ NIF Instances on this Server ]"
	PAD=$((($COLUMNS-${#TITLE})/2))
	printf "%0.${PAD}d%s%0.${PAD}d\n" 0 "$TITLE" 0 | tr 0 =
	for i in /home/nif_*/accounts/*; do
		# Build our own absolute path to vhost
		BRANCH=${i##*/}
	        SITE=${i#/home/nif_}
	        SITE=${SITE%%/*}

		ACCTNAME=$(echo $i | sed -re 's/.*(nif_[a-zA-Z_-]*).*/\1/g')
		TGTVHOST="/etc/httpd/vhosts.d/${ACCTNAME}_${BRANCH}.conf"

		# Where:
		# (key)n = NIF hostname
		# (value)n = Resolved absolute path to nif vhost, and the key is the 
		# both of these come from an apache configuration parse
		for n in "${!arr[@]}"; do
			# The hostname of this NIF is the key of the assoc. array when the array value matches our vhost path
			if [[ "${arr[$n]}" == $TGTVHOST ]]; then
				readarray -t ips < <(dig +short $n | grep -vE '[a-zA-Z]' | sort -u)

				# Comparison of hostips to ips of nif to determine whether the instance points to this server (is active)
				for j in "${ips[@]}"; do
					for k in "${hostips[@]}"; do
						[[ $j == $k ]] && hasIp=$j && break
					done
				done

				# Build array of active sites
				if [[ -n $hasIp ]]; then
					echo -e " * ${SITE}_${BRANCH}"
				else
					echo -e " - ${SITE}_${BRANCH}"
				fi
				# Reset this array for next iteration
				hasIp=
			fi
		done
	done \
	 | column -c $(tput cols) \
	 | sed -re "s/[ \t]- [^ \t]+/$YELLOW\0$RESET/g" -e  "s/[ \t]\\* [^ \t]+/$GREEN\0$RESET/g"

	LEGEND="=====[ * Active-Instances ]======[ - Standby-Instances ]===="
	PAD=$(($COLUMNS-${#LEGEND}))
	printf -- "%s%0.${PAD}d\n" "$LEGEND" 0 | tr 0 = \
	 | sed -re "s/[ \t]- [^ \t]+/$YELLOW\0$RESET/g" -e  "s/[ \t]\\* [^ \t]+/$GREEN\0$RESET/g"
fi


# Detail mode (duplicated code but whatever)
if [[ -n $DETAIL ]]; then
	printf "%.0s-" {1..-120}; echo
	printf "|  %-15s | %-5s | %-40s |  %-8s |  %-35s |\n" Account Branch Account\ Directory State Hostname
	printf "%.0s-" {1..-120}; echo
	for i in /home/nif_*/accounts/*; do
                BRANCH=${i##*/}
                SITE=${i#/home/nif_}
                SITE=${SITE%%/*}

                ACCTNAME=$(echo $i | sed -re 's/.*(nif_[a-zA-Z_-]*).*/\1/g')
                TGTVHOST="/etc/httpd/vhosts.d/${ACCTNAME}_${BRANCH}.conf"

                # Where:
                # (key)n = NIF hostname
                # (value)n = Resolved absolute path to nif vhost, and the key is the 
                # both of these come from an apache configuration parse
                for n in "${!arr[@]}"; do
                        # The hostname of this NIF is the key of the assoc. array when the array value matches our vhost path
                        if [[ "${arr[$n]}" == $TGTVHOST ]]; then
                                readarray -t ips < <(dig +short $n | grep -vE '[a-zA-Z]' | sort -u)

                                # Comparison of hostips to ips of nif to determine whether the instance points to this server (is active)
                                for j in "${ips[@]}"; do
                                        for k in "${hostips[@]}"; do
                                                [[ $j == $k ]] && hasIp=$j && break
                                        done
                                done

                                # Build array of active sites
                                if [[ -z ${Array3[@]} ]]; then
					printf "| %-15s | %-5s | %-40s | %-8s | %-50s|\n " ${SITE} ${BRANCH} ${i} ${GREEN}online${RESET} ${n}
                                else
					printf "| %-15s | %-5s | %-40s | %-8s | %-50s|\n " ${SITE} ${BRANCH} ${i} ${YELLOW}offline${RESET} ${n}
                                        # Reset this array for next iteration
                                        Array3=
                                fi
                        fi
		done
	done | column -t
	printf "%.0s-" {1..-120}
	echo
fi 
