#
# Script by: Mike Fuller (mfuller@atlassian.com)
#            Shane Anderson (sanderson@atlassian.com)
#
# This file should be sourced into your .bash_profile or similar.

CLOUDTOKEN_DIR=~/.config/cloudtoken

alias cloudtoken=". cloudtoken"

unsetcloudtoken () {
    unset AWS_ACCESS_KEY_ID
    unset AWS_SECRET_ACCESS_KEY
    unset AWS_SECURITY_TOKEN
    unset AWS_SESSION_TOKEN
    unset AWS_SECRET_KEY
    unset AWS_ACCESS_KEY
    unset AWS_DELEGATION_TOKEN
    unset AWS_ACCOUNT_ID
    unset AWS_ROLE_NAME
    unset AWS_EXPIRATION
    unset AWS_EXPIRATION_TIMESTAMP
    unset AWS_LASTUPDATED

    cloudtoken --unset
}

# Clean up old token files when we open a new shell.
for FILE in tokens.json tokens.shell;
do
    if [[ -f ${CLOUDTOKEN_DIR}/${FILE} ]]; then
        find ${CLOUDTOKEN_DIR}/ -name ${FILE} -mmin +60 -delete
    fi
done

# Source tokens into a new shell when it opens.
if [[ -f ${CLOUDTOKEN_DIR}/tokens.shell ]]; then
    source ${CLOUDTOKEN_DIR}/tokens.shell
fi
