#! /bin/sh
# Various 'constants'
TARGETDIR=/opt
GOOGLECLOUDVERSION=128.0.0
GOOGLECLOUDINSTALLTAR="/csciloc/ubuntu/Distributions/google-cloud-sdk-${GOOGLECLOUDVERSION}-linux-x86_64.tar.gz"
GOOGLECLOUDDIR="${TARGETDIR}/google-cloud-sdk"
GOOGLECLOUDEXEC="${GOOGLECLOUDDIR}/bin/gcloud"

# From http://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
if ! [ $(id -u) = 0 ]; then
   echo "This script must be run as root"
   exit 1
fi
# end of askubuntu stuff

# Be sure system directories are there
if [ ! -d "$TARGETDIR" ]; then
    echo "creating $TARGETDIR"
    mkdir -p "$TARGETDIR"
fi
if [ ! -d "$APPLICATIONDIR" ]; then
    echo "creating $APPLICATIONDIR"
    sudo mkdir -p "$APPLICATIONDIR"
fi

# extract and set permissions
tar --extract --gzip --verbose --directory "$TARGETDIR" \
  --file "${GOOGLECLOUDINSTALLTAR}"

ln -s "$GOOGLECLOUDEXEC" /usr/local/bin
