#!/bin/bash

# Publishes the endpoint for this gear. Output format = $uuid@$dns:$appname

# Exit on any errors
set -e

function print_help {
    echo "Usage: $0 app-name namespace uuid"
    echo "Start a running application"

    echo "$0 $@" | logger -p local0.notice -t openshift_origin_publish_gear_endpoint
    exit 1
}

while getopts 'd' OPTION
do
    case $OPTION in
        d) set -x
        ;;
        ?) print_help
        ;;
    esac
done


[ $# -eq 3 ] || print_help

source "/etc/openshift/node.conf"
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util

setup_basic_hook "$@"

import_env_vars

cartridge_type=$(get_cartridge_name_from_path)

# Get gear ip address.
if ! gip=$(facter ipaddress); then
   gip=$(python -c "import socket; print socket.gethostbyname('$(hostname)')")
fi

#
# Publish this gear's endpoint.
# 
echo "${OPENSHIFT_GEAR_UUID}@${gip}:${cartridge_type};$OPENSHIFT_GEAR_DNS"
