# -*- coding: utf-8 -*-
from setuphelpers import *
import tarfile

def install():
    package_version = control.version.split("-")[0]
    filename = glob.glob('*.tar.xz')[0]

    run_notfatal("apt-get remove thunderbird -y")
    extract(filename)
    killalltasks("thunderbird")
    if isdir("/opt/thunderbird-esr"):
        remove_tree("/opt/thunderbird-esr")
    copytree2("thunderbird", "/opt/thunderbird-esr")
    run("chown -R root:root /opt/thunderbird-esr")
    run("ln -sf /opt/thunderbird-esr/thunderbird /usr/bin/thunderbird")
    filecopyto("thunderbird.desktop", "/usr/share/applications/thunderbird.desktop")
    run("chown root:root /usr/share/applications/thunderbird.desktop")


def uninstall():
    killalltasks("thunderbird")
    remove_file("/usr/share/applications/thunderbird.desktop")
    remove_tree("/opt/thunderbird-esr")

def extract(filename, path="."):
    with tarfile.open(filename) as tar:
        tar.extractall(path)
