def main():
    # Fill all changes that occurred when track-changes.py wasn't running.
    if os.path.isdir("out"):
        shutil.rmtree("out", True)

    if not os.path.isdir("out"):
        os.mkdir("out")
 
    startup_changes.sync_offline_changes("posts", "out")

    print "Watching posts directory for changes... CTRL+C to quit."
    watch_directory = "posts"

    event_handler = MyHandler()

    # Run the watchdog.
    observer = Observer()
    observer.schedule(event_handler, watch_directory, True)
    observer.start()

    """
    Keep the script running or else python closes without stopping the observer
    thread and this causes an error.
    """
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
def main():
    # get the directory for the articles
    try:
        working_directory = os.environ["FRESHDESK_LOCAL_PATH"]
        os.chdir(working_directory)
    except KeyError:
        print(Fore.RED + "Please set the environment variable FRESHDESK_LOCAL_PATH" + Fore.RESET)
        sys.exit(1)
    except OSError:
        print(Fore.RED + "Please set the environment variable FRESHDESK_LOCAL_PATH to a valid path." + Fore.RESET)
        sys.exit(1)

    # Fill all changes that occurred when track-changes.py wasn't running.
    if os.path.isdir("out"):
        shutil.rmtree("out", True)

    if not os.path.isdir("out"):
        os.mkdir("out")

    startup_changes.sync_offline_changes("posts", "out")

    print "Watching posts directory for changes... CTRL+C to quit."
    watch_directory = "posts"

    event_handler = MyHandler()

    # Run the watchdog.
    observer = Observer()
    observer.schedule(event_handler, watch_directory, True)
    observer.start()

    """
    Keep the script running or else python closes without stopping the observer
    thread and this causes an error.
    """
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()
        # Check if the folder has been renamed (nothing actually moved).
        if path.split("/")[-2] != move.split("/")[-2]:
            old_name = out_path.replace("/" + path.split("/")[-1], '')
            new_name = move_path.replace("/" + move.split("/")[-1], '')
            os.rename(old_name, new_name)
            print "Renamed " + old_name + " to " + new_name
            os.rename(old_name, new_name)
        # Check if file has been moved
        elif out_path != move_path and path.endswith(FILE_FORMATS):
            shutil.move(out_path, move_path)
            print "Moved " + out_path + " to " + move_path

# Fill all changes that occurred when track-changes.py wasn't running.
if not os.path.isdir("out"):
    os.mkdir("out")
startup_changes.sync_offline_changes("posts", "out")

print "Watching posts directory for changes... CTRL+C to quit."
watch_directory = "posts"

event_handler = MyHandler()

# Run the watchdog.
observer = Observer()
observer.schedule(event_handler, watch_directory, True)
observer.start()

"""
Keep the script running or else python closes without stopping the observer
thread and this causes an error.
"""