#!/bin/bash

# change permission to config
hadoop_configs=('core-site.xml' 'hdfs-site.xml' 'mapred-site.xml' 'yarn-site.xml')
for conf in "${{hadoop_configs[@]}}"
do
    chown -R {hadoop_group}:{hadoop_user} {hadoop_conf_dir}/$conf
done

chown -R {hadoop_group}:{hadoop_user} {oozie_conf_dir}/oozie-site.xml

# create dirs for hdfs and mapreduce service
dirs=({hadoop_name_dirs} {hadoop_data_dirs} {hadoop_log_dir} {hadoop_secure_dn_log_dir} {yarn_log_dir})
for dir in "${{dirs[@]}}"
do
    mkdir -p $dir
    chown -R {hadoop_group}:{hadoop_user} $dir
done

# change hadoop log dir
sed -i "s,\#export HADOOP_LOG_DIR=.*,export HADOOP_LOG_DIR={hadoop_log_dir}," {hadoop_conf_dir}/hadoop-env.sh
sed -i "s,export HADOOP_SECURE_DN_LOG_DIR=.*,export HADOOP_SECURE_DN_LOG_DIR={hadoop_secure_dn_log_dir}," {hadoop_conf_dir}/hadoop-env.sh

# change yarn log dir
sed -i "s,YARN_LOG_DIR=.*,YARN_LOG_DIR={yarn_log_dir}," {hadoop_conf_dir}/yarn-env.sh

# prepare scaling files
sc_all_files=('dn-include' 'nm-include' 'dn-exclude' 'nm-exclude')
for file in "${{sc_all_files[@]}}"
do
    touch {hadoop_conf_dir}/$file
    chown {hadoop_group}:{hadoop_user} {hadoop_conf_dir}/$file
done
