# -*- 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"):
        remove_tree("/opt/thunderbird")
    copytree2("thunderbird", "/opt/thunderbird")
    run("chown -R root:root /opt/thunderbird")
    run("ln -sf /opt/thunderbird/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")

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