#!/usr/sbin/install-menu
#
# Generates AfterStep menus for all registered applications.
#

compat="menu-1"

!include menu.h

compat="menu-2"


# If file $idir/$file exists, then output "MiniPixmap" line...
function get_icon($idir, $ifile)=
        ifnempty(
          $ifile,
          ifelse(
             $idir,
             ifempty(
               parent($ifile),
               iffile(
                 $idir "/" $ifile,
                 "MiniPixmap \"" $idir "/" $ifile "\"\n"
                 )
             ),
             iffile(
               $ifile,
               "MiniPixmap \""  $ifile "\"\n"
             )
          )
        );

function my_ifelse($x, $y)=
        ifelse($x, $x, $y);

#
# Main functions
#
function mod($var,$com)=$var " \""
          title() ifnempty($hotkey,"&" toupper($hotkey))
          "\" exec " $com " &\n"
          my_ifelse(
            get_icon("", icon()),
            my_ifelse(
              get_icon("/usr/X11R6/include/X11/pixmaps", icon()),
                my_ifelse(
                  get_icon("/usr/X11R6/include/X11/bitmaps", icon()),
                  "MiniPixmap \"mini-app.xpm\"\n"
                )
             )
          );



function restart($var,$com)=" " $var " \""
          title() ifnempty($hotkey,"&" toupper($hotkey))
          "\" " $com "\n";

function ex($com)=mod("Exec",$com);

# Make menus into directories and entries into files; deal with entries that
# contain slashes.
# For example, if title="Aviplay AVI/ASF/WMF player" and
# section="/Debian/Apps/Viewers/Aviplay AVI/ASF/WMF player",
# the fuction returns:
# "/Debian/Apps/Viewers/Aviplay AVI/ASF/../../Aviplay-20AVI-2fASF-2fWMF-20player"
function genmenu_f()=
        parent($section) "/"
        ifnempty(
          parent($title),
          forall(
            replacewith(
              replacewith(
                parent($title),
                ":",
                "-"
              ),
              "/",
              ":"
            ),
            "x",
            ifnempty($x, "../")
          )
        )
        replacewith(cppesc($title), "$", "-");

genmenu=ifelse($command, genmenu_f(),  "/");


rootprefix="/var/lib/afterstep/menu";
userprefix=".afterstep";
treewalk=(M);
rootsection="/Debian";

preoutput="";

# Clean-up files we may have generated in an earlier run first.
prerun="find " prefix() " -type f -exec grep -q '^Exec' {} \\; -exec rm {} \\; ; find " prefix() " -type d -depth -print0 | xargs -0r rmdir --ignore-fail-on-non-empty";

# And remove any empty directories afterwards
postrun="find " prefix() " -type d -depth -print0 | xargs -0r rmdir --ignore-fail-on-non-empty";

supported;
  asmodule=     mod("Module", $command);
  fvwmmodule=   mod("Exec",   $command);
  x11fvwm=      ex($command);
  wm=           restart("Restart",$command);
  x11=          ex($command);
  text=         ex(term());
endsupported;

mainmenutitle="";
submenutitle= "";

startmenu="";
endmenu="";
