#!/bin/dash
# generic wrapper to run as spot (when currently running as root)
# (C) James Budiono 2012
# License: GPL version 3 or later
#
SPOT_HOME=$(awk -F: '$1=="spot" {print $6}' /etc/passwd)
CWD=$(pwd)
APP="$1"; shift
if [ $(id -u) -eq 0 ]; then
	[ -z "$XAUTHORITY" ] && XAUTHORITY=/root/.Xauthority
	[ -e "$XAUTHORITY" ] && cp $XAUTHORITY $SPOT_HOME/.Xauthority &&
	chown spot:spot $SPOT_HOME/.Xauthority && 
	export XAUTHORITY=$SPOT_HOME/.Xauthority  
	
	export XDG_CONFIG_HOME=$SPOT_HOME/.config
	export XDG_CACHE_HOME=$SPOT_HOME/.cache
	export XDG_DATA_HOME=$SPOT_HOME/.local/share
        mkdir -p $XDG_DATA_HOME; chown spot:spot $XDG_DATA_HOME
    export FATDOG_STATE_DIR=$SPOT_HOME/.fatdog
	ARGS=""
	while [ "$1" ]; do ARGS="$ARGS \"$1\""; shift; done
	exec su spot -s /bin/dash -c "cd \"$CWD\"; \"$APP\" $ARGS"
else
	exec "$APP" "$@"
fi
