#!/bin/bash
#Pass the 3 variable, 1-CLOUDNAME, 2-Private_IP ,3-Nginx_Filename with Path

#Add the default 
if [ "$(tail -n 1 $3)" == "}" ]; then
    sed -i '$s/}$/\
          location \/CLOUD_NAME\/ {\
\
              rewrite \/CLOUD_NAME\/(.*) \/\$1 break;\
              proxy_pass https:\/\/PRIVATE_IP:804;\
\
              proxy_set_header X-Forwarded-Host \$host;\
              proxy_set_header X-Forwarded-Server \$host;\
              proxy_set_header X-Real-IP \$remote_addr;\
              proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\
              proxy_set_header X-NginX-Proxy true;\
              proxy_set_header X-Forwarded-Proto \$scheme;\
              proxy_set_header Host \$http_host;\
              proxy_redirect off;\
             proxy_pass_request_headers on;\
        }\
}/' $3
fi

#SED to Change the default value to passing parameter for CLOUD_NAME $1 and PRIVATE_IP to $2
sed -i "s|CLOUD_NAME|$1|"  $3
sed -i "s|PRIVATE_IP|$2|"  $3
