#!/usr/bin/env bash
if [ "${BASH_SOURCE[0]}" != "$0" ]; then
    echo "${BASH_SOURCE[0]} must be executed."
    return
fi

# Parse replay arguments
CD_WORK="{{ work_dir }}"
PRINT=""
CMDPREFIX=""
SKIPEXPORT=0
while [[ $# -gt 0 ]]; do
    case $1 in
        --which)
            PRINT="which"
            shift
            ;;
        --version)
            PRINT="version"
            shift
            ;;
        --directory)
            PRINT="directory"
            shift
            ;;
        --command)
            PRINT="command"
            shift
            ;;
        --skipcd)
            CD_WORK="."
            shift
            ;;
        --skipexports)
            SKIPEXPORT=1
            shift
            ;;
        --cmdprefix)
            CMDPREFIX="$2"
            shift
            shift
            ;;
        -h|--help)
            echo "Usage: $0"
            echo "  Options:"
            echo "    --which           print which executable would be used"
            echo "    --version         print the version of the executable, if supported"
            echo "    --directory       print the execution directory"
            echo "    --command         print the execution command"
            echo "    --skipcd          do not change directory into replay directory"
            echo "    --skipexports     do not export environmental variables"
            echo "    --cmdprefix <cmd> prefix to add to the replay command, such as gdb"
            echo "    -h,--help         print this help"
            exit 0
            ;;
        *)
            echo "Unknown option $1"
            exit 1
            ;;
    esac
done

if [ $SKIPEXPORT == 0 ]; then
    # Environmental variables{% for key, value in exports.items() %}
    export {{ key }}="{{ value }}"{% endfor %}
fi

# Switch to the working directory
cd "$CD_WORK"

case $PRINT in
    "which")
        which {{ executable }}
        exit 0
        ;;
    "version")
        {% if version_flag is defined %}{{ executable }} {{ version_flag }}{% else %}echo "--version is not supported"{% endif %}
        exit 0
        ;;
    "directory")
        echo "Working directory: $PWD"
        exit 0
        ;;
    "command")
        echo "{% for cmd in cmds %}{% if not loop.first %} {% endif %}{{ cmd }}{% endfor %}"
        exit 0
        ;;
esac

# Command execution
$CMDPREFIX \{% for cmd in cmds %}
{% if not loop.first %}  {% endif %}{{ cmd }}{% if not loop.last %} \{% endif %}{% endfor %}
