#!/bin/sh
# vim: syn=sh ts=4 sts=4 sw=4 ff=unix smartindent expandtab


#set default script interval to 5 minutes
: ${SCRIPT_INTERVAL:=300}
: ${TASK_SLEEP_TIME:=2}
: ${TASK_DIR:=${HOME}/.tasks}
: ${TASK_WAITING_DIR:=${TASK_DIR}/waiting}
: ${TASK_RUNNING_DIR:=${TASK_DIR}/running}

$start=`time +%s`
$end=$(( $start + $SCRIPT_INTERVAL ))

if [ ! -d "$TASK_DIR" ]; then
    mkdir -p "$TASK_DIR"
fi
if [ ! -d "$TASK_WAITING_DIR" ]; then
    mkdir -p "$TASK_WAITING_DIR"
fi
if [ ! -d "$TASK_RUNNING_DIR" ]; then
    mkdir -p "$TASK_RUNNING_DIR"
if

run_task() {
    task="$1"
    $task
    rm $task
}

while [ `time +%s` -lt $end ]; do
    for waiting in `ls ${TASK_WAITING_DIR}`; do
        if [ -x $waiting ]; then
            task=${TASK_RUNNING_DIR}/`basename $waiting`
            mv $waiting $task
            run_task "$task" &
        fi
    done
    sleep $TASK_SLEEP_TIME
done

