#!/bin/sh

I_AM=`whoami`

if [ "$I_AM" = "root" ]; then

  for request_username in /var/lib/menu-generator/* ; do
    for request_wm in $request_username/* ; do

      cat $request_wm | {

	read username
	read homedir
	read wm

	if [ "$wm" = "icewm" ];then
	  target="$homedir/.icewm/menu"
	  su $username -c "mkdir -p \"$homedir/.icewm\""
	elif [ "$wm" = "fluxbox" ];then
	  target="$homedir/.fluxbox/menu"
	  su $username -c "mkdir -p \"$homedir/.fluxbox\""
	elif [ "$wm" = "blackbox" ];then
	  target="$homedir/.blackbox/menu"
	  su $username -c "mkdir -p \"$homedir/.blackbox\""
	elif [ "$wm" = "openbox" ];then
	  target="$homedir/.config/openbox/menu.xml"
	  su $username -c "mkdir -p \"$homedir/.config/openbox\""
	else
	  continue
	fi

	su $username -c "menu-generator $wm > \"$target\""

      }

    done
  done

else

  for request_wm in /var/lib/menu-generator/$I_AM/* ; do

    cat $request_wm | {

      read username
      read homedir
      read wm

      if [ "$wm" = "icewm" ];then
	target="$homedir/.icewm/menu"
	mkdir -p "$homedir/.icewm"
      elif [ "$wm" = "fluxbox" ];then
	target="$homedir/.fluxbox/menu"
	mkdir -p "$homedir/.fluxbox"
      elif [ "$wm" = "blackbox" ];then
	target="$homedir/.blackbox/menu"
	mkdir -p "$homedir/.blackbox"
      elif [ "$wm" = "openbox" ];then
	target="$homedir/.config/openbox/menu.xml"
	mkdir -p "$homedir/.config/openbox"
      else
	continue
      fi

      menu-generator $wm > "$target"

    }

  done

fi
