#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License, version 3.
# See the file http://www.gnu.org/licenses/gpl.txt

from pisi.actionsapi import shelltools, get, pisitools
from pisi.util import join_path

tag = 'release-{version}-linux1'.format(version=get.srcVERSION())

shelltools.export('YARN_CACHE_FOLDER', get.workDIR())
shelltools.export('YARN_DISABLE_SELF_UPDATE_CHECK', 'true')

yarn_params = '--global-folder %s --unsafe-perm=true --allow-root' % get.workDIR()


def setup():
    old_dir = get.curDIR()
    shelltools.cd('..')
    shelltools.system('rm -rf %s' % old_dir)
    shelltools.system('git clone https://github.com/shiftkey/desktop.git#{} {}'.format(tag, old_dir))
    shelltools.cd(old_dir)
    shelltools.system('yarn install %s' % yarn_params)


def build():
    shelltools.system('yarn build:prod %s' % yarn_params)


def install():
    shelltools.copytree('dist/github-desktop-linux-x64/', join_path(get.installDIR(), 'usr/lib/github-desktop'))
    pisitools.dosym('/usr/lib/github-desktop/github-desktop', '/usr/bin/github-desktop')

    for size in [256, 512, 1024]:
        pisitools.insinto(
            "/usr/share/icons/hicolor/{size}x{size}/apps/".format(size=size),
            "app/static/logos/{size}x{size}.png".format(size=size),
            "github-desktop.png"
        )
