#!/bin/bash

# Script which must be sourced to setup the development environment.

# This has to be the first command because BASH_SOURCE[0] gets changed.
SCRIPT=${BASH_SOURCE[0]:-$0}

[[ "${BASH_SOURCE[0]}" == "$0" ]] \
    && echo "This script should not be executed but sourced like:" \
    && echo "    $ source $0" \
    && echo \
    && exit 1

SCRIPTDIR=$(dirname $(realpath $SCRIPT))
export FLATPAK_ENVPATH=$(realpath $SCRIPTDIR/../../)
export CURRENT_GST=$FLATPAK_ENVPATH

# Use ptvenv for running commands in the sandbox.
alias ptvenv="$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -d"

echo "-> Setting up environment if needed..."
$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -q -d echo
if [ "$?" = "0" ];
then
    # Set up environment variables and aliases so configuring, building, etc.
    # takes place in the sandbox.
    export make="ptvenv make"
    export check="ptvenv make check"
    export minstall="ptvenv make install"
    export autogen="ptvenv ./autogen.sh --prefix=/app --disable-gtk-doc"
    export configure="ptvenv ./configure --prefix=/app"

    alias make=$make
    alias check=$check
    alias minstall=$minstall
    alias autogen=$autogen
    alias configure=$configure

    # Prefer to run the binaries in the sandbox. For example "python3".
    for i in `$FLATPAK_ENVPATH/pitivi/build/flatpak/pitivi-flatpak -q -d ls /app/bin/`;
    do
        alias $i="ptvenv $i"
    done

    alias pitivi="ptvenv $FLATPAK_ENVPATH/pitivi/bin/pitivi"

    export PS1="(ptv-flatpak) $PS1"
    export PATH="$FLATPAK_ENVPATH/bin/:$PATH"

    echo "===================================================================="
    echo "                   BATTLECRUISER OPERATIONAL                        "
    echo "                          >(°)__/                                   "
    echo "                           (_~_/                                    "
    echo "                         ~~~~~~~~~~~~                               "
    echo "===================================================================="
fi
