#!/bin/sh
# -*- Mode: sh; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# For copyright information, see AUTHORS.
#
# Déjà Dup is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Déjà Dup is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.

# This script dumps you in a special shell, ideal for testing deja-dup.
# It will point at the locally built executables and plugins.  It will
# point at a custom dconf location and $HOME.

TOPDIR=$(dirname "$( cd "$( dirname "${0}" )" && pwd )")

# Create fake home environment
ROOTDIR=$(mktemp -d --tmpdir dd.XXXXXXXXXX)

mkdir -p "${ROOTDIR}/share/glib-2.0/schemas"
cp "${TOPDIR}/data/org.gnome.DejaDup.gschema.xml" "${ROOTDIR}/share/glib-2.0/schemas"
glib-compile-schemas "${ROOTDIR}/share/glib-2.0/schemas"
export XDG_DATA_HOME="${ROOTDIR}/share"
export XDG_DATA_DIRS="${XDG_DATA_HOME}:${XDG_DATA_DIRS}"

mkdir -p "${XDG_DATA_HOME}/deja-dup/ui"
cp "${TOPDIR}/data/ui/"* "${XDG_DATA_HOME}/deja-dup/ui"

mkdir -p "${ROOTDIR}/cache"
export XDG_CACHE_HOME="${ROOTDIR}/cache"

mkdir -p "${ROOTDIR}/config"
export XDG_CONFIG_HOME="${ROOTDIR}/config"

mkdir -p "${ROOTDIR}/gnupg"
export GNUPGHOME="${ROOTDIR}/gnupg"

mkdir -p "${ROOTDIR}/source"  # for convenience
mkdir -p "${ROOTDIR}/backup"  # for convenience
mkdir -p "${ROOTDIR}/restore" # for convenience


# Point at our built objects
export DEJA_DUP_TOOLS_PATH="${TOPDIR}/tools/duplicity"
export PATH="${TOPDIR}/monitor:${TOPDIR}/deja-dup:${TOPDIR}/preferences:${PATH}"


# Explain what's going on to user
echo "You are now in a special shell that points to all the built objects in"
echo "your local tree.  You are also using a custom gsettings location, so any"
echo "changes to your configuration won't mess up your system."
echo "Your data playground is ${ROOTDIR}"
echo "Run deja-dup, deja-dup-preferences, or deja-dup-monitor to test."
echo "Run exit to leave."
echo "Run exit 1 to leave and keep data playground intact."


# Now dump user out
cd "${ROOTDIR}"
export PS1='\[\e[1;32m\]deja-dup\$\[\e[0m\] '
if dbus-launch bash --norc; then
  rm -rf "${ROOTDIR}" # Cleanup on clean exit
fi
