# -*- coding: utf-8 -*-

import errno
import os
import os.path


HERE = os.path.dirname(__file__)


def force_symlink(source, link_name):
    try:
        os.symlink(source, link_name)
    except OSError as e:
        if e.errno == errno.EEXIST:
            os.unlink(link_name)
            os.symlink(source, link_name)
        else:
            raise


def link_config():
    private_dir = os.path.join(HERE, '..', 'private')
    if not os.path.isdir(private_dir):
        print('Cannot find private dir, you can link your config files manually.')
        return

    conf_dir = os.path.abspath(os.path.join(HERE, '..', 'conf'))
    private_conf_dir = os.path.join(private_dir, 'conf')
    for conf_name in os.listdir(private_conf_dir):
        force_symlink(os.path.join('../private/conf', conf_name), os.path.join(conf_dir, conf_name))

    config_dir = os.path.abspath(os.path.join(HERE, '..', 'doodle', 'config'))
    private_config_dir = os.path.join(private_dir, 'config')
    for config_name in os.listdir(private_config_dir):
        if config_name.endswith('.py') and config_name != '__init__.py':
            force_symlink(os.path.join('../../private/config', config_name), os.path.join(config_dir, config_name))


def mkdir(path, recursive=False):
    try:
        if recursive:
            os.makedirs(path)
        else:
            os.mkdir(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise


def create_dirs():
    log_dir = os.path.join(HERE, '..', 'logs')
    mkdir(log_dir)

    data_dir = os.path.join(HERE, '..', 'data')
    mkdir(data_dir)

    dropbox_redis_dir = os.path.expanduser('~/Dropbox/doodle/redis')
    redis_dir = os.path.join(HERE, '..', 'data', 'redis')
    if os.path.isdir(dropbox_redis_dir):
        force_symlink(dropbox_redis_dir, redis_dir)
    else:
        mkdir(redis_dir)


def install():
    link_config()
    create_dirs()

@@ -559,24 +559,32 @@
 return%0A%0A    
+private_
 conf_dir = o
@@ -594,39 +594,24 @@
 ath.
-abspath(os.path.join(HERE, '..'
+join(private_dir
 , 'c
@@ -619,57 +619,107 @@
 nf')
-)
 %0A    
-private_conf_dir = os.path.join(private_dir
+if os.path.isdir(private_conf_dir):%0A        conf_dir = os.path.abspath(os.path.join(HERE, '..'
 , 'c
@@ -719,25 +719,30 @@
 ..', 'conf')
-%0A
+)%0A    
     for conf
@@ -772,32 +772,36 @@
 vate_conf_dir):%0A
+    
         force_sy
@@ -886,24 +886,32 @@
 name))%0A%0A    
+private_
 config_dir =
@@ -923,49 +923,24 @@
 ath.
-abspath(os.path.join(HERE, '..', 'doodle'
+join(private_dir
 , 'c
@@ -946,22 +946,24 @@
 config')
-)
 %0A    
+if 
 private_
@@ -976,35 +976,80 @@
 _dir
- = os.path.join(private_dir
+:%0A        config_dir = os.path.abspath(os.path.join(HERE, '..', 'doodle'
 , 'c
@@ -1051,25 +1051,30 @@
 ', 'config')
-%0A
+)%0A    
     for conf
@@ -1124,16 +1124,20 @@
         
+    
 if confi
@@ -1193,16 +1193,20 @@
 __.py':%0A
+    
         

fix link_config() fix #3