#!/usr/bin/env bash

#
# update cloud storage metadata to have required http headers.
#

gs_path=$1 # gs storage bucket path, like gs://dl-apps/lorg/master
max_age=$2 # cache control max age in seconds e.g. 3600 is 1 hour.

if [[ -z $gs_path ]]
then
    echo "error: missing gs storage bucket path, like gs://dl-apps/lorg/master"
    exit 1
fi

if [[ -z $max_age ]]
then
    max_age=3600
fi

# content encoding header
for ext in css js map html txt svg xml json
do
    gsutil -m setmeta \
     -h "Content-Encoding: gzip" ${gs_path}/**/*.${ext}
done

# cache control header
gsutil -m setmeta \
    -h "Cache-Control:public, max-age=${max_age}" ${gs_path}/**/*
