#!/usr/bin/bash

BOLD=$(tput bold)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)

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

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

# Array builder mk3000
declare -A arr=(); while read -r a b; do arr["$a"]="$b"; done < <(httpd -S | grep 'op_' | 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="[ OCA Public 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/op_*/accounts/*; do
                [ -d $i ] || continue
                n=${i##*/}
                s=${i##/home/op_}
                s=${s%%/*}
                if [ -f /var/www/ng-server-config/is-offline ]; then
                        echo -e " - ${s}_${n}"
                else
                        echo -e " * ${s}_${n}"
                fi
        done \
         | column -c $(tput cols) \
         | sed -re "s/[ \t]- [^ \t]+/$BOLD$YELLOW\0$RESET/g" -e  "s/[ \t]\\* [^ \t]+/$BOLD$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]+/$BOLD$YELLOW\0$RESET/g" -e  "s/[ \t]\\* [^ \t]+/$BOLD$GREEN\0$RESET/g"
fi

# Detail mode (duplicated code but whatever)
if [[ -n $DETAIL ]]; then
        for i in /home/op_*/accounts/*; do
                BRANCH=${i##*/}
                SITE=${i#/home/op_}
                SITE=${SITE%%/*}

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

                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
				if [ -f /var/www/ng-server-config/is-offline ]; then
                                        echo "${SITE} ${BRANCH} ${i} ${BOLD}${YELLOW}offline${RESET} ${n}"
                                else
                                        echo "${SITE} ${BRANCH} ${i} ${BOLD}${GREEN}online${RESET} ${n}"
                                fi
                        fi
                done
        done | column --table --table-columns "${BOLD}Account,Branch,Account Directory,State,Hostname${RESET}" --output-separator ' | '
fi

