#!/usr/bin/env python
#
# This is to wrap makemessages for including django-popolo strings in our
# translation file

import imp
import os
import sys
from shutil import rmtree

import django
from django.conf import settings
from django.core.management import call_command

# Django set up
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, PROJECT_ROOT)
os.environ['DJANGO_SETTINGS_MODULE'] = 'sayit_parldata_eu.settings'

if django.VERSION < (1, 7):
    raise Exception, "This must be run on 1.7+ (for blocktrans' trimmed)"
django.setup()

os.chdir(os.path.join(settings.PROJECT_ROOT, 'speeches'))

apps = ['popolo']

tempdir = 'extra-locales'

if os.path.exists(tempdir):
    if os.path.isdir(tempdir):
        rmtree(tempdir)
    else:
        sys.exit(
            "'extra-locales' exists and is not a directory. Please remove.")

os.mkdir(tempdir)

for app in apps:
    args = imp.find_module(app)
    mod = imp.load_module(app, *args)
    os.symlink(getattr(mod, '__path__')[0], os.path.join(tempdir, app))

call_command('makemessages', all=True, symlinks=True)

rmtree(tempdir)
