#!/bin/bash 

set -xeuo pipefail

BUCKET="sendsteps-golden-image-config-files-${DEPLOYMENT_GROUP_NAME,,}"

echo "Copying config files from S3"
echo $BUCKET

aws s3 cp s3://$BUCKET/cron/shakespeak_recurring /opt/sendsteps/cron/shakespeak_recurring
aws s3 cp s3://$BUCKET/cron/gecko_votes.php /opt/sendsteps/cron/gecko_votes.php

# Ensure that the cron file has correct line endings
sed -i 's/\r$//' /opt/sendsteps/cron/shakespeak_recurring

ln -fs /opt/sendsteps/cron/shakespeak_recurring /etc/cron.d/shakespeak_recurring

aws s3 cp s3://$BUCKET/sendshake-config/inc/payment_settings.php /opt/sendsteps/www/sendshake/live/1.15/inc/payment_settings.php
aws s3 cp s3://$BUCKET/sendshake-config/inc/settings.php /opt/sendsteps/www/sendshake/live/1.15/inc/settings.php

cd /opt/sendsteps/www/sendshake/live/1.15
echo '#dploy.io postdeploy
RewriteEngine On
RewriteBase /1.15
RewriteRule ^.gitignore$ / [NC,L,R=404]
RewriteRule ^README.md$ / [NC,L,R=404]
RewriteRule ^cronjob.php$ / [NC,L,R=404]
RewriteRule ^inc/.*$ / [NC,L,R=404]
RewriteRule ^images/.*$ / [NC,L,R=404]
RewriteRule ^templates/.*$ / [NC,L,R=404]
RewriteRule ^query/$    query.php [NC,L]
RewriteRule ^query$             query.php [NC,L]
RewriteRule ^api/$    api.php [NC,L]
RewriteRule ^api$     api.php [NC,L]' > .htaccess