   require('./oozie_').configure ctx
   # Allow proxy user inside "core-site.xml"
   require('./core').configure ctx
-  # require('./hdfs').configure ctx
-  # Webhdfs should be active on the NameNode, Secondary NameNode, and all the DataNodes
-  # throw new Error 'WebHDFS not active' if ctx.config.hdp.hdfs_site['dfs.webhdfs.enabled'] isnt 'true'
-  ctx.config.hdp.hue_conf_dir ?= '/etc/hue/conf'
-  ctx.config.hdp.hue_ini ?= {}
-  ctx.config.hdp.hue_user ?= 'hue'
-  ctx.config.hdp.hue_group ?= 'hue'
-
-module.exports.push name: 'HDP Hue # Packages', timeout: -1, callback: (ctx, next) ->
-  ctx.service [
-    name: 'extjs-2.2-1'
-  ,
-    name: 'hue'
-  ], (err, serviced) ->
-    next err, if serviced then ctx.OK else ctx.PASS
-
-module.exports.push name: 'HDP Hue # Core', callback: (ctx, next) ->
-  {hadoop_conf_dir, hadoop_user, hadoop_group} = ctx.config.hdp
-  properties = 
-    'hadoop.proxyuser.hue.hosts': '*'
-    'hadoop.proxyuser.hue.groups': '*'
-    'hadoop.proxyuser.hcat.groups': '*'
-    'hadoop.proxyuser.hcat.hosts': '*'
-  ctx.hconfigure
-    destination: "#{hadoop_conf_dir}/core-site.xml"
-    properties: properties
-    merge: true
-  , (err, configured) ->
-    return next err if err
-    next err, if configured then ctx.OK else ctx.PASS
-
-module.exports.push name: 'HDP Hue # WebHCat', callback: (ctx, next) ->
-  {webhcat_conf_dir} = ctx.config.hdp
-  webhcat_server = ctx.host_with_module 'phyla/hdp/webhcat'
-  hconfigure = (ssh) ->
-    properties = 
-      'webhcat.proxyuser.hue.hosts': '*'
-      'webhcat.proxyuser.hue.groups': '*'
-    ctx.hconfigure
-      destination: "#{webhcat_conf_dir}/webhcat-site.xml"
-      properties: properties
-      merge: true
-    , (err, configured) ->
-      return next err if err
-      next err, if configured then ctx.OK else ctx.PASS
-  if ctx.config.host is webhcat_server
-    hconfigure ctx.ssh
-  else
-    ctx.connect webhcat_server, (err, ssh) ->
-      return next err if err
-      hconfigure ssh
-
-module.exports.push name: 'HDP Hue # Oozie', callback: (ctx, next) ->
-  {oozie_conf_dir} = ctx.config.hdp
-  oozie_server = ctx.host_with_module 'phyla/hdp/oozie_server'
-  hconfigure = (ssh) ->
-    properties = 
-      'oozie.service.ProxyUserService.proxyuser.hue.hosts': '*'
-      'oozie.service.ProxyUserService.proxyuser.hue.groups': '*'
-    ctx.hconfigure
-      ssh: ssh
-      destination: "#{oozie_conf_dir}/oozie-site.xml"
-      properties: properties
-      merge: true
-    , (err, configured) ->
-      return next err if err
-      next err, if configured then ctx.OK else ctx.PASS
-  if ctx.config.host is oozie_server
-    hconfigure ctx.ssh
-  else
-    ctx.connect oozie_server, (err, ssh) ->
-      return next err if err
-      hconfigure ssh
-
-# # http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.0.8.0/bk_installing_manually_book/content/rpm-chap-hue-5-2.html
-module.exports.push name: 'HDP Hue # Configure', callback: (ctx, next) ->
-  {nameservice, active_nn_host, hadoop_conf_dir, hue_conf_dir, hue_ini, hue_smtp_host, webhcat_site} = ctx.config.hdp
+  {nameservice, active_nn_host, hadoop_conf_dir, webhcat_site} = ctx.config.hdp
   webhcat_port = webhcat_site['templeton.port']
   oozie_server = ctx.host_with_module 'phyla/hdp/oozie_server'
   webhcat_server = ctx.host_with_module 'phyla/hdp/webhcat'
   # todo, this might not work as expected after ha migration
   resourcemanager = ctx.host_with_module 'phyla/hdp/yarn_rm'
+  # require('./hdfs').configure ctx
+  # Webhdfs should be active on the NameNode, Secondary NameNode, and all the DataNodes
+  # throw new Error 'WebHDFS not active' if ctx.config.hdp.hdfs_site['dfs.webhdfs.enabled'] isnt 'true'
+  ctx.config.hdp.hue_conf_dir ?= '/etc/hue/conf'
+  ctx.config.hdp.hue_user ?= 'hue'
+  ctx.config.hdp.hue_group ?= 'hue'
+  ctx.config.hdp.hue_db_admin_username ?= ctx.config.mysql_server.username
+  ctx.config.hdp.hue_db_admin_password ?= ctx.config.mysql_server.password
+  hue_ini = ctx.config.hdp.hue_ini ?= {}
   # Configure HDFS Cluster
   hue_ini['hadoop'] ?= {}
   hue_ini['hadoop']['hdfs_clusters'] ?= {}

  hue_ini['desktop']['http_port'] ?= '8000'
  hue_ini['desktop']['secret_key'] ?= 'jFE93j;2[290-eiwMYSECRTEKEYy#e=+Iei*@Mn<qW5o'
  hue_ini['desktop']['smtp'] ?= {}
  hue_ini['desktop']['smtp']['host'] ?= hue_smtp_host if hue_smtp_host
  ctx.ini
    destination: "#{hue_conf_dir}/hue.ini"
    content: hue_ini

  , (err, written) ->
    next err, if written then ctx.OK else ctx.PASS

module.exports.push name: 'HDP Hue # Database', callback: (ctx, next) ->
  {hue_db_admin_username, hue_db_admin_password, hue_ini} = ctx.config.hdp
  modified = false
  engines = 
    mysql: ->
      host = hue_ini['desktop']['database']['host']
      port = hue_ini['desktop']['database']['port']
      username = hue_ini['desktop']['database']['user'] ?= 'hue'
      password = hue_ini['desktop']['database']['password'] ?= 'hue123'
      escape = (text) -> text.replace(/[\\"]/g, "\\$&")
      cmd = "mysql -u#{hue_db_admin_username} -p#{hue_db_admin_password} -h#{host} -P#{port} -e "
      ctx.execute
        cmd: """
        if #{cmd} "use hue"; then exit 2; fi
        #{cmd} "
        create database hue;
        grant all privileges on hue.* to '#{username}'@'localhost' identified by '#{password}';
        grant all privileges on hue.* to '#{username}'@'%' identified by '#{password}';
        flush privileges;
        "
        """
        code_skipped: 2
      , (err, created, stdout, stderr) ->
        return next err, ctx.PASS if err or not created
        ctx.execute
          cmd: """
          su -l hue -c "/usr/lib/hue/build/env/bin/hue syncdb --noinput"
          """
        , (err, executed) ->
          next err, ctx.OK
    sqlite: ->
      next null, ctx.PASS
  engine = hue_ini['desktop']['database']['engine']
  return next new Error 'Hue database engine not supported' unless engines[engine]
  engines[engine]()

###
TODO: install Hue over SSL
