#!/usr/bin/env python3
# coding=utf-8

import os
import subprocess

import wget

# TODO implement the action for mint applets like network speed(being
# considering whereas the risk reminded from Linuxmint)
# TODO refactor sougou_installer() to process universal installation for some daily-use software 
# Upwork: Consider to implement a crawler to detect the download url for linuxmint
# VirtualBox: Consider to implement a simple text crawler then like the up one
# MegaSync: https://mega.nz/linux/MEGAsync/xUbuntu_16.04/amd64/megasync-xUbuntu_16.04_amd64.deb
# Chrome: https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

# TODO implement a simple installer() for the special one of Foxit-reader

CURR_PATH = os.path.abspath('.')
LAST_PATH = os.path.abspath(os.path.join(CURR_PATH, os.pardir))
exe = os.system
isfile = os.path.isfile
expand = os.path.expanduser


def empty_line_at_end(func):
    def wrapper():
        func()
        print()

    return wrapper


@empty_line_at_end
def git_linker():
    print('Git linker:')
    git_path = CURR_PATH + '/git'
    gitconf_dest = expand('~/.gitconfig')
    git_ignore_dest = expand('~/.gitignore_global')
    if isfile(gitconf_dest):
        exe('rm -v ' + gitconf_dest)
    if isfile(git_ignore_dest):
        exe('rm -v ' + git_ignore_dest)
    exe('ln -v -s {0}/gitconfig {1}'.format(git_path, gitconf_dest))
    exe('ln -v -s {0}/gitignore_global {1}'.format(git_path, git_ignore_dest))


@empty_line_at_end
def ssh_linker():
    print('ssh linker:')
    config_dir_dest = expand('~/.ssh')
    if os.path.isdir(config_dir_dest):
        exe('rm -r -v ' + config_dir_dest)
    elif isfile(config_dir_dest):
        exe('rm -v ' + config_dir_dest)
    exe('ln -v -s {}/private_config/ssh {}'.format(LAST_PATH, config_dir_dest))
    exe('chmod -v 600 ~/.ssh/id_rsa')


@empty_line_at_end
def terminator_linker():
    print('terminator linker:')
    config_dir_dest = expand('~/.config/terminator')
    config_file_dest = config_dir_dest + '/config'
    exe('mkdir -v -p ' + config_dir_dest)
    if isfile(config_file_dest):
        exe('rm -v ' + config_file_dest)
    exe('ln -v -s {0}/config4terminator {1}'.format(CURR_PATH, config_file_dest))


@empty_line_at_end
def ipython_linker():
    print('ipython linker:')
    config_file_dest = expand('~/.ipython/profile_default/ipython_config.py')
    if isfile(config_file_dest):
        exe('rm -v ' + config_file_dest)
    exe('ln -v -s {0}/ipython_config.py {1}'.format(CURR_PATH, config_file_dest))


@empty_line_at_end
def vimrc_linker():
    print('vimrc linker:')
    config_file_dest = expand('~/.vimrc')
    if isfile(config_file_dest):
        exe('rm -v ' + config_file_dest)
    exe('ln -v -s {0}/vimrc4ubuntuBased {1}'.format(CURR_PATH, config_file_dest))


@empty_line_at_end
def zshrc_linker():
    print('zshrc linker:')
    config_file_dest = expand('~/.zshrc')
    if isfile(config_file_dest):
        exe('rm -v ' + config_file_dest)
    exe('ln -v -s {0}/zshrc4ubuntuBased {1}'.format(CURR_PATH, config_file_dest))


@empty_line_at_end
def custom_zsh_theme_linker():
    print('custom zsh theme linker:')
    config_file_dest = expand('~/.oh-my-zsh/custom/agnoster.zsh-theme')
    if isfile(config_file_dest):
        exe('rm -v ' + config_file_dest)
    exe('ln -v -s {0}/agnoster.zsh-theme {1}'.format(CURR_PATH, config_file_dest))


@empty_line_at_end
def patch_font():
    default_fonts_dir = expand('~/.local/share/fonts/')
    exe('mkdir -p -v' + default_fonts_dir)
    exe('cp -v Ubuntu_Mono_derivative_Powerline.ttf ' + default_fonts_dir)
    exe('fc-cache -f -v ' + default_fonts_dir)


def sougou_installer():
    url = 'https://pinyin.sogou.com/linux/download.php?f=linux&bit=64'
    _ = wget.download(url, 'sougou.deb')
    print()
    try:
        subprocess.check_call('sudo dpkg -i sougou.deb', shell=True)
    except subprocess.CalledProcessError:
        exe('sudo apt-get -y -f install')
    finally:
        exe('rm -v sougou.deb')


if __name__ == '__main__':
    git_linker()
    ssh_linker()
    terminator_linker()
    ipython_linker()
    vimrc_linker()
    zshrc_linker()
    custom_zsh_theme_linker()
    sougou_installer()
    patch_font()
