#!/bin/sh
#
# Create menu/desktop entries for an application
# This is used by the IShellLink interface
#
# Copyright 2000 Alexandre Julliard
# Modified Lav <lav@altlinux.ru> 2003
# - remove special support for windows manager's menu
# Modified Lav <lav@altlinux.ru> 2004
# - rewrite all menu handlers (only for cp1251 wine encoding)
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# For language diagnostic purposes
LOCALLANG=${LC_ALL:-${LC_CTYPE:-${LANG:-POSIX}}}
LOCALEENC=${LOCALLANG#*.}

mode=""
args=""
menu=""
icon=""
descr=""
link=""
path=""
workdir=""

enc_to_local()
{
	RES=`echo "$1" | iconv -f cp1251 -t $LOCALEENC `
	if [ $? ] ; then
		echo "$RES"
	else
		echo "$1"
	fi
}

enc_to_utf8()
{
	RES=`echo "$1" | iconv -f cp1251 -t UTF-8 `
	if [ $? ] ; then
		echo "$RES"
	else
		echo "$1"
	fi
}

usage()
{
    cat <<EOF
usage: wineshelllink options

options:
  --desktop     create a desktop link
  --menu        create a menu entry
  --path xx     path to the application
  --link xx     name of link to create
  --args xx     command-line arguments for the application
  --icon xx     icon to display
  --workdir xx  working directory for the application
  --descr xx    application description

EOF
    exit 2
}

if [ $# -eq 0 ] ; then
    usage
fi

while [ $# -gt 0 ]
do
  case "$1" in
    --desktop) mode="desktop"; shift 1 ;;
    --menu)    mode="menu"; shift 1 ;;
    --path)    path="$2"; shift 2 ;;
    --link)    link="$2"; shift 2 ;;
    --args)    args="$2"; shift 2 ;;
    --icon)    icon="$2"; shift 2 ;;
    --descr)   descr="$2"; shift 2 ;;
    --workdir) workdir="$2"; shift 2 ;;
    *) usage ;;
  esac
done

# Recoding
path=`enc_to_utf8 "$path"`
icon=`enc_to_local "$icon"`
workdir=`enc_to_utf8 "$workdir"`
descr=`enc_to_utf8 "$descr"`
xlname=`basename "$link"`
xname=`enc_to_utf8 "$xlname"`
link=`enc_to_local "${link/Programs\//Wine Programs/}"`
xlname=`basename "$link"`
xlname=${xlname/ /_}
dirlink=`dirname "$link"`

if [ -z "$mode" ] ; then
    echo "Either --desktop or --menu required"
    usage
fi

if [ -z "$link" ] ; then
    echo "You must specify a link name with --link"
    usage
fi

gnome_entry()
{
    cat <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=$xname
Exec=wine "$path" -- $args
Type=Application
Comment=$descr
EOF
    [ -z "$workdir" ] || echo "Path=\"$workdir\""
    [ -z "$xpmicon" ] || echo "Icon=$xpmicon"
}

# copy the icon file
dir="$HOME/.menu/icons"
mkdir -p "$dir"
if [ -f "$icon" ] ; then
	cp "$icon" "$dir/$xlname.xpm"
	xpmicon="$dir/${xname/ /_}.xpm"
else
	xpmicon=""
fi

# Menu
if [ $mode = "menu" ] ; then
	# KDE
	if [ -d "$HOME/.kde" ] ; then
		mkdir -p "$HOME/.kde/share/applnk-alt/Applications/Emulators/$dirlink"
		gnome_entry > "$HOME/.kde/share/applnk-alt/Applications/Emulators/$dirlink/$xlname.desktop"
	fi
	# Gnome
	if [ -d "$HOME/.gnome" ] ; then
		mkdir -p "$HOME/.gnome/apps-alt/Applications/Emulators/$dirlink"
		gnome_entry > "$HOME/.gnome/apps-alt/Applications/Emulators/$dirlink/$xlname.desktop"
	fi
fi

# Desktop
if [ $mode = "desktop" ] ; then
	if [ -d "$HOME/Desktop" ] ;	then
			gnome_entry > "$HOME/Desktop/$xlname.desktop"
	fi
fi
