#!/bin/sh 
#set -x

function rootUserIdAssert()
{
   if [ "$(id -u)" != "0" ]; then
      echo "You do not have permission to execute this command (try sudo)."
      exit 1
   fi
}

function usage()
{
        echo
        echo "Use this tool to display a list of Presence Services attributes that have been overridden from their default value. This tool will not display Service Attributes"
        echo "that have not been overridden. This tool will also not display the values of encrypted Service Attributes (e.g. passwords, authentication tokens)." 
        echo 
        echo "Usage for $0: A command to list service attributes."
        echo "  List Service Global Overridden attributes :   -g"
        echo "  List Service Cluster Overridden attributes:   -c cluster-name"
        echo "  Display Cluster Names :                       -l"
        echo
        exit 1
}

function run_db_cmd_quietly()
{
        mgmtia -t -q -c "$*"
}

function output_db_cmd()
{
        mgmtia -c "$*"
}

function list_clusters()
{
        output_db_cmd "select name from zr_cluster order by name"
        exit 0
}

function list_attr()
{
        typeset cid=$(run_db_cmd_quietly "select id from zr_cluster where name = '$1'")
        if [ "$cid" = "" ]
        then
                echo "Cluster $1 not found."
                exit 1
        fi
        typeset sid=$(run_db_cmd_quietly "select id from zr_service where service_name = 'PresenceServices'")
        if [ "$sid" = "" ]
        then
                echo "Presence Services is not installed."
                exit 1
        fi
        echo "Service Cluster Overridden Attributes for $1"
        echo ""
        output_db_cmd "select zag.name as group, zam.display_name as name, zsc.cluster_override_value as cluster_value from zr_map_sattribute_cluster zsc join zr_attribute_metadata zam on zsc.sattribute_id = zam.id join zr_join_service_metadata zjsm on zam.id = zjsm.attribute_metadata_id join zr_attribute_group zag on zam.attribute_group_id = zag.id where zsc.cluster_id = $cid and zjsm.service_id = $sid order by zag.id, zam.id"
        echo "** Note: passwords are not decrypted **"
        exit 0
}

function list_global_attr()
{
        typeset sid=$(run_db_cmd_quietly "select id from zr_service where service_name = 'PresenceServices'")
        if [ "$sid" = "" ]
        then
                echo "Presence Services is not installed."
                exit 1
        fi
        echo "Service Global Overridden Attributes"
        echo ""
        output_db_cmd "select zag.name as group, zam.display_name as name, zam.enterprise_override_value as global_value from zr_attribute_metadata zam join zr_join_service_metadata zjsm on zam.id = zjsm.attribute_metadata_id join zr_attribute_group zag on zam.attribute_group_id = zag.id where zjsm.service_id = $sid and length(enterprise_override_value) != 0 and global = false order by zag.id, zam.id"
        echo "** Note: passwords are not decrypted **"
        exit 0
}


# MAIN
rootUserIdAssert
while getopts "lgc:h?" CMD_OPTION
do
    case "${CMD_OPTION}" in
        l) list_clusters;;
        g) list_global_attr;;
        c) list_attr ${OPTARG};;
        h | [?]) usage;;
    esac
done

usage
exit 0
