set -euo pipefail
#set -eo pipefail

DATE=$(date +'%m/%d/%Y %H:%M')

SLACK_WEBHOOK="https://hooks.slack.com/services/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"

function content_message() {

    local slack_msg_header
    local slack_msg_body
    local slack_channel
    custom_header=${BASH_ARGV[2]}
    channel=${BASH_ARGV[1]}
    color=${BASH_ARGV[0]}

    #color="#33acff" # Blue
    #color="#ff0000" # Red

    #slack_msg_header="Pipeline for *${CI_PROJECT_NAME}* in *${CI_ENVIRONMENT_NAME}* started"
    slack_msg_header=${custom_header}
    slack_channel=${channel}

    cat <<-SLACK
            {
                "channel": "${slack_channel}",
                "attachments": [
                    {
                        "color": "${color}",
                        "text": "${slack_msg_header}",
                        "footer": "Branch: ${CI_COMMIT_REF_NAME} \n Job_URL: ${CI_JOB_URL} \n Date: ${DATE}",
                    }
                ]
}
SLACK
}

function send_slack_message() {
    custom_header=$1
    channel=$2
    color=$3
    echo "custom_header=$custom_header | channel=$channel | color=$color"
    curl -sX POST                                           \
        --data-urlencode "payload=$(content_message $custom_header $channel $color)"  \
        "${SLACK_WEBHOOK}"

}

send_slack_message "$1" "$2" "$3"