#!/bin/sh
:
#	Copyright (C) 2000  MOXA Technologies Co., Ltd.
#	All Rights Reserved.
#
#	MOXA Serial I/O Device driver installation script
#	OS = "sco" "svr4.2" "svr4.5" 
#
# Version History :
# 	date		author		moment
#	01/24/2000	jaron chou	Ver=1.0
#	02/24/2001	jming		Ver=1.1

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

################################################################################
# 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) 2004 MOXA Technologies Co., Ltd."
	echo "All Rights Reserved."
	echo "\nMOXA Device driver installation script."
	echo "\nUsage: install [OS name]"
	echo "\n       OS name		{sco | uw21 | svr4.2_2 | uw7}"
	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/$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 3.2v4, SCO OpenServer 5"
		echo "	2. UnixWare 2.1.x" 
		echo "	3. SCO UnixWare 7"
		echo "Select : \c"
		read OS
		case $OS in
		[1])
			OS="sco"
			flag=FALSE
			;;
		[2])
			OS="uw21"
			flag=FALSE
			;;
		[3])
			OS="uw7"
			flag=FALSE
			;;
		*)
			echo "\nInvalid input ! Please retry."
			;;
		esac
		echo ""
	done
}
################################################################################
# get_devicename()
#	to input the floppy device name
################################################################################
get_devicename() {
	case $OS in
	"sco" | "uw21" | "svr4.2_2" | "uw7")
		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
		;;
	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
		;;
	"uw21")
		echo "\nFor UNIX System V/386 Release 4.2"
		;;
	"svr4.2_2")
		echo "\nFor UNIX System V/386 Release 4.2"
		;;
	"uw7")
		echo "\nFor SCO Unixware 7.x"
		;;
	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			# trap signal
if [ "$#" -ge 3 ]
then
	print_usage
	exit 1
fi
echo "
===============================================================================
Copyright (C) 2004  MOXA Technologies Co., Ltd.
All Rights Reserved.

$ProductName Device Driver Installation (Ver. $DriverVer)
==============================================================================="
if [ "$1" = "sco" -o "$1" = "uw21" -o "$1" = "svr4.2_2" -o "$1" = "uw7" ]
then
	OS=$1
else
	#get_osname
	OS="uw21"
fi
Device=harddisk
#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 
tar_files					# copy file
echo "Press any key to continue..."
read tmp
./moxainst
[ $? != 0 ] && {
	clean_tempfile				# clear all temp file
	exit 0
}
clean_tempfile					# clear all temp file
exit 0
