OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 #--------------------------------------------- |
| 3 # xdg-icon-resource |
| 4 # |
| 5 # Utility script to install icons on a Linux desktop. |
| 6 # |
| 7 # Refer to the usage() function below for usage. |
| 8 # |
| 9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
| 11 # |
| 12 # LICENSE: |
| 13 # |
| 14 #--------------------------------------------- |
| 15 |
| 16 manualpage() |
| 17 { |
| 18 cat << _MANUALPAGE |
| 19 _MANUALPAGE |
| 20 } |
| 21 |
| 22 usage() |
| 23 { |
| 24 cat << _USAGE |
| 25 _USAGE |
| 26 } |
| 27 |
| 28 #@xdg-utils-common@ |
| 29 |
| 30 # Set GTK_UPDATE_ICON_CACHE to gtk-update-icon-cache executable path or |
| 31 # to "-" if not found. |
| 32 GTK_UPDATE_ICON_CACHE= |
| 33 find_gtk_update_icon_cache() |
| 34 { |
| 35 [ -n "$GTK_UPDATE_ICON_CACHE" ] && return; |
| 36 |
| 37 GTK_UPDATE_ICON_CACHE="-" |
| 38 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do |
| 39 DEBUG 3 "Checking $x for gtk-update-icon-cache" |
| 40 if [ -x "$x/gtk-update-icon-cache" ] ; then |
| 41 DEBUG 1 "Found $x/gtk-update-icon-cache" |
| 42 GTK_UPDATE_ICON_CACHE="$x/gtk-update-icon-cache" |
| 43 return |
| 44 fi |
| 45 done |
| 46 } |
| 47 |
| 48 # Start GNOME legacy workaround section |
| 49 need_dot_icon_path() |
| 50 { |
| 51 # GTK < 2.6 uses ~/.icons but not XDG_DATA_HOME/icons |
| 52 # The availability of gtk-update-icon-cache is used as indication |
| 53 # of whether the system is using GTK 2.6 or later |
| 54 find_gtk_update_icon_cache |
| 55 [ "$GTK_UPDATE_ICON_CACHE" != "-" ] && return 1; |
| 56 return 0; |
| 57 } |
| 58 |
| 59 update_icon_database() |
| 60 { |
| 61 # Touch me, I'm dirty |
| 62 touch "$1/.@NAME@-dummy" |
| 63 rm -f "$1/.@NAME@-dummy" |
| 64 |
| 65 # Don't create a cache if there wan't one already |
| 66 if [ -f "$1/icon-theme.cache" ] ; then |
| 67 find_gtk_update_icon_cache |
| 68 if [ "$GTK_UPDATE_ICON_CACHE" != "-" ] ; then |
| 69 DEBUG 1 "Running $GTK_UPDATE_ICON_CACHE -f -t \"$1\"" |
| 70 eval '$GTK_UPDATE_ICON_CACHE -f -t "$1"'$xdg_redirect_output |
| 71 return |
| 72 fi |
| 73 fi |
| 74 } |
| 75 |
| 76 [ x"$1" != x"" ] || exit_failure_syntax |
| 77 |
| 78 mode= |
| 79 action= |
| 80 update=yes |
| 81 size= |
| 82 theme=hicolor |
| 83 context=apps |
| 84 icon_file= |
| 85 icon_name= |
| 86 |
| 87 case $1 in |
| 88 install) |
| 89 action=install |
| 90 ;; |
| 91 |
| 92 uninstall) |
| 93 action=uninstall |
| 94 ;; |
| 95 |
| 96 forceupdate) |
| 97 action=forceupdate |
| 98 ;; |
| 99 |
| 100 *) |
| 101 exit_failure_syntax "unknown command '$1'" |
| 102 ;; |
| 103 esac |
| 104 |
| 105 shift |
| 106 |
| 107 vendor=true |
| 108 while [ $# -gt 0 ] ; do |
| 109 parm="$1" |
| 110 shift |
| 111 |
| 112 case $parm in |
| 113 --noupdate) |
| 114 update=no |
| 115 ;; |
| 116 |
| 117 --mode) |
| 118 if [ -z "$1" ] ; then |
| 119 exit_failure_syntax "mode argument missing for --mode" |
| 120 fi |
| 121 case "$1" in |
| 122 user) |
| 123 mode="user" |
| 124 ;; |
| 125 |
| 126 system) |
| 127 mode="system" |
| 128 ;; |
| 129 |
| 130 *) |
| 131 exit_failure_syntax "unknown mode '$1'" |
| 132 ;; |
| 133 esac |
| 134 shift |
| 135 ;; |
| 136 |
| 137 --theme) |
| 138 if [ -z "$1" ] ; then |
| 139 exit_failure_syntax "theme argument missing for --theme" |
| 140 fi |
| 141 theme="$1" |
| 142 shift |
| 143 ;; |
| 144 |
| 145 --size) |
| 146 if [ -z "$1" ] ; then |
| 147 exit_failure_syntax "size argument missing for --size" |
| 148 fi |
| 149 if echo "$1" | grep '[^0-9]' > /dev/null 2> /dev/null; then |
| 150 exit_failure_syntax "size argument must be numeric" |
| 151 fi |
| 152 size="$1" |
| 153 shift |
| 154 ;; |
| 155 |
| 156 --context) |
| 157 if [ -z "$1" ] ; then |
| 158 exit_failure_syntax "context argument missing for --context" |
| 159 fi |
| 160 context="$1" |
| 161 shift |
| 162 ;; |
| 163 |
| 164 --novendor) |
| 165 vendor=false |
| 166 ;; |
| 167 |
| 168 -*) |
| 169 exit_failure_syntax "unexpected option '$parm'" |
| 170 ;; |
| 171 |
| 172 *) |
| 173 if [ -n "$icon_name" ] ; then |
| 174 exit_failure_syntax "unexpected argument '$parm'" |
| 175 elif [ -n "$icon_file" ] ; then |
| 176 icon_name="$parm" |
| 177 else |
| 178 if [ "$action" = "install" ] ; then |
| 179 check_input_file "$parm" |
| 180 fi |
| 181 icon_file="$parm" |
| 182 fi |
| 183 ;; |
| 184 esac |
| 185 done |
| 186 |
| 187 # Shouldn't happen |
| 188 if [ -z "$action" ] ; then |
| 189 exit_failure_syntax "command argument missing" |
| 190 fi |
| 191 |
| 192 # Shouldn't happen |
| 193 if [ -z "$context" ] ; then |
| 194 exit_failure_syntax "context argument missing" |
| 195 fi |
| 196 |
| 197 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then |
| 198 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then |
| 199 mode="system" |
| 200 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then |
| 201 mode="user" |
| 202 fi |
| 203 fi |
| 204 |
| 205 if [ -z "$mode" ] ; then |
| 206 if [ `whoami` = "root" ] ; then |
| 207 mode="system" |
| 208 else |
| 209 mode="user" |
| 210 fi |
| 211 fi |
| 212 |
| 213 xdg_dir_name="icons/$theme" |
| 214 |
| 215 xdg_user_dir="$XDG_DATA_HOME" |
| 216 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" |
| 217 xdg_user_prefix="$xdg_user_dir/icons" |
| 218 xdg_user_dir="$xdg_user_dir/$xdg_dir_name" |
| 219 |
| 220 xdg_global_dir= |
| 221 xdg_global_prefix= |
| 222 xdg_system_dirs="$XDG_DATA_DIRS" |
| 223 [ -n "$xdg_system_dirs" ] || xdg_system_dirs="/usr/local/share/:/usr/share/" |
| 224 for x in `echo "$xdg_system_dirs" | sed 's/:/ /g'`; do |
| 225 if [ -w $x/$xdg_dir_name ] ; then |
| 226 xdg_global_prefix="$x/icons" |
| 227 xdg_global_dir="$x/$xdg_dir_name" |
| 228 break |
| 229 fi |
| 230 done |
| 231 [ -w $xdg_global_dir ] || xdg_global_dir= |
| 232 |
| 233 dot_icon_dir= |
| 234 dot_base_dir= |
| 235 if [ x"$mode" = x"user" ] ; then |
| 236 xdg_base_dir="$xdg_user_dir" |
| 237 #Gnome 2.8 supports ~/.icons but not XDG_DATA_HOME |
| 238 if need_dot_icon_path ; then |
| 239 dot_icon_dir="$HOME/.icons" |
| 240 dot_base_dir="$dot_icon_dir/$theme" |
| 241 fi |
| 242 else |
| 243 xdg_base_dir="$xdg_global_dir" |
| 244 if [ -z "$xdg_base_dir" ] ; then |
| 245 exit_failure_operation_impossible "No writable system icon directory fou
nd." |
| 246 fi |
| 247 fi |
| 248 |
| 249 if [ x"$action" = x"forceupdate" ] ; then |
| 250 if [ -n "$icon_file" ] ; then |
| 251 exit_failure_syntax "unexpected argument '$icon_file'" |
| 252 fi |
| 253 update_icon_database $xdg_base_dir |
| 254 if [ -n "$dot_icon_dir" ] ; then |
| 255 if [ -d "$dot_icon_dir/" -a ! -L "$dot_icon_dir" ] ; then |
| 256 update_icon_database $dot_base_dir |
| 257 fi |
| 258 fi |
| 259 exit_success |
| 260 fi |
| 261 |
| 262 if [ -z "$icon_file" ] ; then |
| 263 if [ x"$action" = x"install" ] ; then |
| 264 exit_failure_syntax "icon-file argument missing" |
| 265 else |
| 266 exit_failure_syntax "icon-name argument missing" |
| 267 fi |
| 268 fi |
| 269 |
| 270 xdg_size_name= |
| 271 extension= |
| 272 |
| 273 if [ -z "$size" ] ; then |
| 274 exit_failure_syntax "the icon size must be specified with --size" |
| 275 fi |
| 276 xdg_size_name="${size}x${size}" |
| 277 |
| 278 if [ x"$action" = x"install" ] ; then |
| 279 case $icon_file in |
| 280 *.xpm) |
| 281 extension="xpm" |
| 282 ;; |
| 283 *.png) |
| 284 extension="png" |
| 285 ;; |
| 286 *) |
| 287 exit_failure_syntax "icon file to install must be a *.png or *.xpm file" |
| 288 ;; |
| 289 esac |
| 290 fi |
| 291 |
| 292 if [ -n "$icon_name" ] ; then |
| 293 case $icon_name in |
| 294 *.png) |
| 295 exit_failure_syntax "icon name should not include an extension" |
| 296 ;; |
| 297 *.xpm) |
| 298 exit_failure_syntax "icon name should not include an extension" |
| 299 ;; |
| 300 esac |
| 301 fi |
| 302 |
| 303 # Start KDE legacy workaround section |
| 304 need_kde_icon_path() |
| 305 { |
| 306 local path |
| 307 path=`readlink -f "$1" 2> /dev/null` # Normalize path |
| 308 DEBUG 2 "need_kde_icon_path $path" |
| 309 if [ -z "$path" ] ; then |
| 310 DEBUG 2 "need_kde_icon_path RETURN 1 (not needed, no xdg icon dir)" |
| 311 return 1; # Not needed |
| 312 fi |
| 313 |
| 314 # if kde-config not found... return 0 |
| 315 kde_icon_dirs=`kde${KDE_SESSION_VERSION}-config --path icon 2> /dev/null |sed
's/:/ /g'` |
| 316 DEBUG 3 "kde_icon_dirs: $kde_icon_dirs" |
| 317 if [ -z "$kde_icon_dirs" ] ; then |
| 318 DEBUG 3 "no result from kde${KDE_SESSION_VERSION}-config --path icon" |
| 319 DEBUG 2 "need_kde_icon_path RETURN 1 (not needed, no kde icon path)" |
| 320 return 1; # Not needed |
| 321 fi |
| 322 needed=0 # Needed |
| 323 for y in $kde_icon_dirs ; do |
| 324 x=`readlink -f "$y"` # Normalize path |
| 325 DEBUG 3 "Normalize $y --> $x" |
| 326 if [ -n "$x" ] ; then |
| 327 if [ "$x" = "$path" ] ; then |
| 328 needed=1 # Not needed |
| 329 fi |
| 330 if [ -w "$x" ] ; then |
| 331 kde_global_prefix="$x" |
| 332 # Take last writable dir |
| 333 fi |
| 334 fi |
| 335 done |
| 336 DEBUG 2 "kde_global_prefix: $kde_global_prefix" |
| 337 [ $needed -eq "1" ] && DEBUG 2 "need_kde_icon_path RETURN $needed (not needed)
" |
| 338 [ $needed -eq "0" ] && DEBUG 2 "need_kde_icon_path RETURN $needed (needed)" |
| 339 return $needed |
| 340 } |
| 341 |
| 342 kde_dir= |
| 343 if [ x"$mode" = x"user" ] ; then |
| 344 xdg_dir="$xdg_base_dir/$xdg_size_name/$context" |
| 345 #KDE 3.x doesn't support XDG_DATA_HOME for icons |
| 346 #Check if xdg_dir prefix is listed by kde-config --path icon |
| 347 #If not, install additional symlink to kdedir |
| 348 if need_kde_icon_path "$xdg_user_prefix" ; then |
| 349 kde_user_dir="$HOME/.kde/share/icons/$theme" |
| 350 kde_dir="$kde_user_dir/$xdg_size_name/$context" |
| 351 fi |
| 352 #Gnome 2.8 supports ~/.icons but not XDG_DATA_HOME |
| 353 if [ -n "$dot_icon_dir" ] ; then |
| 354 if [ -L "$dot_icon_dir" ] ; then |
| 355 # Don't do anything |
| 356 dot_icon_dir= |
| 357 elif [ ! -d "$dot_icon_dir/" ] ; then |
| 358 # Symlink if it doesn't exist |
| 359 eval 'ln -s ".local/share/icons" "$dot_icon_dir"'$xdg_redirect_outpu
t |
| 360 dot_icon_dir= |
| 361 else |
| 362 dot_icon_dir="$dot_icon_dir/$theme/$xdg_size_name/$context" |
| 363 fi |
| 364 fi |
| 365 my_umask=077 |
| 366 else |
| 367 xdg_dir="$xdg_base_dir/$xdg_size_name/$context" |
| 368 #KDE 3.x doesn't support XDG_DATA_DIRS for icons |
| 369 #Check if xdg_dir prefix is listed by kde-config --path icon |
| 370 #If not, install additional symlink to kdedir |
| 371 if need_kde_icon_path "$xdg_global_prefix" ; then |
| 372 kde_global_dir="$kde_global_prefix/$theme" |
| 373 kde_dir="$kde_global_dir/$xdg_size_name/$context" |
| 374 fi |
| 375 my_umask=022 |
| 376 fi |
| 377 # End KDE legacy workaround section |
| 378 |
| 379 # Start GNOME legacy workaround section |
| 380 need_gnome_mime= |
| 381 [ $context = "mimetypes" ] && need_gnome_mime=true |
| 382 # End GNOME legacy workaround section |
| 383 |
| 384 [ -n "$icon_name" ] || icon_name=`basename $icon_file | sed 's/\.[a-z][a-z][a-z]
$//'` |
| 385 |
| 386 if [ "$vendor" = "true" -a "$action" = "install" -a "$context" = "apps" ] ; then |
| 387 check_vendor_prefix "$icon_name" "icon name" |
| 388 fi |
| 389 |
| 390 icon_icon_file=`echo "$icon_file" | sed 's/\.[a-z][a-z][a-z]$/.icon/'` |
| 391 icon_icon_name="$icon_name.icon" |
| 392 |
| 393 DEBUG 1 "$action icon in $xdg_dir" |
| 394 [ $action = "install" -a -f $icon_icon_file ] && DEBUG 1 "install $icon_icon_nam
e meta file in $xdg_dir" |
| 395 [ -n "$kde_dir" ] && DEBUG 1 "$action symlink in $kde_dir (KDE 3.x support)" |
| 396 [ -n "$need_gnome_mime" ] && DEBUG 1 "$action gnome-mime-$icon_name symlink (GNO
ME 2.x support)" |
| 397 [ $action = "install" -a -n "$dot_icon_dir" ] && DEBUG 1 "$action ~/.icons syml
ink (GNOME 2.8 support)" |
| 398 |
| 399 case $action in |
| 400 install) |
| 401 save_umask=`umask` |
| 402 umask $my_umask |
| 403 |
| 404 for icon_dir in $xdg_dir $dot_icon_dir; do |
| 405 mkdir -p $icon_dir |
| 406 eval 'cp "$icon_file" "$icon_dir/$icon_name.$extension"'$xdg_redirec
t_output |
| 407 if [ -f "$icon_icon_file" ] ; then |
| 408 eval 'cp "$icon_icon_file" "$icon_dir/$icon_icon_name"'$xdg_redi
rect_output |
| 409 fi |
| 410 if [ -n "$need_gnome_mime" ] ; then |
| 411 eval 'ln -s "$icon_name.$extension" "$icon_dir/gnome-mime-$icon_
name.$extension"'$xdg_redirect_output |
| 412 fi |
| 413 done |
| 414 if [ -n "$kde_dir" ] ; then |
| 415 mkdir -p $kde_dir |
| 416 eval 'ln -s "$xdg_dir/$icon_name.$extension" "$kde_dir/$icon_name.$e
xtension"'$xdg_redirect_output |
| 417 fi |
| 418 |
| 419 umask $save_umask |
| 420 ;; |
| 421 |
| 422 uninstall) |
| 423 for icon_dir in $xdg_dir $dot_icon_dir; do |
| 424 rm -f "$icon_dir/$icon_name.xpm" "$icon_dir/$icon_name.png" |
| 425 rm -f "$icon_dir/$icon_icon_name" |
| 426 if [ -n "$need_gnome_mime" ] ; then |
| 427 rm -f "$icon_dir/gnome-mime-$icon_name.xpm" |
| 428 rm -f "$icon_dir/gnome-mime-$icon_name.png" |
| 429 fi |
| 430 done |
| 431 if [ -n "$kde_dir" ] ; then |
| 432 rm -f "$kde_dir/$icon_name.xpm" "$kde_dir/$icon_name.png" |
| 433 fi |
| 434 |
| 435 ;; |
| 436 esac |
| 437 |
| 438 if [ x"$update" = x"yes" ] ; then |
| 439 update_icon_database "$xdg_base_dir" |
| 440 if [ -n "$dot_icon_dir" ] ; then |
| 441 if [ -d "$dot_icon_dir/" -a ! -L "$dot_icon_dir" ] ; then |
| 442 update_icon_database $dot_base_dir |
| 443 fi |
| 444 fi |
| 445 fi |
| 446 |
| 447 exit_success |
OLD | NEW |