import org.apache.log4j.Level

/******************************************************************************\
 *  CONFIG MANAGEMENT
 \******************************************************************************/
def appName = 'taxonoverflow'
def ENV_NAME = "${appName.toUpperCase()}_CONFIG"
default_config = "/data/${appName}/config/${appName}-config.properties"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}

if(System.getenv(ENV_NAME) && new File(System.getenv(ENV_NAME)).exists()) {
    println "[${appName}] Including configuration file specified in environment: " + "${System.getenv(ENV_NAME)}/${appName}-config.properties";
    default_config = "${System.getenv(ENV_NAME)}/${appName}-config.properties"
    grails.config.locations.add "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME) && new File(System.getProperty(ENV_NAME)).exists()) {
    println "[${appName}] Including configuration file specified on command line: " + "${System.getProperty(ENV_NAME)}/${appName}-config.properties";
    default_config = "${System.getProperty(ENV_NAME)}/${appName}-config.properties"
    grails.config.locations.add "file:" + System.getProperty(ENV_NAME)
} else if(new File(default_config).exists()) {
    println "[${appName}] Including default configuration file: " + default_config;
    grails.config.locations.add "file:" + default_config
} else {
    println "[${appName}] No external configuration file defined."
}

println "[${appName}] (*) grails.config.locations = ${grails.config.locations}"

ala.baseURL = "http://www.ala.org.au"
bie.baseURL = "http://bie.ala.org.au"
bie.searchPath = "/search"
grails.project.groupId = "au.org.ala" // change this to alter the default package name and Maven publishing destination

bie.baseUrl = "http://bie.ala.org.au"
collectory.baseUrl = "http://collections.ala.org.au"
biocache.apiKey = "api-key-to-use"

biocacheService.baseUrl = "http://biocache.ala.org.au/ws"
biocacheWebapp.baseUrl = "http://biocache.ala.org.au"

ecodata.baseUrl = "http://fielddata.ala.org.au"
pigeonhole.baseUrl = "http://sightings.ala.org.au"


biocacheService.recordUrl = "http://biocache.ala.org.au/ws/occurrence/"
biocacheWebapp.recordUrl = "http://biocache.ala.org.au/occurrence/"

ecodata.recordUrl = "http://ecodata-sightings-dev.ala.org.au/record/"
pigeonhole.recordUrl = "http://sightings.ala.org.au/edit/"

accepted.answer.threshold = 3

//this list should be empty for production
testUsers="taxonoverflow%2Bmick@gmail.com,taxonoverflow%2Bkeef@gmail.com,taxonoverflow%2Bbasher@gmail.com,taxonoverflow%2Bronnie@gmail.com,taxonoverflow%2Bbill@gmail.com"

biocache.queryContext = "" // datahub uuid - e.g. ozcam  = " data_hub_uid:dh1 || avh = data_hub_uid:dh2"
biocache.downloads.extra = "dataResourceUid,dataResourceName.p"
biocache.ajax.useProxy = false
collections.baseUrl = "http://collections.ala.org.au"

ala.image.service.url = "http://images.ala.org.au"

// The ACCEPT header will not be used for content negotiation for user agents containing the following strings (defaults to the 4 major rendering engines)
grails.mime.disable.accept.header.userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident']
grails.mime.types = [ // the first one is the default format
    all:           '*/*', // 'all' maps to '*' or the first available format in withFormat
    atom:          'application/atom+xml',
    css:           'text/css',
    csv:           'text/csv',
    form:          'application/x-www-form-urlencoded',
    html:          ['text/html','application/xhtml+xml'],
    js:            'text/javascript',
    json:          ['application/json', 'text/json'],
    multipartForm: 'multipart/form-data',
    rss:           'application/rss+xml',
    text:          'text/plain',
    hal:           ['application/hal+json','application/hal+xml'],
    xml:           ['text/xml', 'application/xml']
]

// URL Mapping Cache Max Size, defaults to 5000
//grails.urlmapping.cache.maxsize = 1000

// Legacy setting for codec used to encode data with ${}
grails.views.default.codec = "html"

// The default scope for controllers. May be prototype, session or singleton.
// If unspecified, controllers are prototype scoped.
grails.controllers.defaultScope = 'singleton'


expertRole = 'ROLE_TAXONOVERFLOW_EXPERT'


// Fix grails taglib `<g:paginate/>` to work with bootstrap css.
grails.plugins.twitterbootstrap.fixtaglib = true

// GSP settings
grails {
    views {
        gsp {
            encoding = 'UTF-8'
            htmlcodec = 'xml' // use xml escaping instead of HTML4 escaping
            codecs {
                expression = 'html' // escapes values inside ${}
                scriptlet = 'html' // escapes output from scriptlets in GSPs
                taglib = 'none' // escapes output from taglibs
                staticparts = 'none' // escapes output from static template parts
            }
        }
        // escapes all not-encoded output at final stage of outputting
        // filteringCodecForContentType.'text/html' = 'html'
    }
}

// What URL patterns should be processed by the resources plugin
grails.resources.adhoc.patterns = ['/js/*', '/images/*', '/css/*', '/plugins/*', '/vendor/*']
grails.resources.adhoc.includes = ['/js/**', '/images/**', '/css/**','/plugins/**', '/vendor/**']

grails.converters.encoding = "UTF-8"
// scaffolding templates configuration
grails.scaffolding.templates.domainSuffix = 'Instance'

// Set to false to use the new Grails 1.2 JSONBuilder in the render method
grails.json.legacy.builder = false
// enabled native2ascii conversion of i18n properties files
grails.enable.native2ascii = true
// packages to include in Spring bean scanning
grails.spring.bean.packages = []
// whether to disable processing of multi part requests
grails.web.disable.multipart=false

// request parameters to mask when logging exceptions
grails.exceptionresolver.params.exclude = ['password']

// configure auto-caching of queries by default (if false you can cache individual queries with 'cache: true')
grails.hibernate.cache.queries = false

// configure passing transaction's read-only attribute to Hibernate session, queries and criterias
// set "singleSession = false" OSIV mode in hibernate configuration after enabling
grails.hibernate.pass.readonly = false
// configure passing read-only to OSIV session by default, requires "singleSession = false" OSIV mode
grails.hibernate.osiv.readonly = false



/* Environment configuration */
environments {
    development {
        grails {
            /*  Mail Configuration */
            mail {
                // I am using MockSmtp:
                host = "localhost"
                port = "1025"
                subjectPrefix = "[local]"
            }
        }

        notifications.enabled = false

        /*  ElasticSearch Configuration */
        elasticsearch.path.home = System.getProperty(ENV_NAME) ? "${System.getProperty(ENV_NAME)}/elasticsearch" : "/data/${appName}/elasticsearch"
    }

    test {
        notifications.enabled = false


        /*  ElasticSearch Configuration */
        elasticsearch.path.home = System.getProperty(ENV_NAME) ? "${System.getProperty(ENV_NAME)}/elasticsearch-test" : "/data/${appName}/elasticsearch-test"
        elasticsearch.index.store.type = "memory"
        elasticsearch.logging.json = true
    }

    production {
        notifications.enabled = true

        /*  ElasticSearch Configuration */
        elasticsearch.path.home = System.getProperty(ENV_NAME) ? "${System.getProperty(ENV_NAME)}/elasticsearch" : "/data/${appName}/elasticsearch"
    }
}

environments {
    development {
        grails.logging.jul.usebridge = true
    }
    production {
        grails.logging.jul.usebridge = false
    }
}

def loggingDir = (System.getProperty('catalina.base') ? System.getProperty('catalina.base') + '/logs' : './logs')
/* log4j configuration */
log4j = {
// Example of changing the log pattern for the default console
    appenders {
        environments {
            production {
                rollingFile name: "tomcatLog", maxFileSize: '1MB', file: "${loggingDir}/${appName}.log", threshold: Level.ERROR, layout: pattern(conversionPattern: "%d %-5p [%c{1}] %m%n")
            }
            development {
                console name: "stdout", layout: pattern(conversionPattern: "%d %-5p [%c{1}] %m%n"), threshold: Level.DEBUG
            }
            test {
                rollingFile name: "stdout,tomcatLog", maxFileSize: '1MB', file: "/tmp/${appName}", threshold: Level.DEBUG, layout: pattern(conversionPattern: "%d %-5p [%c{1}] %m%n")
            }
        }
    }
    root {
// change the root logger to my tomcatLog file
        error 'tomcatLog'
        warn 'tomcatLog'
        additivity = true
    }

    warn    'au.org.ala.cas.client',
            'grails.spring.BeanBuilder',
            'grails.plugin.webxml',
            'grails.plugin.cache.web.filter',
            'grails.app.services.org.grails.plugin.resource',
            'grails.app.taglib.org.grails.plugin.resource',
            'grails.app.resourceMappers.org.grails.plugin.resource'

    debug 'grails.app',
            'au.org.ala.taxonoverflow'
}
