#!/bin/sh
#  
# doubleclick_delayed.sh: Executes a program if called within a specified 
# delay (A.K.A doubleclick). 
# Requires GNU-version of "date".
#
# Based on double_click.sh from http://hocwp.free.fr/xbindkeys/xbindkeys.html
# Modified by Isak Savo <iso01001@student.mdh.se> 
BUTTON=$1
DELAY=$2
PROG_2=$3
LOCK=/tmp/double_click_delayed.$BUTTON.lock

if [ -z "$BUTTON" -o -z "$DELAY" -o -z "$PROG_2" ]; then
    echo "Usage : Double_Click <Button> <Delay (sec)> <Program after doubleclick>"
    exit
fi

EPOCH=`date +'%s'`;
if [ -e $LOCK ]; then
    LASTTIME=`cat $LOCK`;
    if [ $EPOCH -le $(($LASTTIME + $DELAY)) ]; then
	exec $PROG_2 &
	rm -f $LOCK
    else
	echo "$EPOCH" > $LOCK;
    fi;
else
    echo "$EPOCH" > $LOCK
fi