define wordpress::instance::app (
  $install_dir, $install_url, $version, $db_name, $db_host,
  $db_user, $db_password, $wp_owner, $wp_group, $wp_lang, $wp_config_content,
  $wp_additional_config, $wp_table_prefix, $wp_multisite, $wp_site_domain, $wp_debug, 
) {

## Check if values are string ##
validate_string($install_dir,$install_url,$version,$db_name,$db_host,$db_user,$db_password,$wp_owner,$wp_group, $wp_lang, $wp_additional_config, $wp_table_prefix, $wp_site_domain)

## Check if values are boolean ##
validate_bool($wp_multisite, $wp_debug)

## Check if values are absolute path ##
validate_absolute_path($install_dir)

## Limit parameters with config_content - destroy this ##
  if $wp_config_content and ($wp_lang or $wp_debug or $wp_multisite or $wp_site_domain) {
    warning('When $wp_config_content is set, the following parameters are ignored: $wp_table_prefix, $wp_lang, $wp_debug, $wp_multisite, $wp_site_domain, $wp_additional_config')
  }

## Check dependant variables ##
  if $wp_multisite and ! $wp_site_domain {
    fail('wordpress class requires `wp_site_domain` parameter when `wp_multisite` is true')
  }

## Default variables for files
  File {
    owner  => $wp_owner,
    group  => $wp_group,
    mode   => '0644',
  }
  Exec {
    path      => ['/bin','/sbin','/usr/bin','/usr/sbin'],
    cwd       => $install_dir,
    logoutput => 'on_failure',
  }

## Installation directory and check permissions
  if ! defined(File[$install_dir]) {
    file { $install_dir:
      ensure  => directory,
      recurse => true,
    }
  } else {
    notice("Cannot manage permissions of ${install_dir}!")
  }

## Download and extract Wordpress files
  exec { "Downloading Wordpress to ${install_dir}":
    command => "wget ${install_url}/wordpress-${version}.tar.gz",
    creates => "${install_dir}/wordpress-${version}.tar.gz",
    require => File[$install_dir],
    user    => $wp_owner,
    group   => $wp_group,
  }
  -> exec { "Extracting Wordpress":
    command => "tar zxvf ./wordpress-${version}.tar.gz --strip-components=1",
    creates => "${install_dir}/index.php",
    user    => $wp_owner,
    group   => $wp_group,
  }
  -> exec { "Change ownership for files":
    command     => "chown -R ${wp_owner}:${wp_group} ${install_dir}",
#    refreshonly => true,
    user        => $wp_owner,
    group       => $wp_group,
  }
  -> exec { "Deleting archive":
    command => "rm ${$install_dir}/wordpress-${version}.tar.gz",
    require => File[$install_dir],
    user    => $wp_owner,
    group   => $wp_group, 
  }

## Configure Wordpress via wp-config file (create templates, salts and complete) 
  concat { "${install_dir}/wp-config.php":
    owner   => $wp_owner,
    group   => $wp_group,
    mode    => '0755',
    require => Exec["Extracting Wordpress"],
  }
  
  if $wp_config_content {
    concat::fragment { "${install_dir}/wp-config.php body":
      target  => "${install_dir}/wp-config.php",
      content => $wp_config_content,
      order   => '2',
    }
  } else {
# Template for salts uses no variables
    file { "${install_dir}/wp-keysalts.php":
      ensure  => present,
      content => template('templates/wp-keysalts.php.erb'),
      replace => false,
      require => Exec["Extract wordpress ${install_dir}"],
    }
    concat::fragment { "${install_dir}/wp-config.php keysalts":
      target  => "${install_dir}/wp-config.php",
      source  => "${install_dir}/wp-keysalts.php",
      order   => '1',
      require => File["${install_dir}/wp-keysalts.php"],
    }
# Template uses variables needed for wp-config
    concat::fragment { "${install_dir}/wp-config.php body":
      target  => "${install_dir}/wp-config.php",
      content => template('templates/wp-config.php.erb'),
      order   => '2',
    }
  }
  
}
