#!/bin/bash
echo "---- Runninig scripts in root_local-hooks ----"
echo "---- [1] Installing Sun Java JDK 6 ........ ----"
echo "deb http://free.nchc.org.tw/debian lenny non-free" > lenny-non-free.list
mv lenny-non-free.list /etc/apt/sources.list.d/.
apt-get update
cat << EOF | /usr/bin/debconf-set-selections
sun-java6-bin   shared/accepted-sun-dlj-v1-1    select true
sun-java6-jdk   shared/accepted-sun-dlj-v1-1    select true
sun-java6-jre   shared/accepted-sun-dlj-v1-1    select true
EOF
apt-get -y install sun-java6-jdk

echo "---- [2] Installing Hadoop 0.20.2 ........ ----"
cd /opt
{
  if [ ! -x /opt/hadoop ]; then
    wget http://ftp.twaren.net/Unix/Web/apache/hadoop/core/hadoop-0.20.2/hadoop-0.20.2.tar.gz
    tar zxvf hadoop-0.20.2.tar.gz
    rm -f hadoop-0.20.2.tar.gz
    mv hadoop-0.20.2/ hadoop
    chown -R hadoop:hadoop /opt/hadoop
    if [ ! -x /var/hadoop ]; then
      mkdir -p /var/hadoop
      chown -R hadoop:hadoop /var/hadoop
    fi
  fi
}

if [ ! -x /opt/hadoop ]; then
  echo "---- [ERROR] /opt/hadoop is not exist!! ----"; exit;
else
  echo "---- [3] Configure Hadoop NameNode and JobTracker .... ----"
  cd /opt/hadoop
  {
    if [ ! -f /opt/hadoop/conf/hadoop-env.sh ]; then
      echo "---- [ERROR] /opt/hadoop/conf/hadoop-env.sh is not exist!!  ----"; exit
    else
      if [ ! -f /opt/hadoop/conf/hadoop-env.sh.org ]; then
        echo "---- [3.1] Updating /opt/hadoop/conf/hadoop-env.sh ....  ----"
        cp /opt/hadoop/conf/hadoop-env.sh /opt/hadoop/conf/hadoop-env.sh.org
        cat >> conf/hadoop-env.sh << EOF
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export HADOOP_HOME=/opt/hadoop
export HADOOP_CONF_DIR=/opt/hadoop/conf
EOF
      fi
    fi

    if [ ! -f /opt/hadoop/conf/core-site.xml ]; then
      echo "---- [ERROR] /opt/hadoop/conf/core-site.xml is not exist!!  ----"; exit
    else
      if [ ! -f /opt/hadoop/conf/core-site.xml.org ]; then
        echo "---- [3.2] Updating /opt/hadoop/conf/core-site.xml ....  ----"
        cp /opt/hadoop/conf/core-site.xml /opt/hadoop/conf/core-site.xml.org
        cat > conf/core-site.xml << EOF
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>fs.default.name</name>
    <value>hdfs://localhost:9000</value>
  </property>
  <property>
    <name>hadoop.tmp.dir</name>
    <value>/var/hadoop/hadoop-\${user.name}</value>
  </property>
</configuration>
EOF
      fi
    fi

    if [ ! -f /opt/hadoop/conf/hdfs-site.xml ]; then
      echo "---- [ERROR] /opt/hadoop/conf/hdfs-site.xml is not exist!!  ----"; exit
    else
      if [ ! -f /opt/hadoop/conf/hdfs-site.xml.org ]; then
        echo "---- [3.3] Updating /opt/hadoop/conf/hdfs-site.xml ....  ----"
        cp /opt/hadoop/conf/hdfs-site.xml /opt/hadoop/conf/hdfs-site.xml.org
        cat > conf/hdfs-site.xml << EOF
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>dfs.replication</name>
    <value>1</value>
  </property>
</configuration>
EOF
      fi
    fi

    if [ ! -f /opt/hadoop/conf/mapred-site.xml ]; then
      echo "---- [ERROR] /opt/hadoop/conf/mapred-site.xml is not exist!!  ----"; exit
    else
      if [ ! -f /opt/hadoop/conf/mapred-site.xml.org ]; then
        echo "---- [3.3] Updating /opt/hadoop/conf/mapred-site.xml ....  ----"
        cp /opt/hadoop/conf/mapred-site.xml /opt/hadoop/conf/mapred-site.xml.org
        cat > conf/mapred-site.xml << EOF
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>mapred.job.tracker</name>
    <value>localhost:9001</value>
  </property>
</configuration>
EOF
      fi
    fi

    if [ ! -d /var/hadoop/hadoop-root/dfs/name ]; then
      echo "---- [3.4] Formating NameNode ....  ----"
      bin/hadoop namenode -format
    fi
  }
fi
