##########################################################################
#   Copyright (C) 2003-2012  Moxa Inc.
#   All Rights Reserved.
#
#
# Version History :
#   date        author      comment
#   12/04/2003  James       first version
#   07/01/2004  James       compatible with Real TTY V1
##########################################################################

################################################################################
# define value and default value setting
################################################################################
DirTmp=$0
DirTmp=${DirTmp%/mxinst}
TempDir=$DirTmp

NPPATH="/usr/lib/npreal2"
ProductName="MOXA NPort Server Real TTY Driver V1.18"
OS="linux"
Device=""
check=""
EntryPwd=`pwd`

driverPath="/usr/lib/npreal2/driver"
MX_MOD=npreal2


################################################################################
# interrupt_quit1()
#   to quit install by interrupt or quit
################################################################################
interrupt_quit1() {
    echo "\nInstallation aborted by INTERRUPT !"
    exit 1
}
################################################################################
# interrupt_quit2()
#   to quit install by interrupt or quit
################################################################################
interrupt_quit2() {
    echo "\nInstallation aborted by INTERRUPT !"
    clean_tempfile
    exit 1
}

interrupt_nothing1() {
    exit 1
}

interrupt_nothing2() {
    clean_tempfile
    exit 1
}

################################################################################
# clean_tempfile()
################################################################################
#clean_tempfile () {
#    [ -d $TempDir ] && {
#        rm -rf $TempDir > /dev/null 2>&1
#    }
#    [ -d $NPPATH ] && {
#        rm -rf $NPPATH > /dev/null 2>&1
#    }
#    sync; sync
#}

clean_tempfile () {
     sync;
}
################################################################################
# get_OsName()
#   get the OS name.
################################################################################
get_OsName() {
    if [ -f /etc/redhat-release ]
    then
        OS="linux"
    elif [ -f /etc/SuSE-release ]
    then
        OS="SuSE"
    elif [ -f /etc/gentoo-release ]
    then
        OS="gentoo"
    elif [ -f /etc/debian_version ]
    then
        OS="debian"
    fi
}


################################################################################
# tar_files()
#   tar the needed file to disk at $TempDir
#   And copy the Driver.o file
################################################################################
tar_files() {

    echo -n "Tar files, please wait ..."
    [ -d $TempDir ] || mkdir $TempDir > /dev/null 2>&1
    cd $TempDir
    #[ "$Device" = "harddisk" ] || {
    #    tar xvf $Device $TempDir/linux.tar.Z > /dev/null 2>&1
    #    [ $? != 0 ] && {
    #        [ -d $TempDir ] && rm -rf $TempDir > /dev/null 2>&1
    #        echo "\nDevice name error ! It cannot tar file !"
    #        exit 1
    #    } 
    #}

    #gunzip $TempDir/linux.tar.Z > /dev/null 2>&1
    #tar xvf $TempDir/linux.tar > /dev/null 2>&1
    tar xvfz $TempDir/linux.tgz > /dev/null 2>&1

    #rm -rf linux.tar
    echo " OK!"
}

################################################################################
# chk_libssl()
#   Check /usr/lib/libssl.so.x
#   and link to /tmp/moxa/libsll.so 
################################################################################
chk_libssl() {
	sslpath=$(find /usr/lib64 -name "libssl.so*" 2> /dev/null)
	
    if [ -z "$sslpath" ]
	then
		sslpath=$(find /usr/lib -name "libssl.so*" 2> /dev/null) 
        if [ -z "$sslpath" ]
        then
        	echo
        	echo 'Can not find libssl.so file in your system,'
        	echo 'Please install openssl package if you want to use'
        	echo 'security function.'
        	exit 0
       	else
       		ls /usr/lib | grep libssl.so* | head -1 |
       		awk '{system("ln -s /usr/lib/"$0 " libssl.so")}'
       	fi
    else 
       	ls /usr/lib64 | grep libssl.so* | head -1 |
       	awk '{system("ln -s /usr/lib64/"$0 " libssl.so")}' 
    fi 
}

########################################################
#
# Main procedures
# To read the shell program input argument and process it.
#
########################################################
trap interrupt_quit1 1 3 9 15           # trap signal
trap interrupt_nothing1 2

if [ "$#" -ge 3 ]
then
    print_usage
    exit 1
fi

echo "
===============================================================================
Copyright (C) 2002-2012  Moxa Inc.
All Rights Reserved.

$ProductName Installation.
System Imformation: Kernel `uname -r`; Machine `uname -m`.
===============================================================================
"

 

get_OsName

if [ "$OS" = "debian" ] 
then
    touch /etc/init.d/npreals
fi

trap interrupt_quit2 1 3 9 15       # trap signal
trap interrupt_nothing2 2           # trap signal SIGINT


########################################################
# check configuration file. (npreal2d.cf)
########################################################
if [ -f /usr/lib/npreal2/driver/npreal2d.cf ]
then
    echo 'There is an Real TTY Driver installed in your system.'
    echo 'Continue to install will overwrite the old driver.'
    echo 'Do you want to continue installing? [y/N].'

    read check
    case $check in
    [n])
        exit 0
        ;;
    [N])
        exit 0
        ;;
    [y])
        ;;
    [Y])
        ;;
    *)
        exit 0
        ;;
    esac
fi

########################################################
# rmmove module
########################################################
ps -ef | grep npreal2d | awk '$0 !~ /grep/ {system("kill -15 "$2)}'
ps -ef | grep npreal2d | awk '$0 !~ /grep/ {system("kill -9 "$2)}'
lsmod | grep npreal2 |
awk '$0 != "" {system("rmmod npreal2")}'


########################################################
# install driver
########################################################
echo ''
tar_files

echo "Building driver..."


mkdir -p /lib/modules/`uname -r`/kernel/drivers/char > /dev/null 2>&1
mkdir -p /lib/modules/`uname -r`/misc > /dev/null 2>&1

echo
echo 'If you want to use secure communication with target,'
echo 'you might choose [y] to enable the SSL function.'
echo 'Note: This function support RealCOM with secure mode only.'
echo 'Do you want to enable secure function? [y/N].'

read check
case $check in
[n])
    if [ "$1" = "SP1" ]
    then
        make $1 PATH1=$(pwd)
    else
        make all PATH1=$(pwd)
    fi 
    ;;
[N])
    if [ "$1" = "SP1" ]
    then
        make $1 PATH1=$(pwd)
    else
        make all PATH1=$(pwd)
    fi 
    ;;
[y])
	if [ ! -f "$TempDir/libssl.so" ]
	then
		chk_libssl
	fi

	if [ "$1" = "SP1" ]
	then
		make SP1_ssl PATH1=$(pwd)
	elif [ "$1" = "m64" ]
	then
		make ssl64 PATH1=$(pwd)
	elif [ "$1" = "mppc64" ]
	then
		make ppc64 PATH1=$(pwd)
	elif [ "$1" = "SP1_m64" ]
	then
		make SP1_ssl64 PATH1=$(pwd)
	else
		make ssl PATH1=$(pwd)
	fi 
    ;;
[Y])
	if [ ! -f "TempDir/libssl.so" ]
	then
		chk_libssl
	fi

	if [ "$1" = "SP1" ]
	then
		make SP1_ssl PATH1=$(pwd)
	elif [ "$1" = "m64" ]
	then
		make ssl64 PATH1=$(pwd)
	elif [ "$1" = "mppc64" ]
	then
		make ppc64 PATH1=$(pwd)
	elif [ "$1" = "SP1_m64" ]
	then
		make SP1_ssl64 PATH1=$(pwd)
	else
		make ssl PATH1=$(pwd)
	fi 
    ;;
*)
    make $1 PATH1=$(pwd)
    ;;
esac

echo "Check Driver..."
[ -e $MX_MOD.o -a -e npreal2d.o -a -e npreal2d_redund ] || {
    echo "FAILED !!!"
    echo " "
    echo "Install Not Completed !"
    echo " "
    clean_tempfile
    exit 0
}


########################################################
# Process File
########################################################
#$TempDir/npfile

flag=0

mkdir -p $NPPATH > /dev/null 2>&1
mkdir -p $driverPath > /dev/null 2>&1


[ -f /usr/lib/npreal2/driver/npreal2d.cf ] || {
    echo -n "Copying configurations files ... "
    cp -f $(pwd)/npreal2d.cf $driverPath

    flag=$?
    [ $flag = 0 ] ||    {
        echo "FAILED! ($flag)"
        exit 0
    }
    echo "OK!"
}


### driver ###
NowPath=$(pwd)
echo -n "Copying driver files ... "
cp -f $NowPath/killp       $driverPath
cp -f $NowPath/npreal2d_redund    $driverPath
cp -f $NowPath/npreal2d    $driverPath
#cp -f $NowPath/$MX_MOD   $driverPath
cp -f $NowPath/Makefile    $driverPath
cp -f $NowPath/npreal2d.c  $driverPath
cp -f $NowPath/npreal2.h  $driverPath
cp -f $NowPath/npreal2d.cf $driverPath/config
cp -f $NowPath/npreal2.c   $driverPath
#cp -f $NowPath/linux.mak   $driverPath
cp -f $NowPath/mxaddsvr    $driverPath
cp -f $NowPath/mxdelsvr    $driverPath
cp -f $NowPath/mxcfmat     $driverPath
cp -f $NowPath/mxloadsvr   $driverPath
cp -f $NowPath/mxmknod     $driverPath
cp -f $NowPath/mxrmnod     $driverPath
cp -f $NowPath/mxuninst    $driverPath
cp -f $NowPath/mxsetsec    $driverPath
cp -f $NowPath/README.TXT  $NPPATH
cp -f $NowPath/VERSION.TXT $NPPATH
cp -f $NowPath/kernel2.6.txt $NPPATH
echo "OK!"



### module ###
#cp -f $driverPath/npreal2.o /lib/modules/`uname -r`/kernel/drivers/char
#cp -f $driverPath/npreal2.o /lib/modules/`uname -r`/misc


ps -ef | grep npreal2 |
awk '$0 !~ /grep/ {system("kill -15 "$2)}'
ps -ef | grep npreal2 |
awk '$0 !~ /grep/ {system("kill -9 "$2)}'





########################################################
# load module
########################################################
echo "Load driver..."
modprobe $MX_MOD 
[ "$?" = "0" ] || {
    echo ""
    echo "Failed!!!  please contact the provider"
    echo ""
    exit 0
}
echo "OK!"


########################################################
# Delete the line with npreal2 and append
# "modprobe npreal2" in /etc/rc.d/rc.local.
########################################################


cd $NPPATH/driver
./mxloadsvr install
echo ''

echo "
===============================================================================
Installation process is completed.
The all driver files are installed on /usr/lib/npreal2/driver.
Now you can "cd /usr/lib/npreal2/driver" and run "./mxaddsvr" to add tty port.
===============================================================================
"

#[ -d $TempDir ] && {
#    rm -rf $TempDir > /dev/null 2>&1
#}

