#!/usr/local/bin/python2.7
euid = 0

import os, subprocess, shutil

if os.geteuid() != euid:
    print 'WARNING: uninstaller must be run as', euid, 'to remove all files'

for x in ['/usr/local/bin/ebook-device', '/usr/local/bin/ebook-meta', '/usr/local/bin/ebook-convert', '/usr/local/bin/markdown-calibre', '/usr/local/bin/web2disk', '/usr/local/bin/calibre-server', '/usr/local/bin/lrf2lrs', '/usr/local/bin/lrs2lrf', '/usr/local/bin/calibre-debug', '/usr/local/bin/calibredb', '/usr/local/bin/calibre-parallel', '/usr/local/bin/calibre-customize', '/usr/local/bin/calibre-complete', '/usr/local/bin/pdfmanipulate', '/usr/local/bin/fetch-ebook-metadata', '/usr/local/bin/epub-fix', '/usr/local/bin/calibre-smtp', '/usr/local/bin/calibre', '/usr/local/bin/lrfviewer', '/usr/local/bin/ebook-viewer', '/usr/local/bin/calibre-mount-helper', '/usr/local/lib/calibre', '/usr/local/share/calibre', '/usr/local/etc/bash_completion.d/calibre', '/usr/local/man/man1/ebook-meta.1', '/usr/local/man/man1/ebook-convert.1', '/usr/local/man/man1/web2disk.1', '/usr/local/man/man1/calibre-server.1', '/usr/local/man/man1/lrf2lrs.1', '/usr/local/man/man1/lrs2lrf.1', '/usr/local/man/man1/calibre-debug.1', '/usr/local/man/man1/calibredb.1', '/usr/local/man/man1/calibre-customize.1', '/usr/local/man/man1/pdfmanipulate.1', '/usr/local/man/man1/fetch-ebook-metadata.1', '/usr/local/man/man1/epub-fix.1', '/usr/local/man/man1/calibre-smtp.1']:
    if not os.path.exists(x): continue
    print 'Removing', x
    try:
        if os.path.isdir(x):
            shutil.rmtree(x)
        else:
            os.unlink(x)
    except Exception as e:
        print 'Failed to delete', x
        print '	', e

icr = [('mimetypes', 'application-lrf', '128'), ('mimetypes', 'application-lrs', '128'), ('apps', 'calibre-gui', '128'), ('apps', 'calibre-viewer', '128')]
for context, name, size in icr:
    cmd = ['xdg-icon-resource', 'uninstall', '--context', context, '--size', size, name]
    if (context, name) != icr[-1]:
        cmd.insert(2, '--noupdate')
    ret = subprocess.call(cmd)
    if ret != 0:
        print 'WARNING: Failed to remove icon', name

mr = ['calibre-gui.desktop', 'calibre-lrfviewer.desktop', 'calibre-ebook-viewer.desktop']
for f in mr:
    cmd = ['xdg-desktop-menu', 'uninstall', f]
    ret = subprocess.call(cmd)
    if ret != 0:
        print 'WARNING: Failed to remove menu item', f

os.remove(os.path.abspath(__file__))
