1#!/bin/sh
2#---------------------------------------------
3#   xdg-mime
4#
5#   Utility script to manipulate MIME related information
6#   on XDG compliant systems.
7#
8#   Refer to the usage() function below for usage.
9#
10#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
11#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
12#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
14#
15#   LICENSE:
16#
17#---------------------------------------------
18
19manualpage()
20{
21cat << _MANUALPAGE
22_MANUALPAGE
23}
24
25usage()
26{
27cat << _USAGE
28_USAGE
29}
30
31#@xdg-utils-common@
32
33update_mime_database()
34{
35   if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
36      detectDE
37      if [ x"$DE" = x"kde" ] ; then
38         DEBUG 1 "Running kbuildsycoca"
39         if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
40             eval 'kbuildsycoca4'$xdg_redirect_output
41         else
42             eval 'kbuildsycoca'$xdg_redirect_output
43         fi
44      fi
45   fi
46   for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
47      if [ -x $x/update-mime-database ] ; then
48         DEBUG 1 "Running $x/update-mime-database $1"
49         eval '$x/update-mime-database $1'$xdg_redirect_output
50         return
51      fi
52   done
53}
54
55info_kde()
56{
57    if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
58        DEBUG 1 "Running kmimetypefinder \"$1\""
59        kmimetypefinder "$1" 2>/dev/null | head -n 1
60    else
61        DEBUG 1 "Running kfile \"$1\""
62        kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
63    fi
64
65    if [ $? -eq 0 ]; then
66        exit_success
67    else
68        exit_failure_operation_failed
69    fi
70}
71
72info_gnome()
73{
74    if gvfs-info --help 2>/dev/null 1>&2; then
75        DEBUG 1 "Running gvfs-info \"$1\""
76        gvfs-info "$1" 2> /dev/null | grep standard::content-type | cut -d' ' -f4
77    elif gnomevfs-info --help 2>/dev/null 1>&2; then
78       DEBUG 1 "Running gnomevfs-info \"$1\""
79       gnomevfs-info --slow-mime "$1" 2> /dev/null | grep "^MIME" | cut -d ":" -f 2 | sed s/"^ "//
80    else
81       # according to https://bugs.freedesktop.org/show_bug.cgi?id=33094#c5
82       # neither gvfs-info or gnomevfs-info are present in a default Ubuntu Natty
83       # install, so fallback to info_generic
84       info_generic "$1"
85    fi
86
87    if [ $? -eq 0 ]; then
88        exit_success
89    else
90        exit_failure_operation_failed
91    fi
92}
93
94info_generic()
95{
96    if mimetype --version >/dev/null 2>&1; then
97        DEBUG 1 "Running mimetype -b \"$1\""
98        mimetype -b "$1"
99    else
100        DEBUG 1 "Running file --mime-type \"$1\""
101        /usr/bin/file --mime-type "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
102    fi
103
104    if [ $? -eq 0 ]; then
105        exit_success
106    else
107        exit_failure_operation_failed
108    fi
109}
110
111make_default_kde()
112{
113    # $1 is vendor-name.desktop
114    # $2 is mime/type
115    #
116    # On KDE 3, add to $KDE_CONFIG_PATH/profilerc:
117    # [$2 - 1]
118    # Application=$1
119    #
120    # Remove all [$2 - *] sections, or even better,
121    # renumber [$2 - *] sections and remove duplicate
122    #
123    # On KDE 4, add $2=$1 to $XDG_DATA_APPS/mimeapps.list
124    #
125    # Example file:
126    #
127    # [Added Associations]
128    # text/plain=kde4-kate.desktop;kde4-kwrite.desktop;
129    #
130    # [Removed Associations]
131    # text/plain=gnome-gedit.desktop;gnu-emacs.desktop;
132    vendor="$1"
133    mimetype="$2"
134    if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
135        default_dir=`kde4-config --path xdgdata-apps 2> /dev/null | cut -d ':' -f 1`
136        default_file="$default_dir/mimeapps.list"
137    else
138        default_dir=`kde-config --path config 2> /dev/null | cut -d ':' -f 1`
139        default_file="$default_dir/profilerc"
140    fi
141    if [ -z "$default_dir" ]; then
142        DEBUG 2 "make_default_kde: No kde runtime detected"
143        return
144    fi
145    DEBUG 2 "make_default_kde $vendor $mimetype"
146    DEBUG 1 "Updating $default_file"
147    mkdir -p "$default_dir"
148    [ -f "$default_file" ] || touch "$default_file"
149    if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
150        [ -f "$default_file" ] || touch "$default_file"
151        awk -v application="$vendor" -v mimetype="$mimetype" '
152    BEGIN {
153        prefix=mimetype "="
154        associations=0
155        found=0
156        blanks=0
157    }
158    {
159        suppress=0
160        if (index($0, "[Added Associations]") == 1) {
161            associations=1
162        } else if (index($0, "[") == 1) {
163            if (associations && !found) {
164                print prefix application
165                found=1
166            }
167            associations=0
168        } else if ($0 == "") {
169            blanks++
170            suppress=1
171        } else if (associations && index($0, prefix) == 1) {
172            value=substr($0, length(prefix) + 1, length)
173            split(value, apps, ";")
174            value=application ";"
175            count=0
176            for (i in apps) {
177              count++
178            }
179            for (i=0; i < count; i++) {
180                if (apps[i] != application && apps[i] != "") {
181                    value=value apps[i] ";"
182                }
183            }
184            $0=prefix value
185            found=1
186        }
187        if (!suppress) {
188            while (blanks > 0) {
189                print ""
190                blanks--
191            }
192            print $0
193        }
194    }
195    END {
196        if (!found) {
197            if (!associations) {
198                print "[Added Associations]"
199            }
200            print prefix application
201        }
202        while (blanks > 0) {
203            print ""
204            blanks--
205        }
206    }
207' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
208        eval 'kbuildsycoca4'$xdg_redirect_output
209    else
210        awk -v application="$vendor" -v mimetype="$mimetype" '
211    BEGIN {
212        header_start="[" mimetype " - "
213        suppress=0
214    }
215    {
216        if (index($0, header_start) == 1 )
217            suppress=1
218        else
219            if (/^\[/) { suppress=0 }
220
221        if (!suppress) {
222            print $0
223        }
224    }
225    END {
226        print ""
227        print "[" mimetype " - 1]"
228        print "Application=" application
229        print "AllowAsDefault=true"
230        print "GenericServiceType=Application"
231        print "Preference=1"
232        print "ServiceType=" mimetype
233    }
234' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
235    fi
236}
237
238make_default_generic()
239{
240    # $1 is vendor-name.desktop
241    # $2 is mime/type
242    # Add $2=$1 to XDG_DATA_HOME/applications/mimeapps.list
243    xdg_user_dir="$XDG_DATA_HOME"
244    [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
245    default_file="$xdg_user_dir/applications/mimeapps.list"
246    DEBUG 2 "make_default_generic $1 $2"
247    DEBUG 1 "Updating $default_file"
248    [ -f "$default_file" ] || touch "$default_file"
249    awk -v mimetype="$2" -v application="$1" '
250    BEGIN {
251        prefix=mimetype "="
252        indefault=0
253        added=0
254        blanks=0
255        found=0
256    }
257    {
258        suppress=0
259        if (index($0, "[Default Applications]") == 1) {
260            indefault=1
261            found=1
262        } else if (index($0, "[") == 1) {
263            if (!added && indefault) {
264                print prefix application
265                added=1
266            }
267            indefault=0
268        } else if ($0 == "") {
269            suppress=1
270            blanks++
271        } else if (indefault && !added && index($0, prefix) == 1) {
272                $0=prefix application
273                added=1
274        }
275        if (!suppress) {
276            while (blanks > 0) {
277                print ""
278                blanks--
279            }
280            print $0
281        }
282    }
283    END {
284        if (!added) {
285            if (!found) {
286                print ""
287                print "[Default Applications]"
288            }
289            print prefix application
290        }
291        while (blanks > 0) {
292            print ""
293            blanks--
294        }
295    }
296' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
297}
298
299defapp_generic()
300{
301    MIME="$1"
302    xdg_user_dir="$XDG_DATA_HOME"
303    [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
304    xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
305    xdg_system_dirs="$XDG_DATA_DIRS"
306    [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
307
308    for x in `echo "$xdg_user_dir" | sed 's/:/ /g'`; do
309        mimeapps_list="$x/applications/mimeapps.list"
310        if [ -f "$mimeapps_list" ] ; then
311            DEBUG 2 "Checking $mimeapps_list"
312            trader_result=`awk -v mimetype="$MIME" '
313    BEGIN {
314        prefix=mimetype "="
315        indefault=0
316        found=0
317    }
318    {
319        if (index($0, "[Default Applications]") == 1) {
320            indefault=1
321        } else if (index($0, "[") == 1) {
322            indefault=0
323        } else if (!found && indefault && index($0, prefix) == 1) {
324            print substr($0, length(prefix) +1, length)
325            found=1
326        }
327    }
328' $mimeapps_list`
329            if [ -n "$trader_result" ] ; then
330                echo $trader_result
331                exit_success
332            fi
333        fi
334    done
335
336    for x in `echo "$xdg_system_dirs" | sed 's/:/ /g'`; do
337       DEBUG 2 "Checking $x/applications/defaults.list"
338       trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
339       if [ -n "$trader_result" ] ; then
340          echo $trader_result
341          exit_success
342       fi
343    done
344    exit_success
345}
346
347defapp_kde()
348{
349    MIME="$1"
350    if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
351        KTRADER=`which ktraderclient 2> /dev/null`
352        MIMETYPE="--mimetype"
353        SERVICETYPE="--servicetype"
354    else
355        KTRADER=`which ktradertest 2> /dev/null`
356    fi
357    if [ -n "$KTRADER" ] ; then
358        DEBUG 1 "Running KDE trader query \"$MIME\" mimetype and \"Application\" servicetype"
359        trader_result=`$KTRADER $MIMETYPE "$MIME" $SERVICETYPE Application 2>/dev/null \
360            | grep DesktopEntryPath | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
361        if [ -n "$trader_result" ] ; then
362            basename "$trader_result"
363            exit_success
364        else
365            exit_failure_operation_failed
366        fi
367    else
368        defapp_generic "$1"
369    fi
370}
371
372[ x"$1" != x"" ] || exit_failure_syntax
373
374mode=
375action=
376filename=
377mimetype=
378
379case $1 in
380  install)
381    action=install
382    ;;
383
384  uninstall)
385    action=uninstall
386    ;;
387
388  query)
389    shift
390
391    if [ -z "$1" ] ; then
392        exit_failure_syntax "query type argument missing"
393    fi
394
395    case $1 in
396      filetype)
397        action=info
398
399        filename="$2"
400        if [ -z "$filename" ] ; then
401            exit_failure_syntax "FILE argument missing"
402        fi
403        case $filename in
404          -*)
405            exit_failure_syntax "unexpected option '$filename'"
406            ;;
407        esac
408        check_input_file "$filename"
409        filename=`readlink -f -- "$filename"`
410        ;;
411
412      default)
413        action=defapp
414        mimetype="$2"
415        if [ -z "$mimetype" ] ; then
416            exit_failure_syntax "mimetype argument missing"
417        fi
418        case $mimetype in
419          -*)
420            exit_failure_syntax "unexpected option '$mimetype'"
421            ;;
422
423          */*)
424            # Ok
425            ;;
426
427          *)
428            exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
429            ;;
430        esac
431        ;;
432
433      *)
434      exit_failure_syntax "unknown query type '$1'"
435      ;;
436    esac
437    ;;
438
439  default)
440    action=makedefault
441    shift
442
443    if [ -z "$1" ] ; then
444        exit_failure_syntax "application argument missing"
445    fi
446    case $1 in
447      -*)
448        exit_failure_syntax "unexpected option '$1'"
449        ;;
450
451      *.desktop)
452        filename="$1"
453        ;;
454
455      *)
456        exit_failure_syntax "malformed argument '$1', expected *.desktop"
457        ;;
458    esac
459    ;;
460
461  *)
462  exit_failure_syntax "unknown command '$1'"
463  ;;
464esac
465
466shift
467
468
469if [ "$action" = "makedefault" ]; then
470    if [ -z "$1" ] ; then
471        exit_failure_syntax "mimetype argument missing"
472    fi
473
474    while [ $# -gt 0 ] ; do
475        case $1 in
476          -*)
477            exit_failure_syntax "unexpected option '$1'"
478            ;;
479        esac
480        mimetype="$1"
481        shift
482
483        make_default_kde "$filename" "$mimetype"
484        make_default_generic "$filename" "$mimetype"
485    done
486    exit_success
487fi
488
489if [ "$action" = "info" ]; then
490    detectDE
491
492    if [ x"$DE" = x"" ]; then
493        if [ -x /usr/bin/file ]; then
494            DE=generic
495        fi
496    fi
497
498    case "$DE" in
499        kde)
500        info_kde "$filename"
501        ;;
502
503        gnome*)
504        info_gnome "$filename"
505        ;;
506
507        *)
508        info_generic "$filename"
509        ;;
510    esac
511    exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
512fi
513
514if [ "$action" = "defapp" ]; then
515    detectDE
516
517    case "$DE" in
518        kde)
519        defapp_kde "$mimetype"
520        ;;
521
522        *)
523        defapp_generic "$mimetype"
524        ;;
525    esac
526    exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
527fi
528
529vendor=true
530while [ $# -gt 0 ] ; do
531    parm="$1"
532    shift
533
534    case $parm in
535      --mode)
536        if [ -z "$1" ] ; then
537            exit_failure_syntax "mode argument missing for --mode"
538        fi
539        case "$1" in
540          user)
541            mode="user"
542            ;;
543
544          system)
545            mode="system"
546            ;;
547
548          *)
549            exit_failure_syntax "unknown mode '$1'"
550            ;;
551        esac
552        shift
553        ;;
554
555      --novendor)
556        vendor=false
557        ;;
558
559      -*)
560        exit_failure_syntax "unexpected option '$parm'"
561        ;;
562
563      *)
564        if [ -n "$filename" ] ; then
565            exit_failure_syntax "unexpected argument '$parm'"
566        fi
567
568        filename="$parm"
569        check_input_file "$filename"
570        ;;
571    esac
572done
573
574if [ -z "$action" ] ; then
575    exit_failure_syntax "command argument missing"
576fi
577
578if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
579    if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
580        mode="system"
581    elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
582        mode="user"
583    fi
584fi
585
586if [ -z "$mode" ] ; then
587    if [ `whoami` = "root" ] ; then
588        mode="system"
589    else
590        mode="user"
591    fi
592fi
593
594if [ -z "$filename" ] ; then
595    exit_failure_syntax "mimetypes-file argument missing"
596fi
597
598if [ "$vendor" =  "true" -a "$action" = "install" ] ; then
599    check_vendor_prefix "$filename"
600fi
601
602xdg_base_dir=
603xdg_dir_name=mime/packages/
604
605xdg_user_dir="$XDG_DATA_HOME"
606[ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
607[ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
608xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
609
610xdg_system_dirs="$XDG_DATA_DIRS"
611[ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
612for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
613    if [ -w $x/$xdg_dir_name ] ; then
614        [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
615        xdg_global_dir="$x/$xdg_dir_name"
616        break
617    fi
618done
619[ -w $xdg_global_dir ] || xdg_global_dir=
620DEBUG 3 "xdg_user_dir: $xdg_user_dir"
621DEBUG 3 "xdg_global_dir: $xdg_global_dir"
622
623# Find KDE3 mimelnk directory
624kde_user_dir=
625kde_global_dir=
626kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
627DEBUG 3 "kde_global_dirs: $kde_global_dirs"
628first=
629for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
630    if [ -z "$first" ] ; then
631        first=false
632        kde_user_dir="$x"
633    elif [ -w $x ] ; then
634        kde_global_dir="$x"
635    fi
636done
637DEBUG 3 "kde_user_dir: $kde_user_dir"
638DEBUG 3 "kde_global_dir: $kde_global_dir"
639
640# TODO: Gnome legacy support
641# See http://forums.fedoraforum.org/showthread.php?t=26875
642gnome_user_dir="$HOME/.gnome/apps"
643gnome_global_dir=/usr/share/gnome/apps
644[ -w $gnome_global_dir ] || gnome_global_dir=
645DEBUG 3 "gnome_user_dir: $gnome_user_dir"
646DEBUG 3 "gnome_global_dir: $gnome_global_dir"
647
648if [ x"$mode" = x"user" ] ; then
649    xdg_dir="$xdg_user_dir"
650    kde_dir="$kde_user_dir"
651    gnome_dir="$gnome_user_dir"
652    my_umask=077
653else
654    xdg_dir="$xdg_global_dir"
655    kde_dir="$kde_global_dir"
656    gnome_dir="$gnome_global_dir"
657    my_umask=022
658    if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
659        exit_failure_operation_impossible "No writable system mimetype directory found."
660    fi
661fi
662
663# echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
664# echo "[kde|$kde_user_dir|$kde_global_dir]"
665# echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
666# echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
667
668basefile=`basename "$filename"`
669#[ -z $vendor ] || basefile="$vendor-$basefile"
670
671mimetypes=
672if [ -n "$kde_dir" ] ; then
673    DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
674
675    mimetypes=`awk < "$filename" '
676# Strip XML comments
677BEGIN {
678 suppress=0
679}
680{
681 do
682    if (suppress) {
683       if (match($0,/-->/)) {
684           $0=substr($0,RSTART+RLENGTH)
685           suppress=0
686       }
687       else {
688           break
689       }
690    }
691    else {
692       if (match($0,/<!--/)) {
693           if (RSTART>1) print substr($0,0,RSTART)
694           $0=substr($0,RSTART+RLENGTH)
695           suppress=1
696       }
697       else {
698           if ($0) print $0
699           break
700       }
701    }
702 while(1)
703}
704' | awk '
705# List MIME types listed in <mime-type> tags
706BEGIN {
707  RS="<"
708}
709/^mime-info/, /^\/mime-info/ {
710  if (match($0,/^mime-type/)) {
711    if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
712      print substr($0,RSTART+6,RLENGTH-6)
713    }
714  }
715}'`
716fi
717
718DEBUG 1 "$action mimetype in $xdg_dir"
719
720case $action in
721    install)
722        save_umask=`umask`
723        umask $my_umask
724
725        for x in $xdg_dir ; do
726            mkdir -p $x
727            eval 'cp $filename $x/$basefile'$xdg_redirect_output
728        done
729
730        if [ -n "$mimetypes" ] ; then
731            # No quotes around $mimetypes
732            for x in $mimetypes ; do
733                DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
734                mkdir -p `dirname $kde_dir/$x.desktop`
735                awk < "$filename" '
736# Strip XML comments
737BEGIN {
738 suppress=0
739}
740{
741 do
742    if (suppress) {
743       if (match($0,/-->/)) {
744           $0=substr($0,RSTART+RLENGTH)
745           suppress=0
746       }
747       else {
748           break
749       }
750    }
751    else {
752       if (match($0,/<!--/)) {
753           if (RSTART>1) print substr($0,0,RSTART)
754           $0=substr($0,RSTART+RLENGTH)
755           suppress=1
756       }
757       else {
758           if ($0) print $0
759           break
760       }
761    }
762 while(1)
763}
764' | awk > $kde_dir/$x.desktop '
765# Extract mimetype $x from the XML file $filename
766# Note that bash requires us to escape a single quote as '"'"'
767BEGIN {
768  the_type=ARGV[1]
769  the_source=ARGV[2]
770  ARGC=1
771  RS="<"
772  found=0
773  glob_patterns=""
774}
775/^mime-info/, /^\/mime-info/ {
776  if (match($0,/^mime-type/)) {
777    if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
778      if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
779        found=1
780        print "[Desktop Entry]"
781        print "# Installed by xdg-mime from " the_source
782        print "Type=MimeType"
783        print "MimeType=" the_type
784        the_icon=the_type
785        sub("/", "-", the_icon)
786        print "Icon=" the_icon
787      }
788    }
789  }
790  else if (found) {
791    if (match($0,/^\/mime-type/)) {
792      if (glob_patterns)
793         print "Patterns=" glob_patterns
794      exit 0
795    }
796
797    if (match($0,/^sub-class-of/)) {
798      if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
799        print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
800      }
801      else {
802        print "Error: '"'"'type'"'"' argument missing in " RS $0
803        exit 1
804      }
805    }
806    if (match($0,/^glob/)) {
807      if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
808        glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
809      }
810      else {
811        print "Error: '"'"'pattern'"'"' argument missing in " RS $0
812        exit 1
813      }
814    }
815    if (match($0,/^comment/)) {
816      if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
817        lang=substr($0,RSTART+10,RLENGTH-10)
818      }
819      else {
820        lang=""
821      }
822      if (match($0,/>/)) {
823        comment=substr($0,RSTART+1)
824        sub("&lt;", "<", comment)
825        sub("&gt;", ">", comment)
826        sub("&amp;", "\\&", comment)
827        if (lang)
828           print "Comment[" lang "]=" comment
829        else
830           print "Comment=" comment
831      }
832    }
833  }
834}
835END {
836  if (!found) {
837    print "Error: Mimetype '"'"'" the_type "'"'"' not found"
838    exit 1
839  }
840}
841' $x $basefile
842                if [ "$?" = "1" ] ; then
843                    grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
844                    rm $kde_dir/$x.desktop
845                    exit 1
846                fi
847            done
848        fi
849
850        umask $save_umask
851        ;;
852
853    uninstall)
854        for x in $xdg_dir ; do
855            rm -f $x/$basefile
856        done
857
858        # No quotes around $mimetypes
859        for x in $mimetypes ; do
860            if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop >/dev/null 2>&1; then
861                DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
862                rm -f $kde_dir/$x.desktop
863            fi
864        done
865        ;;
866esac
867
868update_mime_database $xdg_base_dir
869
870exit_success
871
872