##########################################################################
#   Copyright (C) 2003  Moxa Technologies Co., Ltd.
#   All Rights Reserved.
#
#
# Version History :
#   date        author      comment
#   12/04/2003  James       first version
#   07/01/2004  James       compatible with realtty V1
#   08/12/2005  Jared       porting for UC installation
##########################################################################

################################################################################
# define value and default value setting
################################################################################
TempDir="."
NPPATH="/usr/lib/npreal2"
ProductName="MOXA NPort Server Real TTY Driver V2"
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 () {
     sync;
}

########################################################
#
# 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) 2005  Moxa Technologies Co., Ltd.
All Rights Reserved.

$ProductName Installation
===============================================================================
"

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

########################################################
# install gawk to /usr/sbin/
########################################################
if [ ! -f /usr/sbin/gawk ]
then
	echo -n "Install gawk ... "
	cp ./gawk /usr/sbin/gawk
	chmod 755 /usr/sbin/gawk
	ln -sf /usr/sbin/gawk /bin/awk
	echo "OK!"
fi
########################################################
# 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 "$1)}'
ps -ef | grep npreal2d | awk '$0 !~ /grep/ {system("kill -9 "$1)}'
lsmod | grep npreal2 |awk '$0 != "" {system("rmmod npreal2")}'

########################################################
# Process File
########################################################

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 $TempDir/npreal2d.cf $driverPath

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

########################################################
# install driver
########################################################
echo -n "Copying driver files ... "
#cp -f $TempDir/killp       $driverPath
cp -f $TempDir/npreal2d    $driverPath
#cp -f $TempDir/$MX_MOD.o   $driverPath
#cp -f $TempDir/npreal2d.cf $driverPath/config
cp -f $TempDir/mxaddsvr    $driverPath
cp -f $TempDir/mxdelsvr    $driverPath
cp -f $TempDir/mxcfmat     $driverPath
cp -f $TempDir/mxloadsvr   $driverPath
cp -f $TempDir/mxmknod     $driverPath
cp -f $TempDir/mxrmnod     $driverPath
cp -f $TempDir/mxuninst    $driverPath
#cp -f $TempDir/README.TXT  $NPPATH
#cp -f $TempDir/VERSION.TXT $NPPATH
echo "OK!"

### module ###
cp -f $TempDir/$MX_MOD.o /lib/modules/`uname -r`/kernel/drivers/serial

### web ###
# cp -f ./web/npreal2.cgi /usr/www/cgi-bin/

########################################################
# setting the npreal2 when the system booting
########################################################
cp ./npreal2 /etc/init.d/npreal2
chmod 755 /etc/init.d/npreal2
ln -s /etc/init.d/npreal2 /etc/rc.d/rc3.d/S97npreal2
ln -s /etc/init.d/npreal2 /etc/rc.d/rc6.d/K97npreal2

touch /tmp/nprtmp_profile
grep -v "export PATH=$PATH:/usr/lib/npreal2/driver" /etc/profile > /tmp/nprtmp_profile
echo 'export PATH=$PATH:/usr/lib/npreal2/driver' >> /tmp/nprtmp_profile 2>&1
mv -f /tmp/nprtmp_profile /etc/profile 2>&1
export PATH=$PATH:/usr/lib/npreal2/driver

cd $NPPATH/driver
./mxloadsvr module

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.
===============================================================================
"
