#!/bin/bash

# Instance Counter
ONLINECOUNT=0
OFFLINECOUNT=0
TOTALCOUNT=0
ONLINEURLS=()

while [[ "$1" =~ ^-- ]]; do
        case "$1" in
                --online)
                        ONLINE=1
                        ;;
                --offline)
                        OFFLINE=1
                        ;;
                --total)
                        TOTAL=1
                        ;;
		--online-urls)
			LISTONLINEURLS=1
			;;
		--account)
			ACCOUNT=1
			;;
                *)
                        "Unknown option '$1'"
                        ;;
        esac
        shift
done

if [[ -z $ONLINE ]] && [[ -z $OFFLINE ]] && [[ -z $TOTAL ]] && [[ -z $LISTONLINEURLS ]] && [[ -z $ACCOUNT ]]; then
        echo "ERROR: Missing Options"
        exit 1
fi

# Save performance by only doing this once
info_schema=( $(mysql -NB -e 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA') )

# Function for looping over information schema results and seeing if the OCA exists
schema_contains () { 
    local array="$1[@]"
    local seeking=$2
    local in=1
    for element in "${!array}"; do
        if [[ $element == $seeking ]]; then
            in=0
            break
        fi
    done
    return $in
}

# Where to find OCA account dirs
OCAPATH=/home/oca/accounts

# Single account check only
if [[ -n $ACCOUNT ]]; then
	i=$1
	DBNAME=$(awk -F "=" '{IGNORECASE = 1;if (! ($0 ~ /^;/) && $0 ~ /name/) print $2}' "$OCAPATH/$i"/config.ini 2>/dev/null | sed "s/ //g" | grep "^oca_")
	schema_contains info_schema "oca_$i" || schema_contains info_schema "$DBNAME" && RESET=0
	[[ $RESET -ne 0 ]] || [[ -z $RESET ]] && echo 'ZBX_UNSUPPORTED' && exit 0
	
	# Is the account offline or online
	! [[ -e "$OCAPATH/$i/is-offline" ]] || [[ -e "$OCAPATH/$i/is-offline-$(hostname)" ]] && echo '1' || echo '0'
	
	# We're done ehre
	exit 0
fi	

# Everything else
for i in $(find $OCAPATH -mindepth 1 -maxdepth 1 -type d -printf '%f\n'); do

    # Get the Instance database name (only used as a fallback if we can't match based on the OCA dir name)
    DBNAME=$(awk -F "=" '{IGNORECASE = 1;if (! ($0 ~ /^;/) && $0 ~ /name/) print $2}' "$OCAPATH/$i"/config.ini 2>/dev/null | sed "s/ //g" | grep "^oca_")
    
    # Did the information schema contain the database
    schema_contains info_schema "oca_$i" || schema_contains info_schema "$DBNAME" && RESET=0
    [[ $RESET -ne 0 ]] && continue
    
    # Get the Instance URL
    HOST=$(awk -F "=" '{IGNORECASE = 1;if (! ($0 ~ /^;/) && $0 ~ /hostname/) print $2}' "$OCAPATH/$i"/config.ini 2>/dev/null | grep -v '\-soa\.' | grep -v '\.ha\.' | awk -F "\"" '{print $2}' | head -1)

    # Is the account offline or online
    ! [[ -e "$OCAPATH/$i/is-offline" || -e "$OCAPATH/$i/is-offline-$(hostname)" ]] && ONLINECOUNT=$((ONLINECOUNT+1)) && TOTALCOUNT=$((TOTALCOUNT+1)) && ONLINEURLS+=($HOST) && continue
    OFFLINECOUNT=$((OFFLINECOUNT+1))
    TOTALCOUNT=$((TOTALCOUNT+1))
done

[[ $ONLINE -eq 1 ]]  && echo $ONLINECOUNT
[[ $OFFLINE -eq 1 ]] && echo $OFFLINECOUNT
[[ $TOTAL -eq 1 ]]   && echo $TOTALCOUNT
[[ $LISTONLINEURLS -eq 1 ]] && echo "${ONLINEURLS[@]}"
