#
# Read the config files used for kvm-xfstests, gce-xfstests, etc.
#
# Shell scripts should use this file as follows:
#
# XFSTESTS_FLAVOR=kvm # (or gce, etc.)
# t=$(echo ${XFSTESTS_FLAVOR}_xfstests_dir | tr "[:lower:]" "[:upper:]")
# eval DIR="\$$t"
# if test -z "$DIR"
# then
#     DIR="$(dirname "$(readlink -f "$0")")"
# fi
# if test ! -f "$DIR/util/get-config"
# then
#     echo "$(basename "$0"): couldn't find $DIR/util/get-config"
#     exit 1
# fi
#
# . "$DIR/util/get-config"

# Source the default configs
. $DIR/config.common
. $DIR/config.${XFSTESTS_FLAVOR}

export KBUILD_DIR="$(dirname $DIR)/kernel-build"

# Source custom configs in ~/.config/ if present
[ -f "$HOME/.config/xfstests-common" ] && . "$HOME/.config/xfstests-common"

# If XFSTESTS_CONFIG is set, use that
# otherwise, look for config in default location ~/.config/
if [ -n "$XFSTESTS_CONFIG" -a -f "$XFSTESTS_CONFIG" ]; then
    . "$XFSTESTS_CONFIG"
else
    if [ -f "$HOME/.config/${XFSTESTS_FLAVOR}-xfstests" ]; then
	. "$HOME/.config/${XFSTESTS_FLAVOR}-xfstests"
    fi
    if [ -f "$HOME/.local/etc/${XFSTESTS_FLAVOR}-xfstests" ]; then
	. "$HOME/.local/etc/${XFSTESTS_FLAVOR}-xfstests"
    fi
fi

# For gce-xfstests, source the config for the active account if present
if test "$XFSTESTS_FLAVOR" = "gce" -a -z "$GCE_ACCOUNT" -a \
   -n "$(ls $DIR/config-* 2> /dev/null)"
then
   # We aren't just using GCE_ACCOUNT=$(...) to work around a performance
   # bug in gcloud.  This will hopefully be fixed soon.  :-)
   tmpfile=$(mktemp)
   gcloud config list --format='value(core.account)' core/account \
	2>/dev/null > "$tmpfile"
	GCE_ACCOUNT="$(cat $tmpfile)"
	rm "$tmpfile"
	unset tmpfile
fi
if test -n "$GCE_ACCOUNT" -a -f "$DIR/config-$GCE_ACCOUNT" ; then
    . "$DIR/config-$GCE_ACCOUNT"
fi

if test "$XFSTESTS_FLAVOR" = "gce" ; then
    function get_cache_dir() {
	for i in $HOME/.cache /tmp/$USER-cache ; do
	    GCE_CACHE_DIR=$i/gce-xfstests
	    mkdir -p $GCE_CACHE_DIR 2> /dev/null
	    if test -d $GCE_CACHE_DIR -a -w $GCE_CACHE_DIR ; then
		return
	    fi
	done
	GCE_CACHE_DIR=/tmp
    }

    function run_gcloud ()
    {
	if test -n "$GCE_ACCOUNT" ; then
	    $NO_ACTION gcloud --account "$GCE_ACCOUNT" --project "$GCE_PROJECT" "$@"
	else
	    $NO_ACTION gcloud --project "$GCE_PROJECT" "$@"
	fi
    }

    function run_gcloud_always ()
    {
	if test -n "$NO_ACTION" ; then
	    if test -n "$GCE_ACCOUNT" ; then
		echo gcloud --account "$GCE_ACCOUNT" --project "$GCE_PROJECT" \
		     "$@" > /dev/tty
	    else
		echo gcloud --project "$GCE_PROJECT" "$@" > /dev/tty
	    fi
	fi
	if test -n "$GCE_ACCOUNT" ; then
	    gcloud --account "$GCE_ACCOUNT" --project "$GCE_PROJECT" "$@"
	else
	    gcloud --project "$GCE_PROJECT" "$@"
	fi
    }

    function run_gcloud_prj ()
    {
	if test -n "$GCE_ACCOUNT" ; then
	    $NO_ACTION gcloud --account "$GCE_ACCOUNT" "$@"
	else
	    $NO_ACTION gcloud "$@"
	fi
    }

    GCE_DASHBOARD_SERVICE_IAM=dashboard
    SERVICE_ACCOUNT_VM="test-vm@$GCE_PROJECT.iam.gserviceaccount.com"
    SERVICE_ACCOUNT_LTM="ltm-kcs@$GCE_PROJECT.iam.gserviceaccount.com"
    SERVICE_ACCOUNT_IMG="img-create@$GCE_PROJECT.iam.gserviceaccount.com"
    SERVICE_ACCOUNT_DASH="dashboard@$GCE_PROJECT.iam.gserviceaccount.com"

    # setup a cached set of service account options
    get_cache_dir
    GCE_MACHTYPE_FILE=$GCE_CACHE_DIR/machtype_file
    SA="$GCE_CACHE_DIR/service-accounts-opts.$GCE_PROJECT"
    if test -r "$SA" ; then
	. "$SA"
    else
	if run_gcloud_always iam service-accounts describe \
			     "$SERVICE_ACCOUNT_VM" >& /dev/null ; then
	    SERVICE_ACCOUNT_OPT_VM="--service-account $SERVICE_ACCOUNT_VM"
	    SERVICE_ACCOUNT_OPT_LTM="--service-account $SERVICE_ACCOUNT_LTM"
	    SERVICE_ACCOUNT_OPT_IMG="--service-account $SERVICE_ACCOUNT_IMG"
	else
	    SERVICE_ACCOUNT_OPT_VM=
	    SERVICE_ACCOUNT_OPT_LTM=
	    SERVICE_ACCOUNT_OPT_IMG=
	fi
	declare -p SERVICE_ACCOUNT_OPT_VM > "$SA.$$"
	declare -p SERVICE_ACCOUNT_OPT_LTM >> "$SA.$$"
	declare -p SERVICE_ACCOUNT_OPT_IMG >> "$SA.$$"
	if test -r "$SA" ; then
	    rm -f "$SA.$$"
	else
	    mv "$SA.$$" "$SA"
	fi
    fi
    unset SA

    # check whether this machine runs in the same GCE project
    if ping -c 1 -q metadata.google.internal >& /dev/null ; then
        # We must be running on an GCE VM since a DNS resolution of
        # metadata.google.internal succeeded.
	if test -f /usr/local/lib/gce-funcs ; then
	    . /usr/local/lib/gce-funcs
	    if test "$(get_metadata_value project-id)" = "$GCE_PROJECT" ; then
		RUN_INTERNAL="--internal-ip"
            fi
	else
            # fall back to check with internal ip
            INT_IP=$(hostname -I)
            if gcloud compute instances list \
		      --project "$GCE_PROJECT" \
		      --filter="networkInterfaces[0].networkIP:$INT_IP" &> /dev/null; then
		RUN_INTERNAL="--internal-ip"
            fi
	fi
    fi

    if test -z "$GCE_SERIAL_PORT_ACCESS" ; then
	GCE_SERIAL_PORT_ACCESS=TRUE
    fi
fi
