#!/bin/sh -eu

# Store away original args before they are overwritten
orig_args=$@

: ${bindir:="$( cd -P "$( dirname "$0" )" && pwd )"}
: ${prefix:="$( cd -P "$( dirname "$0" )/.." && pwd )"}

debugging=0
legacyhome=0

while [ $# -gt 0 ]; do
  case "$1" in
  --debug)
    debugging=1
    ;;
  --help)
    echo "--debug       - start in debugger (requires that 'gdb' is installed)"
    echo "--legacyhome  - do not migrate ~/.ember to XDG Base Directories"
    exec $bindir/ember.bin --help >&2
    exit 1
    ;;
  --legacyhome)
    legacyhome=1
    ;;
  esac
  shift
done

# Migrate from legacy home directories to those specified by the XDG Base Directory Specification
# Only migrate if legacy directory exists and user does not request it kept
if [ $legacyhome = 0 ] && [ -d "$HOME/.ember" ] ; then
  echo "Migrating '~/.ember' to XDG Base Directories. For more information, please see:"
  echo "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"
  echo "The original '~/.ember' directory will be archived as '~/.emberlegacy'."
  echo "If you are satisfied with the automatic migration, the '~/.emberlegacy'"
  echo "directory can be safely removed."
  cp -a "$HOME/.ember/" "$HOME/.emberlegacy/"
  echo "This is copy of the ~/.ember directory made by the Ember application. Configuration files now instead resides in ~/.config/ember (or XDG_CONFIG_HOME) and data in ~/.local/share/ember (or XDG_DATA_HOME). This directory can be removed if everything in Ember is working as it should." > $HOME/.emberlegacy/README
  if [ "${XDG_DATA_HOME:-}" ] ; then
    data_move="$XDG_DATA_HOME"
  else
    data_move="$HOME/.local/share"
  fi
  if [ "${XDG_CONFIG_HOME:-}" ] ; then
    config_move="$XDG_CONFIG_HOME"
  else
    config_move="$HOME/.config"
  fi
  if [ "${XDG_CACHE_HOME:-}" ] ; then
    cache_move="$XDG_CACHE_HOME"
  else
    cache_move="$HOME/.cache"
  fi
  if [ -d "$config_move/ember/" ] ; then
    echo "Found existing XDG Config directory, not replacing with files from ~/.ember"
    rm "$HOME/.ember"/*.conf || true
    rm "$HOME/.ember"/*.cfg || true
    rm -rf "$HOME/.ember/scripts/" || true
  else
    mkdir -p "$config_move/ember/"
    mv "$HOME/.ember"/*.conf "$config_move/ember/" || true
    mv "$HOME/.ember"/*.cfg "$config_move/ember/" || true
    cp -a "$HOME/.ember/scripts/" "$config_move/ember/scripts/" || true
    rm -rf "$HOME/.ember/scripts/" || true
  fi
  if [ -d "$data_move/ember/" ] ; then
    echo "Found existing XDG Data directory, not replacing with files from ~/.ember"
  else
    cp -a "$HOME/.ember/" "$data_move/ember/" || true
  fi
  rm -rf "$HOME/.ember/" || true
fi

# Rely on built-in paths for installed binaries.
# Note that because of an issue with how CEGUI 0.8.2 detects dynamic libraries we need to include the CEGUI_MODULEDIR path
# CEGUI uses lib64 on some systems, so we'll add that too
if [ "$prefix" != "@prefix@" ]; then
  prefix_arg="--prefix $prefix"
  export LD_LIBRARY_PATH=$prefix/lib64:$prefix/lib:$prefix@CEGUI_MODULE_SUBDIR@${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
else
  prefix_arg=
  export LD_LIBRARY_PATH=@prefix@/lib64:@prefix@/lib:@CEGUI_MODULEDIR@${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
fi

cd $bindir

if [ $debugging = 1 ] ; then
  # Execute real ember binary
  echo "Starting Ember in debugger...."
  gdb --batch -ex "run" -ex "backtrace" -ex "quit" \
  --args $bindir/ember.bin $prefix_arg --config general:logginglevel verbose $orig_args
else
  # Execute real ember binary
  echo "Starting Ember...."
  exec $bindir/ember.bin $prefix_arg $orig_args
fi
