#!/bin/sh

. /etc/init.d/tc-functions

ONDEMAND=/home/"$USER"/.fluxbox/ondemand
TCEDIR=/etc/sysconfig/tcedir

process(){
  if [ ! -s "$ONDEMAND" ]; then
    echo "[submenu](OnDemand)" > "$ONDEMAND"
    echo "[end] OnDemand" >> "$ONDEMAND"
    sync
  fi  
  TMP="/tmp/fluxbox.$$" && TMP2="/tmp/fluxbox2.$$"
  echo "[exec](${1}){$TCEDIR/ondemand/${1}}" >> "$TMP"
  merge "$TMP" "$ONDEMAND" "[end] OnDemand" > "$TMP2"
  mv "$TMP2" "$ONDEMAND"
  rm "$TMP"
}

if [ -n "$1" ]; then
  process "$1"
else
  > /home/"$USER"/.fluxbox/ondemand	  
  [ -d "$TCEDIR"/ondemand ] && for O in `ls "$TCEDIR"/ondemand| grep -v ".img$"`; do fluxbox_ondemand ${O}; done
fi
