"""
Command to create symlink admin extras media 
"""

import os

from django.core.management.base import NoArgsCommand
from django.conf import settings

from rldjango.apps import destinfo


def create_dir(location):
    """
    Creates location if doesnt exists.
    """

    if not os.path.exists(location):
        print "Creating", location
        os.mkdir(location)

def create_symlink(source, target):
    """
    Creates symlink if doesnt exists.
    """

    if not os.path.exists(target):
        print "Creating symlink", source, target
        os.symlink(source, target)

def get_media_root():
    """
    Returns settings.MEDIA_ROOT
    """
    return settings.MEDIA_ROOT

def get_destinfo_root():
    """
    Returns rldjango.aps.destinfo path
    """
    return os.path.join(os.path.realpath(destinfo.__path__[0]))

def destinfo_media():
    """
    Symlink destinfo media.
    """
    create_dir(os.path.join(get_media_root()))
    source = os.path.join(get_destinfo_root(), 'media')
    target = os.path.join(get_media_root(), 'destinfo')
    create_symlink(source, target)

class Command(NoArgsCommand):
    """
    Ads desinfo_media command to manage.py
    """
    def handle_noargs(self, **options):
        """
        Prepares media for destinfo
        """
        destinfo_media()
