#!/bin/bash
# This script was written by Daniele Favara <danjele@gmail.com>
# Stripped for keytouch by Rodrigo Gallardo <rodrigo@nul-unu.com>

set -e

while [ ! -z "$1" ];do
    case "$1" in
      --help|-h)
	    echo "Usage: keytouchd-launch [command]"
            echo "Launch the keytouch daemons in background, before running COMMAND."
            echo "Please see 'man keytouchd-launch' for details."
        exit 
        ;;
      *)
        SESSION=$@ ; shift $#
        ;;
    esac
done


if [ "x$SESSION" = "x" ]; then
    # No session? We die
    echo "You must provide a command to run."
    exit 1
else
    keytouchd & keytouchdpid=$!

    # Run the session, kill the daemons afterwards
    $SESSION 

    while ps $keytouchdpid 
      do
      kill $keytouchdpid || sleep 5
    done

    exit $?
fi

