
:
#	Copyright (C) 1998  Moxa Technologies Co., Ltd.
#	All Rights Reserved.
#
#	MOXA Serial I/O Device driver installation script
#	OS = "sco" "svr4.2" "xenix" "solaris"
#
# Version History :
# 	date		author		moment
#	11/01/1995	Jose		wrote it
#	02/27/1996	Victor		modified. install by this shell program
#	07/24/1996	Victor		modified. for C168/C218/C320
#					Ver=2.5 just for China temp used.
#					Ver=3.1 for every body
#	10/08/1996	Victor		Ver=4.0 for C320 Turbo
#	06/17/1997	Victor		Ver=4.2
#	02/03/2000	Jming		Ver=5.3 for C320T/C218T/CP-204J
#	02/15/2001	Jming		Ver=5.4 for fix bugs
#	02/20/2003	George		Ver=5.6 for new C320 Firmware 

################################################################################
# define value and default value setting
################################################################################
TempDir="/tmp/moxa"
DriverVer="5.6"
ProductName="MOXA"
YES=0
NO=1
OS=""
Device=""
EntryPwd=`pwd`

################################################################################
# check_moxa_conflict()
#	to check C360 and C218/C320 conflict install just for SCO
################################################################################
check_moxa_conflict() {
	[ -d /etc/conf/pack.d/moxa ] && {
		[ -d /etc/conf/pack.d/mxc0 ] && {
			echo "\nInstallation fail !\n"
			echo "MOXA-C360 driver already exist !"
			echo "C360 driver and C218/C320 driver cannot be installed simultaneously."
			clean_tempfile
			exit 1
		}
	}
}

################################################################################
# clean_tempfile()
# 	to clean the temp file and $TempDir
################################################################################
clean_tempfile() {
	cd $EntryPwd 
	[ -d $TempDir ] &&
		rm -rf $TempDir > /dev/null 2>&1
	sync ; sync
}

################################################################################
# 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
}
################################################################################
# print_usage()
#	to print the usage message
################################################################################
print_usage() {
	echo "\nCopyright (C) 2001 Moxa Technologies Co., Ltd."
	echo "All Rights Reserved."
	echo "\nMOXA Serial I/O Device driver installation script."
	echo "\nUsage: install [OS name] [device name]"
	echo "\n       OS name		{sco | xenix}"
	echo "\n       device name	source floppy device name"
	echo ""
}

################################################################################
# tar_files()
#	tar the needed file to disk at $TempDir
#	And copy the Driver.o file
################################################################################
tar_files() {
	echo "\nTar files, please wait ...\c"
	[ -d $TempDir ] || mkdir $TempDir > /dev/null 2>&1
	cd $TempDir
	[ "$Device" = "harddisk" ] || {
		tar xvf $Device $TempDir/c320tunx.cod $TempDir/c218tunx.cod $TempDir/cp204unx.cod $TempDir/$OS.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
		} 
	}
	uncompress $TempDir/$OS.tar.Z > /dev/null 2>&1
	tar xvf $TempDir/$OS.tar > /dev/null 2>&1
	echo " O.K."
}

################################################################################
# get_osname()
#	to input the OS name
################################################################################
get_osname() {
###################################################################
	flag=TRUE
	while [ "$flag" = TRUE ]
	do
		echo "\nPlease select following OS type"
		echo "	1. SCO UNIX"
                echo "	2. UNIX SVR4.2"
		echo "	3. XENIX"
		echo "	4. UNIX SVR5 ( UnixWare 7.x )"
		echo "Select : \c"
		read OS
		case $OS in
		[1])
			OS="sco"
			flag=FALSE
			;;
		[2])
			OS="svr4.2"
			flag=FALSE
			;;
		[3])
			OS="xenix"
			flag=FALSE
			;;
		[4])
			OS="svr5"
			flag=FALSE
			;;
		*)
			echo "\nInvalid input ! Please retry."
			;;
		esac
		echo ""
	done
}

################################################################################
# get_devicename()
#	to input the floppy device name
################################################################################
get_devicename() {
	case $OS in
	"sco" | "svr4.2" | "xenix" | "svr5")
		flag=TRUE
		while [ "$flag" = TRUE ]
		do
			echo "\nPlease select following floppy device type"
			echo "	1. /dev/fd0135ds18		(A:1.44M)"
			echo "	2. /dev/fd096ds15		(A:1.2M)"
			echo "	3. /dev/fd1135ds18		(B:1.44M)"
			echo "	4. /dev/fd196ds15		(B:1.2M)"
			echo "	5. Hard Disk			/tmp/moxa" 
			echo "Select : \c"
			read Device
			case $Device in
			[1])
				flag=FALSE
				Device="/dev/fd0135ds18"
				;;
			[2])
				flag=FALSE
				Device="/dev/fd096ds15"
				;;
			[3])
				flag=FALSE
				Device="/dev/fd1135ds18"
				;;
			[4])
				flag=FALSE
				Device="/dev/fd196ds15"
				;;
			[5])
				flag=FALSE
				Device="harddisk"
				;;
			*)
				echo "\nInvalid input ! Please retry."
				;;
			esac
		done
		;;
	"solaris")
		flag=TRUE
		while [ "$flag" = TRUE ]
		do
			echo "\nPlease select follwing floppy device type"
			echo "1.	/dev/fd0	(A floppy)"
			echo "2.	/dev/fd1	(B floppy)"
			echo "3.	Hard Disk	/tmp/moxa"
			echo "Select : \c"
			read Device
			case $Device in
			[1])
				flag=FALSE
				Device="/dev/fd0"
				;;
			[2])
				flag=FALSE
				Device="/dev/fd1"
				;;
			[3])
				flag=FALSE
				Device="harddisk"
				;;
			*)
				echo "\nInvalid input ! Please retry."
				;;
			esac
		done
		;;
	esac
}

################################################################################
# os_info()
#	to display the OS type message
################################################################################
os_info() {
	case $OS in
	"sco")
		echo "\nFor SCO UNIX System V/386 Release 3.2"
		check_moxa_conflict
		;;
	"svr4.2")
		echo "\nFor UNIX System V/386 Release 4.2"
		;;
	"xenix")
		echo "\nFor XENIX"
		;;
	"solaris")
		echo "\nFor Solaris x86 2.x"
		;;
	"svr5")
		echo "\nFor UNIX System Release 5"
		;;
	esac
}

################################################################################
# backup_os()
#	to backup the OS file
################################################################################
backup_os() {
	case $OS in
	"sco")
		[ -f /unix.moxa ] || cp /unix /unix.moxa > /dev/null 2>&1
		;;
	"svr4.2")
		[ -f /stand/unix.moxa ] || cp /stand/unix /stand/unix.moxa > /dev/null 2>&1
		;;
	"svr5")
		[ -f /stand/unix.moxa ] || cp /stand/unix /stand/unix.moxa > /dev/null 2>&1
		;;
	esac
}

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

$ProductName Device Driver Installation (Ver. $DriverVer)
==============================================================================="
if [ "$1" = "sco" -o "$1" = "svr4.2" -o "$1" = "xenix" -o "$1" = "solaris" -o "$1" = "svr5" ]
then
	OS=$1
else
	get_osname
fi
Device=$2
[ "$Device" = "" ] && get_devicename
#
# start the install process
#
os_info
backup_os
trap interrupt_quit2 1 3 9 15			# trap signal
trap interrupt_nothing2 2			# trap signal SIGINT
tar_files					# copy file
./moxainst
[ $? != 0 ] && {
	clean_tempfile				# clear all temp file
	exit 0
}
clean_tempfile					# clear all temp file
exit 0
