#!/bin/bash

if [[ -e "./aws.config" ]]; then
    export AWS_CONFIG_FILE="./aws.config"
else
    export AWS_CONFIG_FILE="$HOME/.aws/config"
fi

ENVIRONMENT=""
while [[ "$1" != "" ]]; do
    case "$1" in
        "--staging") ENVIRONMENT="staging";;
        "--production") ENVIRONMENT="production";;
        *) die "Please specify environment: --staging or --production";;
    esac
done

SOURCE_DIR="./dist/"
SITE="new.crafting-guide.com"
[[ "$ENVIRONMENT" == "production" ]] && SITE="crafting-guide.com"
TARGET_DIR="s3://$SITE/"

S3CMD_CFG="$HOME/.s3cfg"
echo "[default]" > "$S3CMD_CFG"
cat $AWS_CONFIG_FILE | grep "aws_access_key_id" | sed "s/aws_access_key_id/access_key/" >> "$S3CMD_CFG"
cat $AWS_CONFIG_FILE | grep "aws_secret_access_key" | sed "s/aws_secret_access_key/secret_key/" >> "$S3CMD_CFG"

HEADERS="--add-header=cache-control:max-age=1800"
GZIP="--add-header=content-encoding:gzip"
CMD="sync $SOURCE_DIR $TARGET_DIR -r -P $HEADERS --exclude=*"

echo "Preparing to upload to S3..."
s3cmd $CMD $GZIP -m 'text/css; charset=utf8'        --include="*.css"
s3cmd $CMD $GZIP -m 'text/html; charset=utf8'       --include="*.html"
s3cmd $CMD $GZIP -m 'text/javascript; charset=utf8' --include="*.js"
s3cmd $CMD $GZIP -m 'application/json'              --include="*.json"
s3cmd $CMD       -m 'audio/mpeg3'                   --include="*.mp3"
s3cmd $CMD       -m 'image/png'                     --include="*.png"
echo "S3 Upload Complete"
