summaryrefslogtreecommitdiff
path: root/scripts/xdg-mime
blob: 7c56b7d61f3c6bcbdee65b6d036845340ec88ba2 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
#!/bin/sh
#---------------------------------------------
#   xdg-mime
#
#   Utility script to manipulate MIME related information
#   on XDG compliant systems.
#
#   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.
#
#---------------------------------------------

manualpage()
{
cat << _MANUALPAGE
Name

xdg-mime - command line tool for querying information about file type handling
and adding descriptions for new file types

Synopsis

xdg-mime query { filetype | default } ...

xdg-mime default application mimetype(s)

xdg-mime install [--mode mode] [--novendor] mimetypes-file

xdg-mime uninstall [--mode mode] mimetypes-file

xdg-mime { --help | --manual | --version }

Description

The xdg-mime program can be used to query information about file types and to
add descriptions for new file types.

Commands

query

    Returns information related to file types.

    The query option is for use inside a desktop session only. It is not
    recommended to use xdg-mime query as root.

    The following queries are supported:

    query filetype FILE: Returns the file type of FILE in the form of a MIME
    type.

    query default mimetype: Returns the default application that the desktop
    environment uses for opening files of type mimetype. The default
    application is identified by its *.desktop file.

default

    Ask the desktop environment to make application the default application for
    opening files of type mimetype. An application can be made the default for
    several file types by specifying multiple mimetypes.

    application is the desktop file id of the application and has the form
    vendor-name.desktop application must already be installed in the desktop
    menu before it can be made the default handler. The aplication's desktop
    file must list support for all the MIME types that it wishes to be the
    default handler for.

    Requests to make an application a default handler may be subject to system
    policy or approval by the end-user. xdg-mime query can be used to verify
    whether an application is the actual default handler for a specific file
    type.

    The default option is for use inside a desktop session only. It is not
    recommended to use xdg-mime default as root.

install
    Adds the file type descriptions provided in mimetypes-file to the desktop
    environment. mimetypes-file must be a XML file that follows the
    freedesktop.org Shared MIME-info Database specification and that has a
    mime-info element as its document root. For each new file type one or more
    icons with name major-minor must be installed with the xdg-icon-resource
    command in the mimetypes context. For example the application/
    vnd.oasis.opendocument.text filetype requires an icon by the name of
    application-vnd.oasis.opendocument.text to be installed.
uninstall
    Removes the file type descriptions provided in mimetypes-file and
    previously added with xdg-mime install from the desktop environment.
    mimetypes-file must be a XML file that follows the freedesktop.org Shared
    MIME-info Database specification and that has a mime-info element as its
    document root.

Options

--mode mode

    mode can be user or system. In user mode the file is (un)installed for the
    current user only. In system mode the file is (un)installed for all users
    on the system. Usually only root is allowed to install in system mode.

    The default is to use system mode when called by root and to use user mode
    when called by a non-root user.

--novendor

    Normally, xdg-mime checks to ensure that the mimetypes-file to be installed
    has a proper vendor prefix. This option can be used to disable that check.

    A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated
    with a dash ("-"). Companies and organizations are encouraged to use a word
    or phrase, preferably the organizations name, for which they hold a
    trademark as their vendor prefix. The purpose of the vendor prefix is to
    prevent name conflicts.

--help
    Show command synopsis.
--manual
    Show this manualpage.
--version
    Show the xdg-utils version information.

Environment Variables

xdg-mime honours the following environment variables:

XDG_UTILS_DEBUG_LEVEL
    Setting this environment variable to a non-zero numerical value makes
    xdg-mime do more verbose reporting on stderr. Setting a higher value
    increases the verbosity.
XDG_UTILS_INSTALL_MODE
    This environment variable can be used by the user or administrator to
    override the installation mode. Valid values are user and system.

Exit Codes

An exit code of 0 indicates success while a non-zero exit code indicates
failure. The following failure codes can be returned:

1
    Error in command line syntax.
2
    One of the files passed on the command line did not exist.
3
    A required tool could not be found.
4
    The action failed.
5
    No permission to read one of the files passed on the command line.

See Also

xdg-icon-resource(1), xdg-desktop-menu(1)

Examples

xdg-mime query filetype /tmp/foobar.png

Prints the MIME type of the file /tmp/foobar.png, in this case image/png

xdg-mime query default image/png

Prints the .desktop filename of the application which is registered to open PNG
files.

xdg-mime install shinythings-shiny.xml

Adds a file type description for "shiny"-files. "shinythings-" is used as the
vendor prefix. The file type description could look as folows.

shinythings-shiny.xml:

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="text/x-shiny">
    <comment>Shiny new file type</comment>
    <glob pattern="*.shiny"/>
    <glob pattern="*.shi"/>
  </mime-type>
</mime-info>

An icon for this new file type must also be installed, for example with:

xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny

_MANUALPAGE
}

usage()
{
cat << _USAGE
xdg-mime - command line tool for querying information about file type handling
and adding descriptions for new file types

Synopsis

xdg-mime query { filetype | default } ...

xdg-mime default application mimetype(s)

xdg-mime install [--mode mode] [--novendor] mimetypes-file

xdg-mime uninstall [--mode mode] mimetypes-file

xdg-mime { --help | --manual | --version }

_USAGE
}

#@xdg-utils-common@

#----------------------------------------------------------------------------
#   Common utility functions included in all XDG wrapper scripts
#----------------------------------------------------------------------------

DEBUG()
{
  [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  shift
  echo "$@" >&2
}

#-------------------------------------------------------------
# Exit script on successfully completing the desired operation

exit_success()
{
    if [ $# -gt 0 ]; then
        echo "$@"
        echo
    fi

    exit 0
}


#-----------------------------------------
# Exit script on malformed arguments, not enough arguments
# or missing required option.
# prints usage information

exit_failure_syntax()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
        echo "Try 'xdg-mime --help' for more information." >&2
    else
        usage
        echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
    fi

    exit 1
}

#-------------------------------------------------------------
# Exit script on missing file specified on command line

exit_failure_file_missing()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
    fi

    exit 2
}

#-------------------------------------------------------------
# Exit script on failure to locate necessary tool applications

exit_failure_operation_impossible()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
    fi

    exit 3
}

#-------------------------------------------------------------
# Exit script on failure returned by a tool application

exit_failure_operation_failed()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
    fi

    exit 4
}

#------------------------------------------------------------
# Exit script on insufficient permission to read a specified file

exit_failure_file_permission_read()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
    fi

    exit 5
}

#------------------------------------------------------------
# Exit script on insufficient permission to read a specified file

exit_failure_file_permission_write()
{
    if [ $# -gt 0 ]; then
        echo "xdg-mime: $@" >&2
    fi

    exit 6
}

check_input_file()
{
    if [ ! -e "$1" ]; then
        exit_failure_file_missing "file '$1' does not exist"
    fi
    if [ ! -r "$1" ]; then
        exit_failure_file_permission_read "no permission to read file '$1'"
    fi
}

check_vendor_prefix()
{
    file_label="$2"
    [ -n "$file_label" ] || file_label="filename"
    file=`basename "$1"`
    case "$file" in
       [a-zA-Z]*-*)
         return
         ;;
    esac

    echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&2
    echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
    echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
    echo "Use --novendor to override or 'xdg-mime --manual' for additional info." >&2
    exit 1
}

check_output_file()
{
    # if the file exists, check if it is writeable
    # if it does not exists, check if we are allowed to write on the directory
    if [ -e "$1" ]; then
        if [ ! -w "$1" ]; then
            exit_failure_file_permission_write "no permission to write to file '$1'"
        fi
    else
        DIR=`dirname "$1"`
        if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
            exit_failure_file_permission_write "no permission to create file '$1'"
        fi
    fi
}

#----------------------------------------
# Checks for shared commands, e.g. --help

check_common_commands()
{
    while [ $# -gt 0 ] ; do
        parm="$1"
        shift

        case "$parm" in
            --help)
            usage
            echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
            exit_success
            ;;

            --manual)
            manualpage
            exit_success
            ;;

            --version)
            echo "xdg-mime 1.0.1"
            exit_success
            ;;
        esac
    done
}

check_common_commands "$@"

[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
    # Be silent
    xdg_redirect_output=" > /dev/null 2> /dev/null"
else
    # All output to stderr
    xdg_redirect_output=" >&2"
fi

#--------------------------------------
# Checks for known desktop environments
# set variable DE to the desktop environments name, lowercase

detectDE()
{
    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
    elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
    fi
}

#----------------------------------------------------------------------------
# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
# It also always returns 1 in KDE 3.4 and earlier
# Simply return 0 in such case

kfmclient_fix_exit_code()
{
    version=`kde-config --version 2>/dev/null | grep KDE`
    major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
    minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
    release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
    test "$major" -gt 3 && return $1
    test "$minor" -gt 5 && return $1
    test "$release" -gt 4 && return $1
    return 0
}

update_mime_database()
{
   if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
      detectDE
      if [ x"$DE" = x"kde" ] ; then
         DEBUG 1 "Running kbuildsycoca"
         eval 'kbuildsycoca'$xdg_redirect_output
      fi
   fi
   for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
      if [ -x $x/update-mime-database ] ; then
         DEBUG 1 "Running $x/update-mime-database $1"
         eval '$x/update-mime-database $1'$xdg_redirect_output
         return
      fi
   done
}

info_kde()
{
    DEBUG 1 "Running kfile \"$1\""
    kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1

    exit_success
}

info_gnome()
{
    file=`readlink -f "$1"` # Normalize path
    DEBUG 1 "Running gnomevfs-info \"$file\""
    gnomevfs-info "$file" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "//

    exit_success
}

info_generic()
{
    DEBUG 1 "Running /usr/bin/file -i \"$1\"" 
    /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//

    exit_success
}

make_default_kde()
{
    # $1 is vendor-name.desktop
    # $2 is mime/type 
    # Add to $KDE_HOME/share/config/profilerc:
    # [$2 - 1]
    # Application=$1
    #
    # Remove all [$2 - *] sections, or even better,
    # renumber [$2 - *] sections and remove duplicate
    
    default_file="$HOME/.kde/share/config/profilerc"
    DEBUG 2 "make_default_kde $1 $2"
    DEBUG 1 "Updating $default_file"
    mkdir -p "$HOME/.kde/share/config"
    [ -f $default_file ] || touch $default_file
    awk -v application="$1" -v mimetype="$2" '
    BEGIN {
        header_start="[" mimetype " - "
        supress=0
    }
    {  
        if (index($0, header_start) == 1 )
            supress=1
        else
            if (/^\[/) { supress=0 }

        if (!supress) {
            print $0
        }
    }
    END {
        print "" 
        print "[" mimetype " - 1]"
        print "Application=" application
        print "AllowAsDefault=true"
        print "GenericServiceType=Application"
        print "Preference=1"
        print "ServiceType=" mimetype
    }
' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
}

make_default_generic()
{
    # $1 is vendor-name.desktop
    # $2 is mime/type 
    # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
    xdg_user_dir="$XDG_DATA_HOME"
    [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
    default_file="$xdg_user_dir/applications/defaults.list"
    DEBUG 2 "make_default_generic $1 $2"
    DEBUG 1 "Updating $default_file"
    grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
    if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
       echo "[Default Applications]" >> ${default_file}.new
    fi
    echo $2=$1 >> ${default_file}.new
    mv ${default_file}.new $default_file
}

defapp_generic()
{
    MIME="$1"
    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_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
       DEBUG 2 "Checking $x/applications/defaults.list"
       trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
       if [ -n "$trader_result" ] ; then
          echo $trader_result
          exit_success
       fi
    done
    exit_success
}

defapp_kde()
{
    MIME="$1"
    ktradertest=`which ktradertest 2> /dev/null`
    if [ -n "$ktradertest" ] ; then
        DEBUG 1 "Running ktradertest \"$MIME\" Application"
        trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep DesktopEntryPath \
            | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
        if [ -n "$trader_result" ] ; then
            basename "$trader_result"
            exit_success
        else
            exit_failure_operation_failed
        fi
    else
        defapp_generic "$1"
    fi
}

[ x"$1" != x"" ] || exit_failure_syntax

mode=
action=
filename=
mimetype=

case $1 in
  install)
    action=install
    ;;

  uninstall)
    action=uninstall
    ;;

  query)
    shift

    if [ -z "$1" ] ; then 
        exit_failure_syntax "query type argument missing"
    fi
    
    case $1 in
      filetype)
        action=info

        filename="$2"
        if [ -z "$filename" ] ; then 
            exit_failure_syntax "FILE argument missing"
        fi
        case $filename in
          -*)
            exit_failure_syntax "unexpected option '$filename'"
            ;;
        esac
        check_input_file "$filename"
        ;;

      default)
        action=defapp
        mimetype="$2"
        if [ -z "$mimetype" ] ; then 
            exit_failure_syntax "mimetype argument missing"
        fi
        case $mimetype in
          -*)
            exit_failure_syntax "unexpected option '$mimetype'"
            ;;

          */*)
            # Ok
            ;;

          *)
            exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
            ;;
        esac
        ;;

      *)
      exit_failure_syntax "unknown query type '$1'"
      ;;
    esac
    ;;

  default)
    action=makedefault
    shift

    if [ -z "$1" ] ; then 
        exit_failure_syntax "application argument missing"
    fi
    case $1 in
      -*)
        exit_failure_syntax "unexpected option '$1'"
        ;;

      *.desktop)
        filename="$1"
        ;;

      *)
        exit_failure_syntax "malformed argument '$1', expected *.desktop"
        ;;
    esac
    ;;

  *)
  exit_failure_syntax "unknown command '$1'"
  ;;
esac

shift


if [ "$action" = "makedefault" ]; then
    if [ -z "$1" ] ; then 
        exit_failure_syntax "mimetype argument missing"
    fi

    while [ $# -gt 0 ] ; do
        case $1 in
          -*)
            exit_failure_syntax "unexpected option '$1'"
            ;;
        esac
        mimetype="$1"
        shift
        
        make_default_kde "$filename" "$mimetype"
        make_default_generic "$filename" "$mimetype"
    done
    exit_success
fi

if [ "$action" = "info" ]; then
    detectDE

    if [ x"$DE" = x"" ]; then
        if [ -x /usr/bin/file ]; then
            DE=generic
        fi
    fi

    case "$DE" in
        kde)
        info_kde "$filename"
        ;;

        gnome)
        info_gnome "$filename"
        ;;

        generic)
        info_generic "$filename"
        ;;
    esac
    exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
fi

if [ "$action" = "defapp" ]; then
    detectDE

    case "$DE" in
        kde)
        defapp_kde "$mimetype"
        ;;

        *)
        defapp_generic "$mimetype"
        ;;
    esac
    exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
fi

vendor=true
while [ $# -gt 0 ] ; do
    parm="$1"
    shift

    case $parm in
      --mode)
        if [ -z "$1" ] ; then
            exit_failure_syntax "mode argument missing for --mode"
        fi
        case "$1" in
          user)
            mode="user"
            ;;

          system)
            mode="system"
            ;;
            
          *)
            exit_failure_syntax "unknown mode '$1'"
            ;;
        esac
        shift
        ;;

      --novendor)
        vendor=false
        ;;

      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      *)
        if [ -n "$filename" ] ; then
            exit_failure_syntax "unexpected argument '$parm'"
        fi

        filename="$parm"
        check_input_file "$filename"
        ;;
    esac
done

if [ -z "$action" ] ; then
    exit_failure_syntax "command argument missing"
fi

if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
    if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
        mode="system"
    elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
        mode="user"
    fi
fi

if [ -z "$mode" ] ; then
    if [ `whoami` = "root" ] ; then
        mode="system"
    else
        mode="user"
    fi
fi

if [ -z "$filename" ] ; then
    exit_failure_syntax "mimetypes-file argument missing"
fi

if [ "$vendor" =  "true" -a "$action" = "install" ] ; then
    check_vendor_prefix "$filename"
fi

xdg_base_dir=
xdg_dir_name=mime/packages/

xdg_user_dir="$XDG_DATA_HOME"
[ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
[ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
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
        [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
        xdg_global_dir="$x/$xdg_dir_name"
        break
    fi
done
[ -w $xdg_global_dir ] || xdg_global_dir=
DEBUG 3 "xdg_user_dir: $xdg_user_dir"
DEBUG 3 "xdg_global_dir: $xdg_global_dir"

# Find KDE3 mimelnk directory
kde_user_dir=
kde_global_dir=
kde_global_dirs=`kde-config --path mime 2> /dev/null`
DEBUG 3 "kde_global_dirs: $kde_global_dirs"
first=
for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
    if [ -z "$first" ] ; then
        first=false
        kde_user_dir="$x"
    elif [ -w $x ] ; then
        kde_global_dir="$x"
    fi
done
DEBUG 3 "kde_user_dir: $kde_user_dir"
DEBUG 3 "kde_global_dir: $kde_global_dir"

# TODO: Gnome legacy support
# See http://forums.fedoraforum.org/showthread.php?t=26875
gnome_user_dir="$HOME/.gnome/apps"
gnome_global_dir=/usr/share/gnome/apps
[ -w $gnome_global_dir ] || gnome_global_dir=
DEBUG 3 "gnome_user_dir: $gnome_user_dir"
DEBUG 3 "gnome_global_dir: $gnome_global_dir"

if [ x"$mode" = x"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 [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
        exit_failure_operation_impossible "No writable system mimetype directory found."
    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 "$filename"`
#[ -z $vendor ] || basefile="$vendor-$basefile"

mimetypes=
if [ -n "$kde_dir" ] ; then
    DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"

    mimetypes=`awk < "$filename" '
# Strip XML comments
BEGIN {
 supress=0
}
{
 do 
    if (supress) {
       if (match($0,/-->/)) {
           $0=substr($0,RSTART+RLENGTH)
           supress=0
       }
       else {
           break
       }
    }
    else {
       if (match($0,/<!--/)) {
           if (RSTART>1) print substr($0,0,RSTART)
           $0=substr($0,RSTART+RLENGTH)
           supress=1
       }
       else {
           if ($0) print $0
           break
       }
    }
 while(1)
}
' | awk '
# List MIME types listed in <mime-type> tags
BEGIN {
  RS="<"
}
/^mime-info/, /^\/mime-info/ {
  if (match($0,/^mime-type/)) {
    if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
      print substr($0,RSTART+6,RLENGTH-6)
    }
  }
}'`
fi  

DEBUG 1 "$action mimetype in $xdg_dir"

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

        for x in $xdg_dir ; do
            mkdir -p $x
            eval 'cp $filename $x/$basefile'$xdg_redirect_output
        done

        if [ -n "$mimetypes" ] ; then
            # No quotes around $mimetypes
            for x in $mimetypes ; do 
                DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
                mkdir -p `dirname $kde_dir/$x.desktop`
                awk < "$filename" '
# Strip XML comments
BEGIN {
 supress=0
}
{
 do 
    if (supress) {
       if (match($0,/-->/)) {
           $0=substr($0,RSTART+RLENGTH)
           supress=0
       }
       else {
           break
       }
    }
    else {
       if (match($0,/<!--/)) {
           if (RSTART>1) print substr($0,0,RSTART)
           $0=substr($0,RSTART+RLENGTH)
           supress=1
       }
       else {
           if ($0) print $0
           break
       }
    }
 while(1)
}
' | awk > $kde_dir/$x.desktop '
# Extract mimetype $x from the XML file $filename
# Note that bash requires us to escape a single quote as '"'"' 
BEGIN {
  the_type=ARGV[1]
  the_source=ARGV[2]
  ARGC=1
  RS="<"
  found=0
  glob_patterns=""
}
/^mime-info/, /^\/mime-info/ {
  if (match($0,/^mime-type/)) {
    if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
      if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
        found=1
        print "[Desktop Entry]"
        print "# Installed by xdg-mime from " the_source
        print "Type=MimeType"
        print "MimeType=" the_type
        the_icon=the_type
        sub("/", "-", the_icon)
        print "Icon=" the_icon 
      }
    }
  }
  else if (found) {
    if (match($0,/^\/mime-type/)) {
      if (glob_patterns)
         print "Patterns=" glob_patterns
      exit 0
    }

    if (match($0,/^sub-class-of/)) {
      if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {    
        print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
      }
      else {
        print "Error: '"'"'type'"'"' argument missing in " RS $0
        exit 1 
      }
    }
    if (match($0,/^glob/)) {
      if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {    
        glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
      }
      else {
        print "Error: '"'"'pattern'"'"' argument missing in " RS $0
        exit 1 
      }
    }
    if (match($0,/^comment/)) {
      if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {    
        lang=substr($0,RSTART+10,RLENGTH-10)
      }
      else {
        lang=""
      }
      if (match($0,/>/)) {
        comment=substr($0,RSTART+1)
        sub("&lt;", "<", comment)
        sub("&gt;", ">", comment)
        sub("&amp;", "\\&", comment)
        if (lang)
           print "Comment[" lang "]=" comment
        else
           print "Comment=" comment
      }
    }
  }
}
END {
  if (!found) {
    print "Error: Mimetype '"'"'" the_type "'"'"' not found"
    exit 1
  }
}
' $x $basefile
                if [ "$?" = "1" ] ; then
                    grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
                    rm $kde_dir/$x.desktop
                    exit 1  
                fi
            done
        fi

        umask $save_umask
        ;;

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

        # No quotes around $mimetypes
        for x in $mimetypes ; do 
            if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop &>/dev/null ; then
                DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
                rm -f $kde_dir/$x.desktop
            fi
        done
        ;;
esac

update_mime_database $xdg_base_dir

exit_success