#!/usr/bin/env bash

function _resolveSelfDir {
    local SOURCE="$1"
    local DIR=""
    # Convert to absolute path first if relative
    if [[ $SOURCE != /* ]]; then
        SOURCE="$(cd "$(dirname "$SOURCE")" && pwd)/$(basename "$SOURCE")"
    fi
    while [[ -h "$SOURCE" ]]; do
        DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
        SOURCE="$(readlink "$SOURCE")"
        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
    done
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    echo "${DIR}"
}

# Get script path - works in both bash and zsh
if [[ -n "${BASH_SOURCE[0]}" ]]; then
    _script_path="${BASH_SOURCE[0]}"
elif [[ -n "${(%):-%x}" ]]; then
    # zsh specific
    _script_path="${(%):-%x}"
else
    echo "ERROR: Cannot determine script location" >&2
    return 1
fi

_dir="$(_resolveSelfDir "$_script_path")"

# Execute activate.ts from the same directory as this script
eval "$($_dir/activate.ts)"
