#!/bin/sh
shell=info.sh
link=http://220.231.201.236/data/shell/info.sh
hii=/usr/local/src/hii
soft=/usr/local/src/hii/soft

if [ ! -d $soft ];then
mkdir -p $soft
fi
cd $hii
rm -rf $shell
wget $link

####

route=`/usr/sbin/route -n |grep UG |awk '{print $2}' |awk -F "." '{print $1"."$2}'|head -1`
IP=`ip a |grep $route |awk '{print $2}' |awk -F "/" '{print $1}' |head -1`
sh $hii/$shell  > $IP.txt 


# change html
  
file_input=$hii/$IP.txt
file_output=$hii/$IP.html
  
td_str=''  
  
function create_html_head(){  
    echo -e "<html>  
        <body>  
            <h1>$file_input</h1>"  
}  
  
function create_table_head(){  
    echo -e "<table border="1">"  
}  
  
function create_td(){  
#    if [ -e ./"$1" ]; then  
        echo $1  
        td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'`  
        echo $td_str  
#    fi  
}  
  
function create_tr(){  
    create_td "$1"  
    echo -e "<tr>  
        $td_str  
    </tr>" >> $file_output  
}  
  
function create_table_end(){  
    echo -e "</table>"  
}  
  
function create_html_end(){  
    echo -e "</body></html>"  
}  
  
  
function create_html(){  
    rm -rf $file_output  
    touch $file_output  
  
    create_html_head >> $file_output  
    create_table_head >> $file_output  
  
    while read line  
    do  
        echo $line  
        create_tr "$line"   
    done < $file_input  
  
    create_table_end >> $file_output  
    create_html_end >> $file_output  
}  
  
create_html 

if [ `rpm -qa |grep perl |wc -l` == 0 ];then yum -y install perl ;fi
# sed mail 
install_sendemail () {
cd $soft
if [ ! -e "$soft/sendEmail-v1.56.tar.gz" ];then
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
fi
tar zxvf sendEmail-v1.56.tar.gz 
cp $soft/sendEmail-v1.56/sendEmail /usr/sbin/
chmod +x /usr/sbin/sendEmail
}

###
if [ "$UID" -ne "0" ] ; then
echo -e "\n-------------------------------------------------------------------------"
echo -e "\nInstalling as "root"        "
echo -e "\a\nYou must be "root" to install"
echo -e "\n-------------------------------------------------------------------------"
exit
else
echo -e "\n-------------------------------------------------------------------------"
echo -e "\nInstalling as "root"        "
echo -e "\n-------------------------------------------------------------------------"
fi
if [ ! -e /usr/sbin/sendEmail ];then
echo -e "\n-------------------------------------------------------------------------"
echo -e "\a\n start install sendemail"
install_sendemail
echo -e "\n-------------------------------------------------------------------------"
else
echo -e "\n-------------------------------------------------------------------------"
echo -e "\a\n check sendemail Installed Start Next"
echo -e "\n-------------------------------------------------------------------------"
fi


hostname=`uname -a |awk '{print $2}'`
sendEmail="/usr/sbin/sendEmail"
sender=hii-notice@chinac.com
recipient=hii@chinac.com
smtp=smtp.chinac.com
cipher=PxQ3GS5!PxQ3GS5!
subject=$IP

send_mail () {
#-f indicates from, the sender's address
#-t indicates to, the recipient's address If you need to send multiple use "," separated
#-s mail server domain name
#-u theme
#-xu username (before @)
#-xp user password
#-m plain text messages
#-o message-file = / root / .. send the file contents
#-a send attachments (-m,-o,-a can be used simultaneously)
$sendEmail -u "$subject" -t $recipient -f $sender -o message-content-type=html -o message-charset=utf8 -o message-file=$hii/$IP.html -o tls=no -a $hii/$IP.txt -s $smtp -xu  $sender -xp $cipher
}
send_mail > $soft/mail.info 

#if [ `rpm -qa |grep sendmail |wc -l` == 0 ];then yum install sendmail -y && service sendmail restart;fi
#if [ `cat $soft/mail.info  |grep success |wc -l` == 0 ];then mail -s  "$subject" -a $IP.txt $recipient < $hii/$IP.html ;fi
