wineshelllink 5.38 KB
Newer Older
1 2 3 4 5 6
#!/bin/sh
#
# Create menu/desktop entries for an application
# This is used by the IShellLink interface
#
# Copyright 2000 Alexandre Julliard
7
# Copyright 2006 Vitaliy Margolen
8
#
9 10 11 12 13 14 15 16 17 18 19 20
# 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
21
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23
#

24 25
# Note that the link is a relative unix-style path name.  Since the / character
# is not valid in Windows filenames it is an adequate separator to show the
26
# menu structure.
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
mode=""
args=""
menu=""
icon=""
descr=""
link=""
path=""
workdir=""

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

options:
  --desktop     create a desktop link
  --menu        create a menu entry
  --path xx     path to the application
46
  --link xx     name of link to create, including path
47 48 49 50 51 52
  --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
53
    exit 2
54 55
}

56 57 58 59
if [ $# -eq 0 ] ; then
    usage
fi

60 61 62
while [ $# -gt 0 ]
do
  case "$1" in
Simon Walton's avatar
Simon Walton committed
63 64 65 66 67 68 69 70 71
    --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 ;;
72 73 74
  esac
done

75 76
if [ -z "$mode" ] ; then
    echo "Either --desktop or --menu required"
77 78 79
    usage
fi

80 81
if [ -z "$link" ] ; then
    echo "You must specify a link name with --link"
82 83 84
    usage
fi

85
desktop_entry()
86 87 88
{
    cat <<EOF
[Desktop Entry]
89
Name=$linkname
90
Exec=env WINEPREFIX="${WINEPREFIX:-$HOME/.wine}" wine "$path" $args
91
Type=Application
92
StartupWMClass=Wine
93
EOF
94
    [ -z "$descr"   ] || echo "Comment=$descr"
95
    [ -z "$workdir" ] || echo "Path=$workdir"
96 97 98
    [ -z "$xpmicon" ] || echo "Icon=$xpmicon"
}

99
directory_entry()
100
{
101 102 103 104
    cat <<EOF
[Desktop Entry]
Type=Directory
EOF
105
    if [ "$1" = "wine" ] ; then
106 107 108 109 110 111
        echo "Name=Wine"
        echo "Icon=wine"
    else
        echo "Name=$1"
        echo "Icon=folder"
    fi
112 113
}

114 115 116
# copy the icon file to a specified dir and set xpmicon to the resulting path
copy_icon()
{
117 118
    if [ -f "$icon" ]
    then
119 120
        xpmicon=`basename "$icon"`
        xpmicon=${xpmicon%.*}
121 122

        mkdir -p "$1"
123
        cp "$icon" "$1"
124 125 126
    else
        xpmicon=""
    fi
127 128
}

129
# XDG
130

131 132
xdg_config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/menus/applications-merged"
xdg_data_dir="${XDG_DATA_HOME:-$HOME/.local/share}"
133

134 135 136
# Create common directories
mkdir -p "$xdg_config_dir"
mkdir -p "$xdg_data_dir/desktop-directories"
137

138 139 140 141 142 143 144 145
get_menu_entries()
{
    tmp="$xdg_config_dir/$1.menu"

    if [ -r "$tmp" ] ; then
        awk '
BEGIN {  RS="<" }
/^Filename/ {
146 147
  RSTART=index($0,">")
  if (RSTART>0) {
148 149 150
     print substr($0,RSTART+1)
  }
}' $tmp
151
    fi
152
}
153

154 155 156 157 158
# Input
#   menu file name
#   new item
write_menu_file()
{
159 160
    menu=`echo "$1" | sed 's!/!-!g'`
    filename=`echo "$2" | sed 's!/!-!g'`
161

162
    tmpfile=`mktemp /tmp/wine.XXXXXX`
163 164 165 166 167 168
    (
        echo '<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"'
        echo '"http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd">'
        echo '<Menu>'
        echo '  <Name>Applications</Name>'

169
        IFS="/"
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

        fullname='wine'
        for i in $1; do
            echo "  <Menu>"
            echo "    <Name>$fullname-$i</Name>"
            echo "    <Directory>$fullname-$i.directory</Directory>"

            dir_file_name="$xdg_data_dir/desktop-directories/$fullname-$i.directory"
            if [ ! -f "$dir_file_name" ] ; then
                directory_entry "$i" > "$dir_file_name"
            fi
            test "$i" = "wine" || fullname="$fullname-$i"
        done

        echo "    <Include>"

        IFS="
"
188 189
        for i in `get_menu_entries "$menu"`
        do
190
            test "$i" = "$filename" && continue
191 192 193 194
            echo "      <Filename>$i</Filename>"
        done

        # New record
195
        echo "      <Filename>$filename</Filename>"
196 197
        echo "    </Include>"

198
        IFS='/'
199 200 201 202 203 204 205
        for i in $1; do
            echo "  </Menu>"
        done
        echo '</Menu>'                        
    ) > $tmpfile
    chmod 0600 $tmpfile

206
    mv -f $tmpfile "$xdg_config_dir/$menu.menu"
207
}
208

209

210
copy_icon "$xdg_data_dir/icons"
Dustin Navea's avatar
Dustin Navea committed
211

212 213 214 215 216
linkname=`basename "$link"`

if [ $mode = "menu" ] ; then
    mkdir -p "$xdg_data_dir/applications/wine/`dirname "$link"`"

217
    linkpath=`dirname "$link"`
218
    if [ "$linkpath" = "." ] ; then
219 220
        linkpath=""
    else
221
        linkpath="/$linkpath"
Dustin Navea's avatar
Dustin Navea committed
222
    fi
223

224
    desktop_entry > "$xdg_data_dir/applications/wine/$link.desktop"
225
    write_menu_file "wine$linkpath" "wine$linkpath/$linkname.desktop"
226
else
227 228 229 230 231 232 233
    if [ -d "$HOME/Desktop" ]
    then
      desktop_target="$HOME/Desktop/$linkname.desktop"
    else
      desktop_target="$HOME/$linkname.desktop"
    fi
    desktop_entry > "$desktop_target"
234 235 236
fi

exit 0