#!/bin/bash
source install_lang
####### garbage here #############
function mainFunction ( )
{
echo "$Good"
}
function braBraBra ( )
{
echo "$Bra_Bra_Bra"
}
####### garbage end ###############



####### fafa code here ###########

# 參數假設
# /home/nutchuser/NutchEZ_source下有3個檔案
# install.sh, nutch-1.0.tar.gz, apache-tomcat-6.0.18.tar.gz
# 安裝路徑為/opt/nutchEZ

Nutch_install_folder=/opt/nutchEZ
Install_source=/home/nutchuser/NutchEZ_source
NutchEZ_HOME=/opt/nutchEZ
MasterIP=`/sbin/ifconfig eth0 | grep 'inet addr' |  sed 's/^.*addr://g' | sed 's/Bcast.*$//g'`


set_install_information () {
  read -p "Please enter administrator's e-mail address:  " admin_email
  read -p "Please enter the Master DNS:  " MasterDNS
}

show_info () {
  echo "Administrator's e-mail address is $Admin_email."
  echo "The master DNS is: $MasterDNS"
}

confirm_install_information () {
  read -p "Please confirm your install infomation: 1.Yes 2.No  " confirm
}

Install_Nutch () {
  tar zxvf $Install_source/nutch-1.0.tar.gz /opt/
  mv nutch-1.0 nutchEZ
  chown -R nutchuser:nutchuser /opt/nutchEZ
  set_Nutch_conf
}

set_Nutch_conf () {
  set_hadoop-env
  set_haoop-site
  set_nutch-site
  set_crawl-urlfilter
}

# set $NutchEZ_HOME/conf/hadoop-env.sh
set_hadoop-env () {
  echo "set $NutchEZ_HOME/conf/hadoop-env.sh"
  cat >> hadoop-env.sh << EOF
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export HADOOP_HOME=/opt/nutch
export HADOOP_LOG_DIR=/tmp/nutch/logs
export HADOOP_SLAVES=/opt/nutch/conf/slaves
EOF
}

# set $NutchEZ_HOME/conf/hadoop-site.xml
set_hadoop-site () {
  echo "set $NutchEZ_HOME/conf/hadoop-site.xml"
  cat > $NutchEZ_HOME/conf/hadoop-site.xml << EOF
<configuration>
<property>
    <name>fs.default.name</name>
    <value>$MasterDNS:9000</value>
    <description> The name of the default file system. Either the literal string "local" or a host:port for NDFS. </description>
</property>
<property>
    <name>mapred.job.tracker</name>
    <value>$MasterDNS:9001</value>
    <description> The host and port that the MapReduce job tracker runs at. If "local", then jobs are run in-process as a single map and reduce task. </description>
</property>
</configuration>
EOF
}

set_nutch-site () {
  echo "set $NutchEZ_HOME/conf/nutch-site.xml"
  cat > $NutchEZ_HOME/conf/nutch-site.xml << EOF
<configuration>
<property>
  <name>http.agent.name</name>
  <value>nutchuser</value>
  <description>HTTP 'User-Agent' request header. </description>
</property>
<property>
  <name>http.agent.description</name>
  <value>MyTest</value>
  <description>Further description</description>
</property>
<property>
  <name>http.agent.url</name>
  <value>$MasterDNS</value>
  <description>A URL to advertise in the User-Agent header. </description>
</property>
<property>
  <name>$MasterDNS</name>
  <value>$Admin_email</value>
  <description>An email address
  </description>
</property>
</configuration>
EOF
}

###################
# undo... sed or cp
set_crawl-urlfilter () {
  echo "set NutchEZ_HOME/conf/set_crawl-urlfilter.txt"
}

format_HDFS () {
  echo "format HDFS..."
  $NutchEZ_HOME/bin/hadoop namenode -format
}


start_up_NutchEZ (){
  echo "start up NutchEZ..."
  $NutchEZ_HOME/bin/start-all.sh
}

# install tomcat
Install_Tomcat () {
  tar zxvf $Install_source/apache-tomcat-6.0.18.tar.gz $NutchEZ_HOME
  mv $NutchEZ_HOME/apache-tomcat-6.0.18 $NutchEZ_HOME/tomcat
  chown -R nutchuser:nutchuser $NutchEZ_HOME
  mkdir $NutchEZ_HOME/web
  jar -xvf $NutchEZ_HOME/nutch-1.0.war $NutchEZ_HOME/web
  mv $NutchEZ_HOME/tomcat/webapps/ROOT $NutchEZ_HOME/tomcat/webapps/ROOT-ori
  mv $NutchEZ_HOME/web $NutchEZ_HOME/tomcat/webapps/ROOT
  mkdir $NutchEZ_HOME/search
  set_server
  set_nutch-site
}

####################
# undo...sed or cp
set_server () {
  echo "$NutchEZ_HOME/tomcat/conf/server.xml"
}

####################
# undo...sed or cp
set_nutch-site () {
  echo "$NutchEZ_HOME/tomcat/webapps/ROOT/WEB-INF/classes/nutch-site.xml"
}


start_up_tomcat () {
  echo "start up tomcat..."
  $NutchEZ_HOME/tomcat/bin/startup.sh
}
