#!/bin/sh
#
# Create menu/desktop entries for an application
# This is used by the IShellLink interface
#
# Copyright 2000 Alexandre Julliard
# Copyright 2002-2003 TransGaming Technologies Inc.
#
mode=""
args=""
menu=""
icon=""
descr=""
link=""
path=""
workdir=""
name=""
fe_icon=""

WINE_LOADER=${WINE_LOADER:-wine}


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 1
}

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

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

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

# Handle special cases where we want to mess with the link that's being
# passed.  EG: If we see 'Warcraft III.exe', we want to substitute
# 'War3.exe', and add the '-- -opengl' parameter.
newpath=`echo $path | sed -n 's/Warcraft III\.exe/War3.exe/p'`
if [ -n "$newpath" ]; then
    path=$newpath
    args="-opengl"
fi

# Set up the loader command to use the working dir if it's winex.
# Generally, setting the 'path' in many desktop systems does the
# wrong thing.
# Note that if we're using the winex launcher script we don't want 
# any double-dashes contaminating the input. That's the signal to 
# the winex script that 'I know what I'm doing, don't mess with my 
# input'.  
if [ -n "$workdir" -a ["$WINE_LOADER" = "winex" -o "$WINE_LOADER" = "winex3"] ]; then
    WINE_MENU="$WINE_LOADER -workdir \\\"$workdir\\\""
    WINE_START="$WINE_LOADER -workdir \"$workdir\""
    DOUBLEDASH=""
else
    WINE_MENU=$WINE_LOADER
    WINE_START=$WINE_LOADER
    DOUBLEDASH="--"
fi

# We need to determine the KDE version (and without crapping out if it's
# not found.)
if which kde-config > /dev/null 2>&1 ; then  
    kdeversionmajor=`kde-config --version |grep KDE | cut -f2 -d \ | cut -d. -f1`
    kdeversionminor=`kde-config --version |grep KDE | cut -f2 -d \ | cut -d. -f0,2`
else
    kdeversionmajor=
    kdeversionminor=
fi

# The insanity below is needed to make sure that the correct quoting
# occurs when going 1st to the .menu/wine file, and then from there
# to the appropriate desktop system entries via update-menus.  Note
# that every round trip through the shell string processing affects
# the count of slashes.  Thus, four \s here == 1 in the menu file.
# Thus, the first replacement below actually replaces each input \ 
# in the path with 8 \s in the menu file.  That in turn gets mashed
# into 2 \s in the kde and gnome desktop links created by update-menus

# Unfortunately, this doesn't actually seem to work for the test
# case that it was meant to handle (3DMark2000).  We'll leave the 
# code in here though anyway, since it seems like it's quite close

# Substitutions for mdk_entry
mdk_args=`echo $args | perl -e '$t = pack('C', 39); while (<>) { s,\\\\,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,g; s,([ "]),\\\\$1,g; s,$t,\\\\$t,g; print "$_"; }'`

# We use a slightly different substitution if we're going directly 
# to the desktop system ourselves.  Note that kde2 and gnome share
# the same format of desktop entries.
# Substitutions for gnome_entry
gnome_args=`echo $args | perl -e '$t = pack('C', 39); while (<>) { s,\\\\,\\\\\\\\,g; print "$_"; }'`

# We would do something similar to the above for the actual launch path,
# but we run into problems with gnome, which does an extra shell evaluation 
# pass, but *only* for menu items, not desktop entries.  A better solution
# is to simply reverse the slash direction in the path.  Wine will do
# the right thing, and we avoid all troubles with backslashes and quoting.
path=`echo $path | sed -e 's,\\\\,/,g'` 

kde_entry()
{
    xname=`basename "$link"`
    cat <<EOF
# KDE Config File
[KDE Desktop Entry]
Name=$xname
Exec=$WINE_START "$path" $DOUBLEDASH $args
Type=Application
Comment=$descr
EOF
    [ -z "$workdir" ] || echo "Path=\"$workdir\""
    [ -z "$xpmicon" ] || echo "Icon=$xpmicon"
}

gnome_entry()
{
    xname=`basename "$link"`
    cat <<EOF
[Desktop Entry]
Name=`basename "$link"`
Exec=$WINE_START "$path" $DOUBLEDASH $gnome_args
Type=Application
Comment=$descr
EOF
    [ -z "$workdir" ] || echo "Path=\"$workdir\""
    [ -z "$xpmicon" ] || echo "Icon=$xpmicon"
}

mdk_entry()
{
    base=`basename "$link"`
    section=`dirname "$link"`
    [ -z "$icon" ] || xicon="icon=\"$xpmicon\""
    echo "?package(local.Wine):needs=x11 section=\"WineX/$section\" title=\"$base\" longtitle=\"$descr\" command=\"$WINE_MENU \\\"$path\\\" $DOUBLEDASH $mdk_args\" $xicon"
}

# copy the icon file to a specified dir and set xpmicon to the resulting path
copy_icon()
{
  dir="$1"
  mkdir -p "$dir"
  mkdir -p "$dir/""`dirname "$link"`" || true
  if [ -f "$icon" ]
  then
    cp "$icon" "$dir/$link.xpm"
    xpmicon="$dir/$link.xpm"
    cp "$icon" "$WINEPREFIX""/icons/`basename "$link"`.xpm"
  else
    xpmicon=""
  fi
}


# p2p - if we're running from p2p, don't install icons elsewhere

if [ -n "$WINEX_INSTALL_FILE" ];
then
   name="`basename "$link"`"
   fe_icon="$WINEPREFIX""/icons/`basename "$link"`.xpm"
cat << EOF >> $WINEX_INSTALL_FILE
$name
$path
$fe_icon
$workdir
EOF
exit 0
fi

# Debian/Mandrake

which update-menus > /dev/null 2>&1
if [ $? = 0 -a $mode = "menu" ]
then
  iconname="`basename "$link"`.xpm"
  dir="$HOME/.menu/icons"
  if [ -f "$icon" ]
  then
    mkdir -p $dir
    cp "$icon" "$dir/$iconname"
    xpmicon="$dir/$iconname"
  else
    xpmicon=""
  fi
  if [ "$kdeversionmajor" = 3 ]
  then
    if [ -d "$HOME/.kde3" ]
    then
      if [ ! -d "$HOME/.kde3/share/applnk-mdk/WineX" ]
      then
	mkdir "$HOME/.kde3/share/applnk-mdk/WineX"  
      fi
      copy_icon "$HOME/.kde3/share/applnk-mdk/WineX"
      if [ $mode = "menu" ]
      then
        gnome_entry > "$HOME/.kde3/share/applnk-mdk/WineX/$link.desktop"
      elif [ -d "$HOME/Desktop" ]
      then
        gnome_entry > "$HOME/Desktop/$link.desktop"
      fi
    else						  
      copy_icon "$HOME/.kde/share/applnk/WineX"
      if [ $mode = "menu" ]
      then
	gnome_entry > "$HOME/.kde/share/applnk/WineX/$link.desktop"
      elif [ -d "$HOME/Desktop" ]
      then
        gnome_entry > "$HOME/Desktop/$link.desktop"
      fi
    fi
    kbuildsycoca &> /dev/null
  else
    mdk_entry >> "$HOME/.menu/wine"
    update-menus > /dev/null 2>&1
  fi
fi

# KDE

if [ -d "$HOME/.kde" ]
then
  copy_icon "$HOME/.kde/share/applnk/WineX"
  if [ $mode = "menu" ]
  then
    gnome_entry > "$HOME/.kde/share/applnk/WineX/$link.desktop"
  elif [ -d "$HOME/Desktop" ]
  then
    gnome_entry > "$HOME/Desktop/$link.desktop"
  fi
fi
if [ -d "$HOME/.kde3" ]
then
  copy_icon "$HOME/.kde3/share/applnk/WineX"
  if [ $mode = "menu" ]
  then
    gnome_entry > "$HOME/.kde3/share/applnk/WineX/$link.desktop"
  elif [ -d "$HOME/Desktop" ]
  then
    gnome_entry > "$HOME/Desktop/$link.desktop"
  fi
  kbuildsycoca &> /dev/null
fi
						    


# Gnome

if [ -d "$HOME/.gnome" ]
then
  copy_icon "$HOME/.gnome/apps/WineX"
  if [ $mode = "menu" ]
  then
    gnome_entry > "$HOME/.gnome/apps/WineX/$link.desktop"
  elif [ -d "$HOME/.gnome-desktop" ]
  then
    gnome_entry > "$HOME/.gnome-desktop/$link.desktop"
  fi
fi

 

exit 0
