#!/usr/bin/bash

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

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 $i/is-offline ] || [ -f $i/code/is-offline ] || [ -f $i/is-offline-`hostname` ]; then
                echo -e " - ${s}_${n}"
        else
                echo -e " * ${s}_${n}"
        fi
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"
