#!/bin/sh -e
# HiveMind - Cross-platform collaborative mind map editor
# HiveMind deployment script for ScratchBox
# Copyright (C) 2011 Ilya Paramonov

DSTDIR=/scratchbox/users/$LOGNAME/home/$LOGNAME/hivemind

# Build
cd `dirname $0`
make version
make

# Check if VDSO is enabled
if [ `cat /proc/sys/vm/vdso_enabled` = 1 ] ; then
    ASROOT='echo 0 > /proc/sys/vm/vdso_enabled'
fi

# Check if destination directory exists and is mounted inside Scratchbox
if [ `mount | grep -c "$DSTDIR "` = 0 ] ; then
    if [ ! -d $DSTDIR ] ; then mkdir $DSTDIR ; fi
    if [ -n "$ASROOT" ] ; then ASROOT=$ASROOT" ; " ; fi
    ASROOT=$ASROOT"mount -o bind . $DSTDIR"
fi

# Execute neccessary commands as root
if [ -n "$ASROOT" ] ; then
    gksu -u root "$ASROOT"
fi

# Start Xephyr is it is not started yet
if [ -z `pgrep Xephyr` ] ; then
    Xephyr :2 -host-cursor -screen 800x480x16 -dpi 96 -ac &
    /scratchbox/login af-sb-init.sh stop
    /scratchbox/login af-sb-init.sh start
fi

# Clear the project from old binaries
make clean-binaries

# Finally, start HiveMind
/scratchbox/login -d hivemind run-standalone.sh python2.5 hivemind.py $@
