"""
Sets up the dotfiles in this repository

Requires python 3
"""

import sys
import os
import shutil

from dploy.util import resolve_abs_path

def link_file(target, dest):
    dest_dir = os.path.dirname(dest)
    # get a relative path to the target from the destination location of
    # the file, this way we will have a relative symlink
    target_relative = os.path.relpath(target, start=dest_dir)

    try:
        os.symlink(target_relative, dest)
    except Exception as exception_message:
        print(exception_message)

def deploy_files(target, dest):
    target_absolute = resolve_abs_path(target)
    dest_absolute = resolve_abs_path(dest)

    print("{dest_absolute} = dest_absolute".format(dest_absolute=dest_absolute))
    print("{target_absolute} = target_absolute".format(target_absolute=target_absolute))

    if os.path.islink(dest_absolute):
        link_location = os.readlink(dest_absolute)
        print("dest is a link that points to {link_location}".format(
            link_location=link_location))

        print("dest is a link that points to {link_location}".format(
            link_location=resolve_abs_path(link_location)))

        if resolve_abs_path(os.readlink(dest_absolute)) == target_absolute:
            print("already linked")
        else:
            print("Abort other link exits")
    elif os.path.isfile(dest_absolute):
        print("Abort File Already Exists")
    elif os.path.isdir(dest_absolute):
        for file in os.listdir(target_absolute):
            link_file(os.path.join(target_absolute, file),
                      os.path.join(dest_absolute, file))
    else:
        link_file(target_absolute, dest_absolute)
        print("Creating link {dest} pointing to {target}".format(
            target=target,
            dest=dest))

def create_directories(directories):
    print("Creating Directories")

    for directory in directories:
        directory = resolve_abs_path(directory)
        print("Creating Directory: {0}".format(directory))

        try:
            os.makedirs(directory)
        except Exception as exception_message:
            print(exception_message)

@@ -444,24 +444,99 @@
 tive, dest)%0A
+        print(%22Link: %7Bdest%7D =%3E %7Btarget%7D%22.format(target=target, dest=dest))%0A
     except E
@@ -731,69 +731,26 @@
     
-print(%22%7Bdest_absolute%7D = dest_absolute%22.format(dest_absolute=
+if os.path.islink(
 dest
@@ -763,86 +763,49 @@
 ute)
-)
+:
 %0A    
-print(%22%7Btarget_absolute%7D = target_absolute%22.format(target_absolute=targe
+    link_location = os.readlink(des
 t_ab
@@ -815,88 +815,73 @@
 ute)
-)%0A%0A
+%0A  
     
+  
 if 
-os.path.islink(dest_absolute):%0A        link_location = os.readlink(des
+resolve_abs_path(os.readlink(dest_absolute)) == targe
 t_ab
@@ -878,34 +878,38 @@
  target_absolute
-)%0A
+:%0A    
         print(%22d
@@ -911,114 +911,175 @@
 nt(%22
-dest is a link that points to %7Blink_location%7D%22.format(%0A            link_location=link_location))%0A%0A
+Link: Already Linked %7Bdest%7D =%3E %7Btarget%7D%22.format(target=target,%0A                                                                   dest=dest))%0A        else:%0A   
         
 prin
@@ -1078,43 +1078,51 @@
     
+ 
 print(%22
-d
+Abort: D
 est 
-is a l
+Is A L
 ink 
-t
+T
 hat 
-p
+P
 oints 
-t
+T
 o %7Bl
@@ -1136,32 +1136,36 @@
 ation%7D%22.format(%0A
+    
             link
@@ -1211,179 +1211,8 @@
 n)))
-%0A%0A        if resolve_abs_path(os.readlink(dest_absolute)) == target_absolute:%0A            print(%22already linked%22)%0A        else:%0A            print(%22Abort other link exits%22)
 %0A   
@@ -1272,10 +1272,11 @@
 bort
- F
+: f
 ile 
@@ -1559,125 +1559,8 @@
 ute)
-%0A        print(%22Creating link %7Bdest%7D pointing to %7Btarget%7D%22.format(%0A            target=target,%0A            dest=dest))
 %0A%0Ade

remove debugging printing