#!/bin/bash

hdfs_dirs="/user /var/log /tmp"
mapred_dirs="/tmp/hadoop-yarn/staging /tmp/hadoop-yarn/staging/history /tmp/hadoop-yarn/staging/history/done /tmp/hadoop-yarn/staging/history/done_intermediate"
yarn_dirs="/tmp/hadoop-yarn /var/log/hadoop-yarn"

# Must be run as root
if [[ $EUID -ne 0 ]]
then
  echo "This must be run as root" 1>&2
  exit 1
fi

# Start the namenode if it isn't running
started=0
systemctl status hadoop-namenode > /dev/null 2>&1
rc=$?
if [[ $rc -gt 0 ]]
then
  # Format the namenode if it hasn't been formatted
  runuser hdfs -s /bin/bash /bin/bash -c "hdfs namenode -format -nonInteractive" > /dev/null 2>&1
  if [[ $? -eq 0 ]]
  then
    echo "Formatted the Hadoop namenode"
  fi

  echo "Starting the Hadoop namenode"
  systemctl start hadoop-namenode > /dev/null 2>&1
  rc=$?
  started=1
fi

if [[ $rc -ne 0 ]]
then
  echo "The Hadoop namenode failed to start"
  exit 1
fi

for dir in $hdfs_dirs $yarn_dirs $mapred_dirs
do
  echo "Creating directory $dir"
  runuser hdfs -s /bin/bash /bin/bash -c "hadoop fs -mkdir -p $dir" > /dev/null 2>&1
done

echo "Setting permissions on /tmp"
runuser hdfs -s /bin/bash /bin/bash -c "hadoop fs -chmod 1777 /tmp" > /dev/null 2>&1

for dir in $mapred_dirs
do
  echo "Setting permissions and ownership for $dir"
  runuser hdfs -s /bin/bash /bin/bash -c "hadoop fs -chown mapred:mapred $dir" > /dev/null 2>&1
  runuser hdfs -s /bin/bash /bin/bash -c "hadoop fs -chmod 1777 $dir" > /dev/null 2>&1
done

for dir in $yarn_dirs
do
  echo "Setting permissions and ownership for $dir"
  runuser hdfs -s /bin/bash /bin/bash -c "hadoop fs -chown yarn:mapred $dir" > /dev/null 2>&1
done

# Stop the namenode if we started it
if [[ $started -gt 0 ]]
then
  echo "Stopping the Hadoop namenode"
  systemctl stop hadoop-namenode > /dev/null 2>&1
fi
