summaryrefslogtreecommitdiff
path: root/scripts/xdg-menu
blob: 2382a7a2eaf81ebc7856bb7e0b9d8812bb88fcc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/sh
#---------------------------------------------
#   xdg-menu
#
#   Utility script to install menu items on a Linux desktop.
#   Works on most XDG compliant systems; does
#   not work everywhere.
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.
#
#---------------------------------------------

usage()
{
cat << _USAGE
xdg-menu -- command line tool for (un)installing desktop menu items

Synopsis

   xdg-menu [--help] [--vendor vendor-id] [--noupdate] [[--user] |
   [--system]] [[--install desktop-file] | [--install directory-file] |
   [--install menu-file] | [--uninstall desktop-file] | [--uninstall
   directory-file] | [--uninstall menu-file] | [--forceupdate]]

_USAGE
    exit 1
}

update_desktop_database()
{
#    echo Update desktop database: $mode
    if [ "$mode" = "system" ] ; then
        for x in `echo $PATH | sed 's/:/ /g'` /opt/gnome/bin; do
           if [ -x $x/update-desktop-database ] ; then
              echo Running $x/update-desktop-database
              $x/update-desktop-database
           fi
        done
    fi
}

[ "$1" != "" ] || usage

mode=
action=
update=yes
desktop_file=
while [ $# -gt 0 ] ; do
    parm=$1
    shift

    case $parm in
      --install)
        if [ -n "$action" ] ; then
            echo Error:  Too many options: $parm $1
            exit 1
        fi
        if [ ! -f "$1" ] ; then
            echo Error:  You must specify an existing file to install.
            exit 1
        fi

        action=install
        desktop_file=$1
        shift
        ;;

      --uninstall)
        if [ -n "$action" ] ; then
            echo Error:  Too many options: $parm $1
            exit 1
        fi
        if [ -z "$1" ] ; then
            echo Error:  You must specify a file to uninstall.
            exit 1
        fi
        action=uninstall
        desktop_file=$1
        shift
        ;;

      --forceupdate)
        if [ -n "$action" ] ; then
            echo Error:  Too many options: $parm
            exit 1
        fi

        action=forceupdate
        ;;

      --noupdate)
        update=no
        ;;

      --user)
        mode=user
        ;;

      --system)
        mode=system
        ;;

      --vendor)
        if [ -z "$1" ] ; then
            echo Error:  You must specify a vendor-id.
            exit 1
        fi
        vendor=$1
        shift
        ;;

      *)
        echo "$parm:  Invalid parameter/option"
        usage
        exit 2
        ;;
    esac
done

if [ -z "$mode" ] ; then
    echo Error:  You must specify either --user or --system
    exit 2
fi

if [ -z "$action" ] ; then
    usage
    exit 2
fi

if [ "$action" = "forceupdate" ] ; then
    update_desktop_database
    exit 0;
fi

filetype=
xdg_dir_name=
case $desktop_file in
  *.desktop)
     filetype=desktop
     xdg_dir_name=applications
     ;;
  *.directory)
     fileype=directory
     xdg_dir_name=desktop-directories
     ;;
  *.menu)
     filetype=menu
     xdg_dir_name=menus/applications-merged
     ;;
  *)
     echo Error: File to $action must be a *.desktop, *.directory or *.menu file
     exit 1
     ;;
esac

if [ "$filetype" = "menu" ] ; then
    xdg_user_dir=$XDG_CONFIG_HOME
    [ -n "$xdg_user_dir" ] || xdg_user_dir=$HOME/.config
    xdg_user_dir=$xdg_user_dir/$xdg_dir_name

    xdg_system_dirs=$XDG_CONFIG_DIRS
    [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/etc/xdg
    for x in `echo $xdg_system_dirs | sed 's/:/ /g'` ; do
        if [ -w $x/$xdg_dir_name ] ; then
            xdg_global_dir=$x/$xdg_dir_name
            break
        fi
    done
    [ -w $xdg_global_dir ] || xdg_global_dir=
else
    xdg_user_dir=$XDG_DATA_HOME
    [ -n "$xdg_user_dir" ] || xdg_user_dir=$HOME/.local/share
    xdg_user_dir=$xdg_user_dir/$xdg_dir_name

    xdg_system_dirs=$XDG_DATA_DIRS
    [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
    for x in `echo $xdg_system_dirs | sed 's/:/ /g'` ; do
        if [ -w $x/$xdg_dir_name ] ; then
            xdg_global_dir=$x/$xdg_dir_name
            break
        fi
    done
    [ -w $xdg_global_dir ] || xdg_global_dir=

    if [ "$filetype" = "desktop" ] ; then
        kde_user_dir=$HOME/.kde/share/applnk
        kde_global_dir=/usr/share/applnk
        [ -w $kde_global_dir ] || kde_global_dir=

        gnome_user_dir=$HOME/.gnome/apps
        gnome_global_dir=/usr/share/gnome/apps
        [ -w $gnome_global_dir ] || gnome_global_dir=
    fi
fi


if [ "$mode" = "user" ] ; then
    xdg_dir=$xdg_user_dir
    kde_dir=$kde_user_dir
    gnome_dir=$gnome_user_dir
    my_umask=077
else
    xdg_dir=$xdg_global_dir
    kde_dir=$kde_global_dir
    gnome_dir=$gnome_global_dir
    my_umask=022
    if [ -n "$xdg_dir $kde_dir $gnome_dir" ] ; then
        [ `whoami` = "root" ] || rootmsg="Try as root or use --user."
        echo Error:  No writable system menu directory found. $rootmsg
        exit 3 
    fi
fi


# echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
# echo "[kde|$kde_user_dir|$kde_global_dir]"
# echo "[gnome|$gnome_user_dir|$gnome_global_dir]"

# echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"

basefile=`basename $desktop_file`
[ -z $vendor ] || basefile=$vendor-$basefile

case $action in
    install)
        save_umask=`umask`
        umask $my_umask

        for x in $xdg_dir $kde_dir $gnome_dir ; do
            mkdir -p $x
            cp $desktop_file $x/$basefile
        done

        if [ -f $kde_dir/$basefile ] ; then
            echo "OnlyShowIn=Old;" >> $kde_dir/$basefile
        fi

        if [ -f $gnome_dir/$basefile ] ; then
            echo "OnlyShowIn=Old;" >> $gnome_dir/$basefile
        fi

        umask $save_umask
        ;;

    uninstall)
        for x in $xdg_dir $kde_dir $gnome_dir ; do
            rm -f $x/$basefile
        done

        ;;
esac

if [ "$update" = "yes" ] ; then
    update_desktop_database
fi

exit 0