#!/bin/bash

# Script to install the Google Cloud Storage Commands
# Ravi - 2017-06-11
set -ev

function install_gc_sdk () {
   # Ensure that Google Cloud SDK is available
    GC_SDK="https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz"

    curl -s -XGET ${GC_SDK} -O   # -O = create a file with the same name as remote; -s = silent
    tar xzf $(basename ${GC_SDK}) 
    GC_DIR=$(basename ${GC_SDK} | awk -F. '{print $1}') 
    if [[ -d ${GC_DIR} ]]; then
   # the SDK directory is available; so install
	${GC_DIR}/install.sh -q && echo "Google SDK installed"
    fi
   #source /home/travis/build/vpsinc/ice-frontend-react-mobx/google-cloud-sdk/path.bash.inc
   #echo $PATH
   #ls -l /home/travis/build/vpsinc/ice-frontend-react-mobx/google-cloud-sdk/bin
   # Test to see if Google SDK is successfully installed; 
   # Note: ${TC// } uses pattern submission to make sure that blank string is reduced to null string
   # if [[ (TC=$(which /home/travis/build/vpsinc/ice-frontend-react-mobx/google-cloud-sdk/bin/gcloud) && -z "${TC// }") || (TC=$(which /home/travis/build/vpsinc/ice-frontend-react-mobx/google-cloud-sdk/bin/gsutil) && -z "${TC// }") ]]; then
   # Switch check to see if file exists
   if [[ (TC=$(which gcloud) && -z "${TC// }") || (TC=$(which gsutil) && -z "${TC// }") ]]; then
       echo "Cannot find the gcloud and gsutil commands; possibly the SDK did not get installed"
       exit 1
   else
    # Updated the installed components
       gcloud components update --quiet || echo "Update of Google SDK components FAILED !!"
   fi
}

if [[ install_gc_sdk ]]; then
    # Looks like the check is to make sure the function is there, call the function !!!
    echo "Google Cloud SDK/Commands installed!!"
else
    echo "Google Cloud SDK install FAILED :-( "
fi

