#!/usr/sbin/install-menu

compat="menu-1";
!include menu.h
compat="menu-2";
onlyrunasuser=true;
outputlanguage="C";
outputencoding="UTF-8";
function term_prog()="Eterm";
treewalk=(M);
userprefix=".e/e/applications/favorite/";
mainmenutitle="Mandriva Linux Menu";
rootsection="Mandriva Linux Menu";
#if you think this added menu level is too much
#or don't use Mandriva... change this to something else:
#rootsection="";
#rootsection="My Distrib";

function menu_sh() = prefix() "mandriva_menu_sh";

sort=ifelse($command, "1", "0")":"$title;
function newline()="\n";
function quote($text)= "\"" $text "\"";
function tick($text)= "\'" $text "\'";
function ltitle()=translate(lang(),title());
function icondir()="/usr/share/icons/";
function menufile()="/usr/lib/menu/"$package;
sort=ifelse($command, "1", "0")":"ltitle();

prerun="set -e; rm -f " menu_sh() ";"
        "cat > " prefix() "icon.png.edc <<END" newline()
        "images {" newline()
        "   image: \"icon.png\" COMP;" newline()
        "}" newline()
        "collections {" newline()
        "   group {" newline()
        "      name: \"icon\";" newline()
        "      max: 48 48;" newline()
        "      parts {" newline()
        "         part {" newline()
        "            name: \"image\";" newline()
        "            mouse_events: 0;" newline()
        "            description {" newline()
        "               state: \"default\" 0.0;" newline()
        "               aspect: 1.0 1.0;" newline()
        "               image.normal: \"icon.png\";" newline()
        "            }" newline()
        "         }" newline()
        "      }" newline()
        "   }" newline()
        "}" newline()
        "END" newline()
        "cat > " prefix() "icon.xpm.edc <<END" newline()
        "images {" newline()
        "   image: \"icon.xpm\" COMP;" newline()
        "}" newline()
        "collections {" newline()
        "   group {" newline()
        "      name: \"icon\";" newline()
        "      max: 48 48;" newline()
        "      parts {" newline()
        "         part {" newline()
        "            name: \"image\";" newline()
        "            mouse_events: 0;" newline()
        "            description {" newline()
        "               state: \"default\" 0.0;" newline()
        "               aspect: 1.0 1.0;" newline()
        "               image.normal: \"icon.xpm\";" newline()
        "            }" newline()
        "         }" newline()
        "      }" newline()
        "   }" newline()
        "}" newline()
        "END" newline()
        "mkdir -p " prefix() "../all/" newline();
#        "mkdir -p " prefix() "../all/" replace(rootsection()," ","") newline();

#if rootsection isn't empty, you may want to change
#icondir() "large/mandrake.png" to something else...
postrun="set -e; cat " menu_sh() "| sh ; "
        ifnempty(rootsection(),"rm -f icon.png ; cp " icondir() "large/mandrake.png icon.png ; "
        "edje_cc -id . -fd . icon.png.edc .directory.eap ; "
        "enlightenment_eapp .directory.eap -set-name \"" rootsection() "\" ; "
        "mv -f .directory.eap " replace(rootsection()," ","") "/ ;")
        "rm -f icon.png icon.png.edc icon.xpm icon.xpm.edc " menu_sh() newline();

#"edje_cc -id . -fd . icon.png.edc .directory.eapp ; "
#"mv -f .directory.eapp " replace(rootsection()," ","") "/ ; "

#findicon: the last one is the default icon, if not found in the searched paths
function findicon($iconname)=ifelsefile($iconname, $iconname,
 ifelsefile(icondir() "large/" $iconname, icondir() "large/" $iconname,
 ifelsefile(icondir() $iconname, icondir() $iconname,
 ifelsefile(icondir() "mini/" $iconname, icondir() "mini/" $iconname,
 ifelsefile(icondir() "crystalsvg/48x48/apps/" $iconname, icondir() "crystalsvg/48x48/apps/" $iconname ,
 ifelsefile(icondir() "crystalsvg/32x32/apps/" $iconname, icondir() "crystalsvg/32x32/apps/" $iconname ,
 ifelsefile(icondir() "crystalsvg/16x16/apps/" $iconname, icondir() "crystalsvg/16x16/apps/" $iconname ,
 ifelsefile(icondir() "hicolor/48x48/apps/" $iconname, icondir() "hicolor/48x48/apps/" $iconname ,
 ifelsefile(icondir() "hicolor/32x32/apps/" $iconname, icondir() "hicolor/32x32/apps/" $iconname ,
 ifelsefile(icondir() "hicolor/16x16/apps/" $iconname, icondir() "hicolor/16x16/apps/" $iconname ,
 ifelsefile(icondir() "locolor/32x32/apps/" $iconname, icondir() "locolor/32x32/apps/" $iconname ,
 icondir() "large/mandrake.png")))))))))));


function orderdir($sec)= prefix() replace($sec," ","") ;
function orderfile-app()= orderdir(parent($section)) "/.order";
function orderfile-sect()= orderdir(parent($section)) "/.order";
function eapfile()= replace(stripdir($command)".eap"," ","");
function eapdir()= replace(title()," ","");
#function eapfilefull()= prefix() "../all/" replace(rootsection()," ","") "/" eapfile();
function eapfilefull()= prefix() "../all/" eapfile();
function eapdirfull()= orderdir($section) "/.directory.eap";

function createeapfile()="if [ " eapfilefull() " -ot " menufile() " ] ; then" newline()
                "ICON="findicon(icon()) newline()
		"ICONTYPE=${ICON:$((-3))}" newline()
		"rm -f icon.$ICONTYPE ; ln -s " findicon(icon()) " icon.$ICONTYPE" newline()
                "edje_cc -id . -fd . " prefix() "icon.$ICONTYPE.edc " eapfilefull() newline()
                "enlightenment_eapp " eapfilefull()
                " -set-name " quote(title())
                ifnempty($longtitle," -set-comment " quote(esc($longtitle,"\"")))
                " -set-exe " quote($command) newline()
                "enlightenment_eapp " eapfilefull()
                " -lang $TARGETLANG"
                " -set-name " quote(encode_translate($lang,title(),"utf-8"))
                ifnempty($longtitle," -set-comment " quote(esc(encode_translate($lang,$longtitle,"utf-8"),"\"")))
                newline()
                "touch "eapfilefull()" -r "menufile() newline()
                "fi" newline();

#		"edje_cc -id . -fd . " prefix() "icon.png.edc " eapdirfull() "p" newline()
function createeapfile-sect()="if [ " eapdirfull() " -ot " menufile() " ] ; then" newline()
                "rm -f icon.png ; ln -s " findicon(icon()) " icon.png" newline()
                "edje_cc -id . -fd . " prefix() "icon.png.edc " eapdirfull() newline()
                "enlightenment_eapp " eapdirfull()
                " -set-name " quote(title())
                ifnempty($longtitle," -set-comment " quote($longtitle))
                 newline()
                "enlightenment_eapp " eapdirfull()
                " -lang $TARGETLANG"
                " -set-name " quote(encode_translate($lang,title(),"utf-8"))
                ifnempty($longtitle," -set-comment " quote(encode_translate($lang,$longtitle,"utf-8")))
                newline()
                "touch "eapdirfull()" -r "menufile() newline()
                "fi" newline();

function updateorderfile-app()="if ! grep -q " quote("^"eapfile()"$") " " orderfile-app() " ; then {" newline()
                "cat >> " orderfile-app()" << END " newline()
                eapfile() newline()
                "END" newline()
                "};" newline()
                "fi" newline();

#folders in .e/applications/all cause problems with entangle...:(
#ifnempty(rootsection(), replace(rootsection()," ","") "/") eapfile() newline()

function updateorderfile-sect()="if ! grep -q " quote("^"eapdir()"$") " " orderfile-sect() "; then {" newline()
                "cat >> " orderfile-sect() " << END " newline()
                eapdir() newline()
                "END" newline()
                "};" newline()
                "fi" newline();

function term($com)= term_prog() " -T " tick(ltitle()) " -e " $com;

supported;
#  wm=         entry(quote("eesh -e " tick("restart_wm " $command)) newline());

  x11=        createeapfile()
                updateorderfile-app();

#  text=       createeapfile(term($command))
#               updateorderfile-app();
endsupported;

startmenu= "TARGETLANG=`echo $LANG | cut -d . -f 1`.UTF-8" newline()
                "mkdir -p " orderdir($section) newline()
                updateorderfile-sect()
                createeapfile-sect() newline();

genmenu="mandriva_menu_sh"; 
