#!/bin/bash
NAME=""                                   # Name of person to greet.
TIMES=1                                   # Number of greetings to give. 

usage() {                                 # Function: Print a help message.
  echo "Usage: $0 [ -f FUNC_NAME ] [ -i IP ] [ -p PORT ]" 1>&2 
}


exit_abnormal() {                         # Function: Exit with error.
  usage
  exit 1
}


while getopts ":b:f:i:p:" options; do         # Loop: Get the next option;
                                          # use silent error checking;
                                          # options n and t take arguments.
  case "${options}" in
    b) 
      BUILD_NAME=${OPTARG}
      ;;                                  # 
    f)                                    # If the option is n,
      FUNC_NAME=${OPTARG}                      # set $NAME to specified value.
      ;;
    i)                                    # If the option is t,
      IP=${OPTARG}
      ;;                     # Set $TIMES to specified value.
    p)
      echo "Inside port"
      PORT=${OPTARG}
      echo "PORT is ${PORT}"
      ;;  
    *)                                    # If unknown (any other) option:
      exit_abnormal                       # Exit abnormally.
      ;;
  esac
done


echo "build name ${BUILD_NAME},Function name ${FUNC_NAME}, ip ${IP}, port ${PORT}"

# aws lambda get-function-configuration --function-name  ${FUNC_NAME} --output json

zip ${BUILD_NAME}.zip ${BUILD_NAME}

aws lambda update-function-code --function-name ${FUNC_NAME} --zip-file fileb://${BUILD_NAME}.zip

aws lambda update-function-configuration --function-name ${FUNC_NAME} --environment "{\"Variables\":{\"CAV_APP_AGENT_PROXYIP\":\"${IP}\",\"CAV_APP_AGENT_PROXYPORT\":\"${PORT}\"}}" --handler ${BUILD_NAME}
