#!/usr/bin/env python3
# vim: set ts=4 sw=4 fileencoding=utf-8:
# Luomio <nohappiness@gmail.com>
# Filename: dde-first-run.py
# Create Date: 27-03, 13

from os import path, remove
import locale
import os
import shutil
import getpass
import re
import subprocess
from gi.repository import GLib

def querySystemType():
    f = open("/etc/deepin-version", "r")
    content = f.read()

    r = re.search(r"^Type=(.*)$", content, re.M)

    return r.group(1)

def queryXDGDesktopDir():
    return GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP)

if __name__ == "__main__":
    lang, charset = locale.getdefaultlocale()
    sys_type = querySystemType()
    desktop_path = queryXDGDesktopDir()

    if path.exists("/usr/bin/dde-introduction"):
        subprocess.Popen("/usr/bin/dde-introduction")

    # create symlink for introduction video
    # fix introduction to demo.mp4,different mirror interfaces are consistent,but the content is different
    introduction_video_path = "/usr/share/dde-introduction/demo.mp4"
    introduction_video_link_path = "~/Videos/dde-introduction.mp4"
    if path.exists(introduction_video_path):
        if path.exists(path.expanduser(introduction_video_link_path)):
            remove(path.expanduser(introduction_video_link_path))
        os.symlink(introduction_video_path, path.expanduser('~/Videos/dde-introduction.mp4'))

    if sys_type != "Desktop":
        desktops = ['dde-computer.desktop', 'dde-trash.desktop' , 'dde-home.desktop']
        if not path.exists(desktop_path):
            os.makedirs(desktop_path)
        for desktop in desktops:
            desktop_file = path.join("/usr/share/applications", desktop)
            if path.exists(desktop_file):
                shutil.copyfile(desktop_file, path.join(desktop_path, desktop))

    chrome_config = None
    if not path.exists(path.expanduser('~/.config/google-chrome')):
        if path.exists('/usr/share/deepin-default-settings/google-chrome/override-chrome-config.tar'):
            chrome_config = '/usr/share/deepin-default-settings/google-chrome/override-chrome-config.tar'
        elif path.exists('/usr/share/deepin-default-settings/google-chrome/chrome-config-%s.tar' % lang):
            chrome_config = '/usr/share/deepin-default-settings/google-chrome/chrome-config-%s.tar' % lang
        elif path.exists('/usr/share/deepin-default-settings/google-chrome/chrome-config.tar'):
            chrome_config = '/usr/share/deepin-default-settings/google-chrome/chrome-config.tar'
        else:
            chrome_config = None

    if chrome_config is not None:
        import tarfile
        tar = tarfile.open(chrome_config)
        tar.extractall(path.expanduser('~/.config/'))
        tar.close()

    if path.exists(path.expanduser('~/.config/google-chrome/PepperFlash/latest-component-updated-flash')):
        user_name=getpass.getuser()
        pepperflash = path.expanduser('~/.config/google-chrome/PepperFlash/latest-component-updated-flash')
        with open(pepperflash,"r") as f:
            lines = f.readlines()
        with open(pepperflash,"w") as f:
            for line in lines:
                f.write(re.sub(r'deepin',user_name,line))

    if path.exists(path.expanduser('~/.config/autostart/dde-first-run.desktop')):
        remove(path.expanduser('~/.config/autostart/dde-first-run.desktop'))
