#!/bin/bash
###############################################################
#                                                             #                                   
#   Feature :-  Script for AWS metadata instance Category     #
#   Developer :- Abhishek Sharma                              #                                    
#   Modified Date :- 31st May 2023                            #                                 
#                                                             #                                    
###############################################################

#List of running container names
CUR_DIR=$( cd "$(dirname "$0")" ; pwd -P )

jqpath=$CUR_DIR/jq_64
resolver_path=$CUR_DIR/httpclient
CONTAINER_ID=$($resolver_path http://169.254.170.2/v2/metadata | $jqpath -r '.Containers[].DockerId')
# Check if any container is running
if [ -z "$CONTAINER_ID" ]; then
                echo "No running containers found"
                        exit 1
fi
# Display the list of running containers
#echo "List of running containers:"
#echo "$CONTAINER_ID"
HOSTNAME=$(hostname)
#echo $HOSTNAME
#read HOSTNAME


if ! echo "$CONTAINER_ID" | grep -q "$HOSTNAME"; then
                echo "Invalid container name"
#			export LD_LIBRARY_PATH=$ldpath	
                        exit 1
fi

# Get the details of the entered container
cur_container_detail=$($resolver_path http://169.254.170.2/v2/metadata | $jqpath '.Containers[] | select(.DockerId == "'"$HOSTNAME"'")')

#echo "$cur_container_detail" | $jqpath -r "$field"
echo "$cur_container_detail" | $jqpath -r ".$1"

