#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/create-menu - create a new menu file
#
# Creation:	    2005-04-09  jed
# Last Update:  $Id$
#
# Copyright (c) 2001-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org
#
# Usage:
#
# create-menu menu-file menu-title
#
#          menu-file      filename relativ to menupath /var/install/menu
#
#          menu-title     title of the new menu
#
#
# new style version (for XML-menus)
# ---------------------------------
# 
# create-menu --menu setup.services.foo.menu --title 'foo administration' --package base
#
#          --quiet    -   suppress all screen output 
#
#          --menu     -   filename relativ to menupath /var/install/menu       
#
#          --title    -   title of the new menu
#
#          --package  -   package name for the new menu [optional]
#
# 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.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

menu_path='/var/install/menu'
pgmname=$(basename ${0})

# ---------------------------------------------------------------------------
# usage
# ---------------------------------------------------------------------------
usage()
{
cat  <<EOF
    
 Usage:
 ${0}
                    
           -q, --quiet    -   suppress all screen output         
           -m, --menu     -   filename relativ to menupath /var/install/menu
           -t, --title    -   title of the new menu
           -p, --package  -   package name for the new menu [optional]


 Example: ${0} --menu setup.services.foo.menu  --title 'Foo administration'

EOF


}

# ---------------------------------------------------------------------------
# work
# ---------------------------------------------------------------------------
work()
{
    if [ ! -f "${menu_path}/${menu_file}" ]
    then
        if [ -z "${package_name}" ]
        then
            package_name=$(echo ${menu_file} | cut -d. -f3)
        fi  

        {
            echo "<!-- ${menu_path}/${menu_file} -->"
            echo "<!-- Creation: ${EISDATE} ${EISTIME} by ${pgmname} -->"
            echo "<package>${package_name}</package>"
            echo "<title>${menu_title}</title>"
        } > ${menu_path}/${menu_file}
        chmod 0640 ${menu_path}/${menu_file}
          
        ret=0
    else
        myecho --error "Menu file '${menu_file}' exists!"
    fi    
}

# ---------------------------------------------------------------------------
# myecho
# ---------------------------------------------------------------------------
myecho()
{
    if ! ${quiet:-false}
    then
        mecho ${1} "${2}"
    fi
}

#----------------------------------------------------------------------------
# check menu naming
# input: $1 - menu name
# return: 0 - ok
#         1 - flase
#----------------------------------------------------------------------------
check_menu_naming()
{
    p_name="${1}"
    m_name="${2}"
    rval=1
    
    if [ -n "${p_name}" ]
    then
        #/var/install/menu/setup.services.foo.menu
        echo "${m_name}"|grep -q "^setup\.services\.${p_name}\.menu$"
    
        if [ $? -eq 0 ]
        then
            rval=0
        else    
            #/var/install/menu/setup.services.foo.bar.menu
            echo "${m_name}"|grep -E -q "^setup\.services\.${p_name}\..+\.menu$"

            if [ $? -eq 0 ]
            then
                rval=0
            fi
        fi
    fi
    
    return $rval
}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    ret=1

    if [ ${#} -eq 0 ]
    then
        usage
        exit 0
    fi

    while [ ${#} -gt 0 ]
    do
        case ${1} in
        --help|-H|-h|-help|help)
            usage
            exit 0
            ;;
        -q|--quiet)
            quiet=true
            shift
            ;;
        -m|--menu)
            menu_file=$(basename ${2})  
            shift 2
            ;;
        -t|--title)
            menu_title="${2}" 
            shift 2
            ;;
        -p|--package)
            package_name="${2}"
            shift 2
            ;;
        *)
            args=$((${#} - 1))

            menu_file=$(basename ${1})
            shift 1
            menu_title="${*}"
            package_name=$(echo ${menu_file} | cut -d. -f3)
            shift ${args}
            ;;
        esac
    done

    work

    exit ${ret}
}
# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------