#!/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 "    $ . $0" \
    && echo \
    && exit 1

SCRIPT_DIR=$(dirname $(realpath $SCRIPT))
export PITIVI_REPO_DIR=$(realpath $SCRIPT_DIR/..)
export FLATPAK_ENVPATH=$(realpath $PITIVI_REPO_DIR/..)
export CURRENT_GST=$FLATPAK_ENVPATH
export PATH="$FLATPAK_ENVPATH/bin/:$PATH"

# Use ptvenv for entering or running commands in the sandbox.
alias ptvenv="$PITIVI_REPO_DIR/build/flatpak/pitivi-flatpak"

echo "Setting up the build dir of the sandbox in $FLATPAK_ENVPATH/pitivi-prefix"
ptvenv --init

if [ "$?" = "0" ]
then
    echo "Sandbox ready."

    echo "Setting up aliases so configuring, building takes place in the sandbox..."

    # Meson sets up the build directory where ninja works.
    # Consider using `setup` instead of `meson`, see below.
    alias meson="ptvenv meson"

    # Normally, Pitivi's mesonbuild/ directory is created when
    # initializing or updating (recreating) the Flatpak sandbox.
    # The initialization happens above, look for `ptvenv --init`.
    # The updating happens when you run `ptvenv --update`.
    # You can also create it manually if you deleted it by mistake.
    # This should also be used when building dependent projects
    # such as GES, etc.
    alias setup="mkdir mesonbuild; ptvenv meson mesonbuild/ --prefix=/app --libdir=lib -Ddisable_gtkdoc=true -Ddisable_doc=true"

    # Ninja builds the buildable parts of the project.
    alias ninja="ptvenv ninja"

    # We could get rid of these, but some devs like them.
    alias build="ptvenv ninja -C mesonbuild/"
    alias binstall="ptvenv ninja -C mesonbuild/ install"
    alias ptvtests="ptvenv gst-validate-launcher $PITIVI_REPO_DIR/tests/ptv_testsuite.py --dump-on-failure"

    # Prefer to run some binaries in the sandbox.
    for i in `$PITIVI_REPO_DIR/build/flatpak/pitivi-flatpak -q ls /app/bin/`
    do
        alias $i="ptvenv $i"
    done

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

    echo "Installing our pre-commit git hook, for doing clean commits..."
    rm -f .git/hooks/pre-commit
    ln -s ../../pre-commit.hook .git/hooks/pre-commit

    # Update the prompt to indicate this is the Pitivi dev env,
    # unless PS1 has already been modified.
    PS1_PREFIX="(ptv-flatpak)"
    if [[ $PS1 != *"$PS1_PREFIX"* ]]
    then
        export PS1="$PS1_PREFIX $PS1"
    fi

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