#!/usr/bin/python
from __future__ import print_function, unicode_literals
euid = 0

import os, subprocess, shutil

try:
    raw_input
except NameError:
    raw_input = input

if os.geteuid() != euid:
    print ('The installer was last run as user id:', euid, 'To remove all files you must run the uninstaller as the same user')
    if raw_input('Proceed anyway? [y/n]:').lower() != 'y':
        raise SystemExit(1)

frozen_path = None
if not frozen_path or not os.path.exists(os.path.join(frozen_path, 'resources', 'calibre-mimetypes.xml')):
    frozen_path = None

for f in ['/tmp/work/src/calibre-2.77.0/resources/calibre-mimetypes.xml']:
    cmd = ['xdg-mime', 'uninstall', f]
    print ('Removing mime resource:', os.path.basename(f))
    ret = subprocess.call(cmd, shell=False)
    if ret != 0:
        print ('WARNING: Failed to remove mime resource', f)

for x in tuple(['/tmp/work/pkg/usr/bin/ebook-device', '/tmp/work/pkg/usr/bin/ebook-meta', '/tmp/work/pkg/usr/bin/ebook-convert', '/tmp/work/pkg/usr/bin/ebook-polish', '/tmp/work/pkg/usr/bin/markdown-calibre', '/tmp/work/pkg/usr/bin/web2disk', '/tmp/work/pkg/usr/bin/calibre-server', '/tmp/work/pkg/usr/bin/lrf2lrs', '/tmp/work/pkg/usr/bin/lrs2lrf', '/tmp/work/pkg/usr/bin/calibre-debug', '/tmp/work/pkg/usr/bin/calibredb', '/tmp/work/pkg/usr/bin/calibre-parallel', '/tmp/work/pkg/usr/bin/calibre-customize', '/tmp/work/pkg/usr/bin/calibre-complete', '/tmp/work/pkg/usr/bin/fetch-ebook-metadata', '/tmp/work/pkg/usr/bin/calibre-smtp', '/tmp/work/pkg/usr/bin/calibre', '/tmp/work/pkg/usr/bin/lrfviewer', '/tmp/work/pkg/usr/bin/ebook-viewer', '/tmp/work/pkg/usr/bin/ebook-edit', '/tmp/work/pkg/usr/lib/calibre', '/tmp/work/pkg/usr/share/calibre', '/tmp/work/pkg/usr/share/zsh/site-functions/_calibre', '/tmp/work/pkg/usr/share/bash-completion/completions/calibre']) + tuple(['/tmp/work/pkg/usr/share/appdata/calibre-gui.appdata.xml', '/tmp/work/pkg/usr/share/appdata/calibre-ebook-viewer.appdata.xml', '/tmp/work/pkg/usr/share/appdata/calibre-ebook-edit.appdata.xml']) + (os.path.abspath(__file__), __file__, frozen_path):
    if not x or 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', '16'), ('mimetypes', 'application-lrf', '32'), ('mimetypes', 'application-lrf', '48'), ('mimetypes', 'application-lrf', '64'), ('mimetypes', 'application-lrf', '128'), ('mimetypes', 'application-lrf', '256'), ('mimetypes', 'text-lrs', '16'), ('mimetypes', 'text-lrs', '32'), ('mimetypes', 'text-lrs', '48'), ('mimetypes', 'text-lrs', '64'), ('mimetypes', 'text-lrs', '128'), ('mimetypes', 'text-lrs', '256'), ('mimetypes', 'application-x-mobipocket-ebook', '16'), ('mimetypes', 'application-x-mobipocket-ebook', '32'), ('mimetypes', 'application-x-mobipocket-ebook', '48'), ('mimetypes', 'application-x-mobipocket-ebook', '64'), ('mimetypes', 'application-x-mobipocket-ebook', '128'), ('mimetypes', 'application-x-mobipocket-ebook', '256'), ('mimetypes', 'application-x-topaz-ebook', '16'), ('mimetypes', 'application-x-topaz-ebook', '32'), ('mimetypes', 'application-x-topaz-ebook', '48'), ('mimetypes', 'application-x-topaz-ebook', '64'), ('mimetypes', 'application-x-topaz-ebook', '128'), ('mimetypes', 'application-x-topaz-ebook', '256'), ('mimetypes', 'application-x-kindle-application', '16'), ('mimetypes', 'application-x-kindle-application', '32'), ('mimetypes', 'application-x-kindle-application', '48'), ('mimetypes', 'application-x-kindle-application', '64'), ('mimetypes', 'application-x-kindle-application', '128'), ('mimetypes', 'application-x-kindle-application', '256'), ('mimetypes', 'application-x-mobi8-ebook', '16'), ('mimetypes', 'application-x-mobi8-ebook', '32'), ('mimetypes', 'application-x-mobi8-ebook', '48'), ('mimetypes', 'application-x-mobi8-ebook', '64'), ('mimetypes', 'application-x-mobi8-ebook', '128'), ('mimetypes', 'application-x-mobi8-ebook', '256'), ('apps', 'calibre-gui', '16'), ('apps', 'calibre-gui', '32'), ('apps', 'calibre-gui', '48'), ('apps', 'calibre-gui', '64'), ('apps', 'calibre-gui', '128'), ('apps', 'calibre-gui', '256'), ('apps', 'calibre-viewer', '16'), ('apps', 'calibre-viewer', '32'), ('apps', 'calibre-viewer', '48'), ('apps', 'calibre-viewer', '64'), ('apps', 'calibre-viewer', '128'), ('apps', 'calibre-viewer', '256'), ('apps', 'calibre-ebook-edit', '16'), ('apps', 'calibre-ebook-edit', '32'), ('apps', 'calibre-ebook-edit', '48'), ('apps', 'calibre-ebook-edit', '64'), ('apps', 'calibre-ebook-edit', '128'), ('apps', 'calibre-ebook-edit', '256')]
mimetype_icons = []

def remove_icon(context, name, size, update=False):
    cmd = ['xdg-icon-resource', 'uninstall', '--context', context, '--size', size, name]
    if not update:
        cmd.insert(2, '--noupdate')
    print ('Removing icon:', name, 'from context:', context, 'at size:', size)
    ret = subprocess.call(cmd, shell=False)
    if ret != 0:
        print ('WARNING: Failed to remove icon', name)

for i, (context, name, size) in enumerate(icr):
    if context == 'mimetypes':
        mimetype_icons.append((name, size))
        continue
    remove_icon(context, name, size, update=i == len(icr) - 1)

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

print ()

if mimetype_icons and raw_input('Remove the ebook format icons? [y/n]:').lower() in ['', 'y']:
    for i, (name, size) in enumerate(mimetype_icons):
        remove_icon('mimetypes', name, size, update=i == len(mimetype_icons) - 1)
