#!/usr/sbin/install-menu
#   -*- mode: shell-script; -*-
#
# (c) 2003 Gtz Waschk
# based on a Debian menu-method by Jan Wagemakers
# includes fixes from Maxim Heijndijk
# Thanks to Thomas Leonard for advice about "$@" and symlink /usr/share/doc
# creates SendTo links and AppDirs for menu entries, including AppInfo.xml
# and Help symlinks
#
compat="menu-1"
!include menu.h
genmenu="roxmenu"
rootprefix="/usr/lib/apps"

userprefix="/Choices"
# we use a list of common icon dirs, including the typical dirs from KDE
# as a last resort we use the absolute path from the icon entry
preoutput="#!/bin/sh \n# ROX menu \n\n \
#set -x \n \
ICONDIRS=\"/usr/share/icons/large \\\n \
           /usr/share/icons \\\n \
           /usr/share/icons/crystalsvg/48x48/apps \\\n \
           /usr/share/icons/crystalsvg/32x32/apps \\\n \
           /usr/share/icons/hicolor/48x48/apps \\\n \
           /usr/share/icons/hicolor/32x32/apps \\\n \
           /usr/share/icons/locolor/32x32/apps \\\n \
          /\" \n\n"

# In what order we output the menus.
treewalk="(M)"

# get the translated title and replace quotation characters with spaces
function ttitle()=replacewith(encode_translate(lang(),title(),"utf-8"),"\"\'","  ")

# split mime types (separated by either ; or ,) and create dirs prefixed by a .
# symlink the appdir into the mime directories
# remove a trailing separator from the mimetypes or the last symlink goes to 
# the main SendTo dir
function mimelink()= \
    "dir=`pwd`\n" \
    "pushd .\n" \
    "cd /usr/share/Choices/SendTo \n" \
    "types=`echo \"."$mimetypes"\" | sed -e \"s/[,;]$// ; s/[,;]/ ./g ; s/\\//_/g\"`\n" \
    "echo \"$types\"|xargs mkdir -p  \n" \
    "echo \"$types\"|xargs -n 1 ln -s \"$dir/"ttitle()"\"\n" \
    "popd \n"	

function linkicon()= \
    "\nif test -z \""$icon"\" ; then\n" \
    "    for DIR in ${ICONDIRS} ; do\n" \
    "       if test -f \"${DIR}/windowmanager_section.png\" ; then\n" \
    "           ln -s \"${DIR}/windowmanager_section.png\" \"" ttitle() "/.DirIcon\"\n" \
    "           break\n" \
    "       fi\n" \
    "    done\n" \
    "else\n" \
    "    for FILE in ${ICONDIRS} ; do\n" \
    "       if test -f \"${FILE}/"$icon"\" ; then\n" \
    "           ln -s \"${FILE}/"$icon"\" \"" ttitle() "/.DirIcon\"\n" \
    "           break\n" \
    "       fi\n" \
    "    done\n" \
    "fi\n\n"

# get the AppInfo metadata from rpm.
# TODO: 1. there could be characters in the spec file fields that need to be 
#          escaped
#       2. missing translated longtitles lead to garbage and broken XML
function appinfo()= \
    "echo \"<?xml version=\\\"1.0\\\"?><AppInfo><Summary>"replacewith(encode_translate(lang(),$longtitle,"utf-8"),"\"\'","  ")"</Summary>\" > \""ttitle()"/AppInfo.xml\"\n" \
    "rpm -q "$package" --queryformat \"<About><Purpose>%{SUMMARY}</Purpose><Version>%{VERSION}</Version><License>%{LICENSE}</License><Homepage>%{URL}</Homepage></About>\" >> \""ttitle()"/AppInfo.xml\"\n" \
    "echo \"</AppInfo>\">> \""ttitle()"/AppInfo.xml\"\n"


supported

x11= "mkdir -p \""ttitle()"\"\n" \
     "echo \"#!/bin/sh\" > \""ttitle()"/AppRun\"\n" \
     "echo \"# Generated by /etc/menu-methods/rox : do not edit \"" \
     " >> \""ttitle()"/AppRun\"\n" \
     "echo \""esc($command,"\\$")" \\\"\\\$@\\\"\" >> \""ttitle()"/AppRun\"\n" \
     "chmod 755 \""ttitle()"/AppRun\"\n" \
     "docdir=/usr/share/doc/`rpm -q "$package" --queryformat %{name}-%{version}` \n" \
     "if test -d \"$docdir\" \n" \
     "then ln -s $docdir \""ttitle()"/Help\"\n" \
     "fi \n" \
     linkicon() \
     appinfo() \
     ifroot(ifnempty($mimetypes,mimelink()),"")

text="mkdir -p \""ttitle()"\"\n" \
     "echo \"#!/bin/sh\" > \""ttitle()"/AppRun\"\n" \
     "echo \"# Generated by /etc/menu-methods/rox : do not edit \"" \
     " >> \""ttitle()"/AppRun\"\n" \
     "echo \"xvt -T \\\""esc(ttitle(), "()")"\\\" -e \\\""esc($command,"\\$")" \\\"\\\$@\\\"\\\"\" >> \""ttitle()"/AppRun\"\n" \
     "chmod 755 \""ttitle()"/AppRun\"\n" \
     "docdir=/usr/share/doc/`rpm -q "$package" --queryformat %{name}-%{version}` \n" \
     "if test -d \"$docdir\" \n" \
     "then ln -s $docdir \""ttitle()"/Help\"\n" \
     "fi \n" \
     linkicon() \
     appinfo() \
     ifroot(ifnempty($mimetypes,mimelink()),"")

endsupported


startmenu="cd \""ttitle()"\"\n\n""
submenutitle="mkdir -p \""ttitle()"\"\n"linkicon()
endmenu="\ncd ../\n\n"

# Command to run at end.
postrun=ifroot("rm -rf /usr/share/Choices/SendTo \n" \
               "mkdir -p /usr/share/Choices/SendTo \n","") \
        " rm -rf Mandrake; mkdir -p Mandrake  \n" \
        "ln -s /usr/share/icons/large/mandrake.png Mandrake/.DirIcon; " \
        "source roxmenu; rm roxmenu "
