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

####### fafa code here ###########
User_HOME=/home/nutchuser/nutchez
NutchEZ_HOME=/opt/nutchez
Nutch_HOME=$NutchEZ_HOME/nutch
Tomcat_HOME=$NutchEZ_HOME/tomcat
Index_DB=$User_HOME/search
MasterIP_Address=`/sbin/ifconfig eth0 | grep 'inet addr' |  sed 's/^.*addr://g' | sed 's/Bcast.*$//g' | sed 's/ .*// '`

# DNS暫時以IP取代
MasterDNS=$MasterIP_Address

function set_install_information () { 
  set_nutchuser_passwd
  read -p "Please enter Administrator's e-mail address: " Admin_email
  read -p "Please enter the Master DNS:  " MasterDNS
}

function set_nutchuser_passwd () {
  read -p "Please enter nutchuser's password :  " nutchuser_passwd
  read -p "Please enter nutchuser's password again:  " nutchuser_passwd_confirm
  if [ $nutchuser_passwd != $nutchuser_passwd_confirm ]; then
    set_nutchuser_passwd
  fi
}

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

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

function make_ssh_key () {
  su nutchuser -c 'ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""'
  su nutchuser -c "cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys"
}


function set_haoop-site () {
  echo "set $Nutch_HOME/conf/hadoop-site.xml"
  cd $Nutch_HOME/conf/
  cat > hadoop-site.xml << EOF
<configuration>
  <property>
    <name>fs.default.name</name>
    <value>hdfs://$MasterIP_Address:9000</value>
  </property>
  <property>
    <name>mapred.job.tracker</name>
    <value>$MasterIP_Address:9001</value>
  </property>
  <property>
    <name>hadoop.tmp.dir</name>
    <value>/var/nutchez/nutch-nutchuser</value>
  </property>
</configuration>
EOF
}

# 修改nutch-site.xml中-http.agent.url, http.agent.email
function set_nutch-site () {
  echo "set $Nutch_HOME/conf/nutch-site.xml"
  Line_NO=`cat $Nutch_HOME'/conf/nutch-site.xml' | grep -n 'http.agent.url' | sed 's/:.*//g'`
  echo "debug...http.agent.url line number = $Line_NO..."
  sed -i ''$((Line_NO+1))'d' $Nutch_HOME/conf/nutch-site.xml
  echo "debug...edit http.agent.url delete line $((Line_NO+1))..."
  sed -i ''$Line_NO'a <value>'$MasterIP_Address'</value>' $Nutch_HOME/conf/nutch-site.xml
  echo "debug...edit http.agent.url done..."

  Line_NO=`cat $Nutch_HOME'/conf/nutch-site.xml' | grep -n 'http.agent.email' | sed 's/:.*//g'`
  echo "debug...http.agent.email line number = $Line_NO..."

  sed -i ''$((Line_NO+1))'d' $Nutch_HOME/conf/nutch-site.xml
  echo "debug...edit http.agent.email delete line $((Line_NO+1))..."
  sed -i ''$Line_NO'a <value>'$Admin_email'</value>' $Nutch_HOME/conf/nutch-site.xml
  echo "debug...edit http.agent.email done..."
}

function format_HDFS () {
  echo "format HDFS..."
  su nutchuser -c "$Nutch_HOME/bin/hadoop namenode -format"
}

function start_up_NutchEZ (){
  echo "start up NutchEZ..."
  su nutchuser -c "$Nutch_HOME/bin/start-all.sh"
}

function Install_Nutch () {
  set_haoop-site
  set_nutch-site
}


function client_PassMasterIPAddr () {
  Line_NO=`cat client_install | grep -n '# Master IP here' | sed 's/:.*//g'`
  echo "debug...Master IP here line number = $Line_NO..."
  sed -i ''$((Line_NO+1))'d' client_install
  echo "debug...edit Master IP at line $((Line_NO+1))..."
  sed -i ''$Line_NO'a Master_IP_Address='$MasterIP_Address'' client_install
  echo "edit client_install done..."
}

function make_client_install () {
  # 將Master_IP_Address給client
  # 打包安裝目錄(不含tomcat)
  echo "function make_client_install..."
  client_PassMasterIPAddr
  tar -cvzf /opt/nutchez/NutchezForClientOf_$Master_IP_Address.tar.gz  /opt/nutchez/nutch
}

function start_up_tomcat () {
  echo "start up tomcat..."
  sleep 10s
  echo "su nutchuser -c "$Tomcat_HOME/bin/startup.sh""
  su nutchuser -c "$Tomcat_HOME/bin/startup.sh"
}
