#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/add-menu - add a menu entry
# Copyright (c) 2001-2016 The Eisfair Team <team@eisfair.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#-----------------------------------------------------------------------------
# add-menu [--menu] menu-file sub-menu-file comment
# add-menu --script menu-file script-file comment
#
#          --menu         add <menu>-Tag   [default]
#          --script       add <script>-Tag instead of <menu>-Tag
#          --logfile      add <doc>-Tag for logfiles instead of <menu>-Tag
#
#          menu-file      filename relativ to menupath /var/install/menu
#                         The new entry is added to this menu.
#
#          sub-menu-file  filename relativ to menupath /var/install/menu
#                         The new entry
#                             <menu file="sub-menu-file">comment</menu>
#                         is added to the end of the menu-file
#                         if such a line does not already exist.
#
#          script-file    filename relativ to /var/install/bin or absolute path
#                         The new entry
#                             <script file="script-file">comment</script>
#                         is added to the end of the menu-file
#                         if such a line does not already exist.
#
#          comment        text to display in the menu
#----------------------------------------------------------------------------

case "$1" in
    --script)
        shift
        tag="script"
    ;;
    --menu)
        shift
        tag="menu"
    ;;
    --logfile)
        shift
        tag="doc"
        opt=" tail=\"yes\""
    ;;    
    *)
        tag="menu"
    ;;
esac

menu_file=$1
menu_cmd=$2
shift 2
menu_text="$*"

if [ -f "/var/install/menu/$menu_file" ] ; then
    if grep -q "<${tag} file=\"${menu_cmd}\"" /var/install/menu/${menu_file} ; then
        exit 0
    fi
    echo "<${tag} file=\"${menu_cmd}\"${opt}>${menu_text}</${tag}>" >>/var/install/menu/${menu_file}
fi

exit 0
