#!/usr/bin/env python2
# 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

if __name__ == "__main__":
    # Users update from a version before 15.3 have /etc/skel/Templates,
    # if they try to create a new user, there will be a redundant templates
    # directory, we need to remove it first.
    if path.exists(path.expanduser('~/Templates')):
       shutil.rmtree(path.expanduser('~/Templates/'))

    lang, charset = locale.getdefaultlocale()
    if lang == "zh_CN":
        if path.exists(path.expanduser('~/.Templates/zh')):
            for  i in os.listdir(path.expanduser('~/.Templates/zh')):
                shutil.move(os.path.join(path.expanduser('~/.Templates/zh'), i),
                    path.expanduser('~/.Templates/'))
    elif lang == "zh_TW":
        if path.exists(path.expanduser('~/.Templates/tw')):
            for  i in os.listdir(path.expanduser('~/.Templates/tw')):
                shutil.move(os.path.join(path.expanduser('~/.Templates/tw'), i),
                    path.expanduser('~/.Templates/'))
    else:
        if path.exists(path.expanduser('~/.Templates/en')):
            for  i in os.listdir(path.expanduser('~/.Templates/en')):
                shutil.move(os.path.join(path.expanduser('~/.Templates/en'), i),
                    path.expanduser('~/.Templates/'))

    if path.exists(path.expanduser('~/.Templates/zh')):
        shutil.rmtree(path.expanduser('~/.Templates/zh'))
    if path.exists(path.expanduser('~/.Templates/tw')):
        shutil.rmtree(path.expanduser('~/.Templates/tw'))
    if path.exists(path.expanduser('~/.Templates/en')):
        shutil.rmtree(path.expanduser('~/.Templates/en'))

    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'

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

    if path.exists('/usr/lib/PepperFlash'):
		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'))
