#!/bin/bash
set -e
set -o pipefail

help()
{
    echo
    echo "$0: enables egress proxy for a given app."
    echo "Note that this will not set up egress, but rather re-enable egress that has been disabled with disable-egress."
    echo "Syntax: $0 <APP>"
    echo "  <APP> must be a valid cf app in the current space with egress disabled."
    #echo "Options:"
    #echo "  --space <SPACE>: #TODO"
    echo
    echo "To disable egress for an app, use disable-egress."
    echo "This depends on DATAGOV_DIR and CG_DIR environment variables being set."
    exit 1
}

app="$1"
space="$2"

if [ -z "$app" ]; then
    echo "No app provided."
    help
elif [ -z "$space" ]; then
    echo "No space provided."
    help
else
    # cg-egress-proxy needs jq 🤷
    if ! command -v jq &> /dev/null; then
        apt-get install jq -y
    fi

    cp "$DATAGOV_DIR/egress/acl/${space}.allow.acl" "$CG_DIR/${app}.allow.acl"
    cp "$DATAGOV_DIR/egress/acl/${space}.deny.acl" "$CG_DIR/${app}.deny.acl"

    # need to be in CG_DIR for cg-egress-proxy scripts to run correctly
    cd "$CG_DIR"
    bin/cf-deployproxy -a "$app" -s "$space-egress" -e "proxy_url"
fi
