#!/usr/bin/env python
# -*- coding: utf-8 -*-

import glob
import subprocess as sp

# change the order of item in these list will change the default applications order.

pdfs=["okular", "evince"]
editors = ["gvim", "gedit"]
browsers = ["firefox", "chromium-browser"]

def log(program, mime):
    print("mime:{m} default handle by {p}".format(m=mime, p=program))

def setDefault(proglist, mime):
    for p in proglist:
        prog = p + ".desktop"
        if len(glob.glob("/usr/share/applications/**/" + prog)) or len(glob.glob("/usr/share/applications/*" + prog)):
            sp.call(["xdg-mime", "default", prog, mime])
            log(p, mime)
            break

def main():
    # browsers
    setDefault(browsers, "x-scheme-handler/http")
    setDefault(browsers, "x-scheme-handler/https")
    setDefault(browsers, "text/html")

    # editors
    setDefault(editors, "text/plain")

    # pdf
    setDefault(pdfs, "application/pdf")

if __name__ == '__main__':
    main()
