#!/usr/bin/env python

"""
Project-wide application configuration.

DO NOT STORE SECRETS, PASSWORDS, ETC. IN THIS FILE.
They will be exposed to users. Use environment variables instead.
See get_secrets() below for a fast way to access them.
"""

import os

"""
NAMES
"""
# Project name used for display
PROJECT_NAME = 'App Template'

# Project name used for paths on the filesystem and in urls
# Use dashes, not underscores
PROJECT_SLUG = 'app-template'

# The name of the repository containing the source
REPOSITORY_NAME = 'app-template'
DEPLOYED_NAME = 'apptemplate'

"""
DEPLOYMENT
"""
PRODUCTION_S3_BUCKETS = ['apps.npr.org', 'apps2.npr.org']
PRODUCTION_SERVERS = ['cron.nprapps.org']

STAGING_S3_BUCKETS = ['stage-apps.npr.org']
STAGING_SERVERS = ['50.112.92.131']

# Should code be deployed to the web/cron servers?
DEPLOY_TO_SERVERS = False

# Should the crontab file be installed on the servers?
# If True, DEPLOY_TO_SERVERS must also be True
DEPLOY_CRONTAB = False

# Should the service configurations be installed on the servers?
# If True, DEPLOY_TO_SERVERS must also be True
DEPLOY_SERVICES = False

# These variables will be set at runtime. See configure_targets() below
S3_BUCKETS = []
SERVERS = []
DEBUG = True

"""
COPY EDITING
"""
COPY_GOOGLE_DOC_KEY = '0AlXMOHKxzQVRdHZuX1UycXplRlBfLVB0UVNldHJYZmc'

"""
SHARING
"""
PROJECT_DESCRIPTION = 'An opinionated project template for (mostly) server-less apps.'
SHARE_URL = 'http://%s/%s/' % (PRODUCTION_S3_BUCKETS[0], PROJECT_SLUG)


TWITTER = {
    'TEXT': PROJECT_NAME,
    'URL': SHARE_URL
}

FACEBOOK = {
    'TITLE': PROJECT_NAME,
    'URL': SHARE_URL,
    'DESCRIPTION': PROJECT_DESCRIPTION,
    'IMAGE_URL': '',
    'APP_ID': '138837436154588'
}

NPR_DFP = {
    'STORY_ID': '171421875',
    'TARGET': '\/news_politics;storyid=171421875'
}

"""
SERVICES
"""
GOOGLE_ANALYTICS_ID = 'UA-5828686-4'

"""
Utilities
"""
def get_secrets():
    """
    A method for accessing our secrets.
    """
    secrets = [
        '%s_TUMBLR_APP_KEY' % DEPLOYED_NAME,
        '%s_TUMBLR_OAUTH_TOKEN' % DEPLOYED_NAME,
        '%s_TUMBLR_OAUTH_TOKEN_SECRET' % DEPLOYED_NAME,
        '%s_TUMBLR_APP_SECRET' % DEPLOYED_NAME
    ]

    secrets_dict = {}

    for secret in secrets:
        # Saves the secret with the old name.
        secrets_dict[secret.replace('%s_' % DEPLOYED_NAME, '')] = os.environ.get(secret, None)

    return secrets_dict

def configure_targets(deployment_target):
    """
    Configure deployment targets. Abstracted so this can be
    overriden for rendering before deployment.
    """
    global S3_BUCKETS
    global SERVERS
    global DEBUG

    if deployment_target == 'production':
        S3_BUCKETS = PRODUCTION_S3_BUCKETS
        SERVERS = PRODUCTION_SERVERS
        DEBUG = False
    else:
        S3_BUCKETS = STAGING_S3_BUCKETS
        SERVERS = STAGING_SERVERS
        DEBUG = True

"""
Run automated configuration
"""
DEPLOYMENT_TARGET = os.environ.get('DEPLOYMENT_TARGET', None)

configure_targets(DEPLOYMENT_TARGET)


@@ -535,38 +535,8 @@
 ate'
-%0ADEPLOYED_NAME = 'apptemplate'
 %0A%0A%22%22
@@ -1915,24 +1915,76 @@
 ts.%0A    %22%22%22%0A
+    env_var_prefix = PROJECT_SLUG.replace('-', '')%0A%0A
     secrets 
@@ -2013,37 +2013,38 @@
 _APP_KEY' %25 
-DEPLOYED_NAME
+env_var_prefix
 ,%0A        '%25
@@ -2063,37 +2063,38 @@
 TH_TOKEN' %25 
-DEPLOYED_NAME
+env_var_prefix
 ,%0A        '%25
@@ -2124,29 +2124,30 @@
 CRET' %25 
-DEPLOYED_NAME
+env_var_prefix
 ,%0A      
@@ -2173,29 +2173,30 @@
 CRET' %25 
-DEPLOYED_NAME
+env_var_prefix
 %0A    %5D%0A%0A
@@ -2339,21 +2339,22 @@
 ' %25 
-DEPLOYED_NAME
+env_var_prefix
 , ''

Kill DEPLOYED_NAME.