#!/bin/sh
# Program
#        Install Supermicro Thin-agent

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
prog="Thin-Agent Service"
directory="/opt/supermicro"
tas_dir="/etc/rc.d"
ret_code=0
ret=""
force=$1

smarttoolFail()
{
    #echo "install smartmontools....."
    while true; do
        if [ "$force" == "-y" ]; then
			yn="Y"
		else
            read -p "To enable SMART HDD information, please install smartmontools package [y/n] " yn
        fi
		
		case $yn in
			[Yy]* )                
				pkg install -y smartmontools
				ret=`pkg info smartmontools`
				ret_code=$?
				[ $ret_code == 0 ] || fail
				break;;
			[Nn]* ) break;;
			* ) echo "Please answer yes or no.";
		esac
    done
}

libconfigFail()
{
	pkg install -y libconfig
	ret=`pkg info libconfig`
	ret_code=$?
	[ $ret_code == 0 ] || fail
}

pciutilsFail()
{
	pkg install -y pciutils
	ret=`pkg info pciutils`
	ret_code=$?
	[ $ret_code == 0 ] || fail
}

fail()
{
    echo "install fail"
    exit 1
}

IPMITAS_missing()
{
    echo "tas-freebsd not found"
    fail
}

tas_missing()
{
    echo "tas not found"
    fail
}

watchtas_missing()
{
    echo "watchtas not found"
    fail
}

watchtas_exe_missing()
{
    echo "watchtas_exe not found"
    fail
}

storcli_missing()
{
    echo "storcli not found"
}

sas3ircu_missing()
{
    echo "sas3ircu not found"
}

copyFile()
{
    cp -rf IPMITAS "$directory"
    cp -rf tas "$tas_dir"
    cp -rf watchtas "$tas_dir"
    cp -rf watchtas_exe "$directory"
    cp -rf storcli /usr/local/bin
    cp -rf sas3ircu /usr/local/bin
    cp -rf license.txt "$directory"
    # echo ""
    # echo "*****StorCLI License start*****"
    # cat $directory/license.txt
    # echo "*****StorCLI License end*****"
}

checkTool()
{
    ret=`pkg info | grep -i smartmontools`
    ret_code=$?
    [ $ret_code == 0 ] || smarttoolFail
    ret=`pkg info | grep -i libconfig`
    ret_code=$? 
    [ $ret_code == 0 ] || libconfigFail
    ret=`pkg info | grep -i pciutils`
    ret_code=$?
    [ $ret_code == 0 ] || pciutilsFail
}

check()
{
    [ -f IPMITAS ] || IPMITAS_missing
    [ -f tas ] || tas_missing
    [ -f watchtas ] || watchtas_missing
    [ -f watchtas_exe ] || watchtas_exe_missing
    [ -f storcli ] || storcli_missing
	[ -f sas3ircu ] || sas3ircu_missing
	
    if [ -x IPMITAS ]; then
        if [ -x tas ]; then
            if [ -x watchtas ] && [ -x watchtas_exe ]; then
                checkTool                
            else
                echo "watchtas not executable"
                fail
            fi   
        else
            echo "tas not executable"
            fail
        fi       
    else
       echo "IPMITAS not executable"
       fail
    fi
}

startup()
{
    egrep -i -q "tas_enable" /etc/rc.conf
	
    if [ $? -ne 0 ]
    then
        echo 'tas_enable="YES"' >> /etc/rc.conf 
    fi
    egrep -i -q "watchtas_enable" /etc/rc.conf
	
    if [ $? -ne 0 ]
    then
        echo 'watchtas_enable="YES"' >> /etc/rc.conf
    fi
    egrep -i -q "smartd_enable" /etc/rc.conf
	
    if [ $? -ne 0 ]
    then
        echo 'smartd_enable="YES"' >> /etc/rc.conf
    fi
}

echo "Install" $prog
check

if [ -d "$directory" ]
then
   copyFile
else
    mkdir "/opt"
    mkdir "$directory"
    copyFile
fi

startup
echo "Install success..."

killall IPMITAS 2> /dev/null
if [ $? != 0 ]; then
    pkill -9 -x IPMITAS
fi

test -f /var/run/watchtas.pid
if [ $? -ne 0 ]; then
  watch=`ps -x | grep watchtas | grep -v grep | awk '{print $1}'`
  if [ $? -eq 0 ]; then
    for i in $watch
      do
        kill $i
      done
  fi
else
  kill `cat /var/run/watchtas.pid`
fi

sleep 3 
$tas_dir/tas start
