#!/usr/bin/env bash

set -euo pipefail
set -x

if [[ -z "$1" || "$1" == "-h" ]]; then
    echo "Usage: $0 <path_to_firefox_settings_tar_file>"
    echo "Example: $0 /tmp/firefox.tar.gz"
    echo "See README.md for more info"
    exit 1
fi


firefox_settings_path="$1"

sha_256="$(sha256sum "${firefox_settings_path}" | awk '{print $1}')"
s3_url="s3://zoox-third-party/${sha_256}/$(basename ${firefox_settings_path})"

if aws s3 ls "${s3_url}" > /dev/null ; then
    echo "WARNING: ${firefox_settings_path} is already in S3. Run 'aws s3 rm ${s3_url}' if you'd like to re-upload."
    echo ""
    exit 1
else
    aws s3 cp --quiet "${firefox_settings_path}" "s3://zoox-third-party/${sha_256}/$(basename ${firefox_settings_path})" --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers
fi

url="https://s3-us-west-1.amazonaws.com/zoox-third-party/${sha_256}/$(basename ${firefox_settings_path})"


echo "---

# S3 url where firefox settings are stored
firefox_settings_url: \"${url}\"

# SHA of the firefox tar
firefox_settings_sha: \"${sha_256}\"

" > vars/main.yml
