#!/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, 2005
# - rewrite all menu handlers (only for cp1251 wine encoding)
# Modified Lav <lav@altlinux.ru> 2005
# - now local encoding used from WINE
# - remove -r option for iconv (I hope we have all needed characters in UTF-8)
#
# 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
#

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

enc_to_utf8()
{
	# -r possible only in ALT Linux
	iconv -t UTF-8 || exit 1
}

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

xname=`basename "$link"`
link="${link/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

freedesktop_entry()
{
    cat <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=$xname
Exec=wine "$path" $args
Type=Application
Comment=$descr
Categories=Application;WINE;
EOF
    [ -n "$workdir" ] && echo "Path=$workdir"
    [ -n "$xpmicon" ] && echo "Icon=$xpmicon"
}

# copy the icon file
dir="$HOME/.icons"
mkdir -p "$dir"
if [ -f "$icon" ] ; then
	xpmicon="$dir/$xlname.xpm"
	# Note: if icon will not xpm?
	cp "$icon" "$xpmicon"
	rm "$icon"
else
	xpmicon=""
fi

# Menu
if [ $mode = "menu" ] ; then
	SECT="WINE Applications"
	# Freedesktop
	if [ -d "$HOME/.local" ] ; then
		mkdir -p "$HOME/.local/share/applications/$SECT/$dirlink"
		freedesktop_entry | enc_to_utf8 > "$HOME/.local/share/applications/$SECT/$dirlink/$xlname.desktop"
	fi
	# Desktop's menu folder
	if [ -d "$HOME/Desktop/$SECT" ] ; then
		mkdir -p "$HOME/Desktop/$SECT/$dirlink"
		freedesktop_entry | enc_to_utf8 > "$HOME/Desktop/$SECT/$dirlink/$xlname.desktop"
	fi
fi

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