#!/bin/sh
###############################################################################
# Program:
# 		MOXA Linux driver installer
#	
# History:
# 2009/09/11
#
# Author:
# Ken Huang, ken.huang@moxa.com
#
# Discription:
# The script install and load the MOXA Linux driver.
#
###############################################################################

TARGET_DRIVER="mxuport"
MXVER=`awk '{if($1=="Version" && $2=="Number:"){print $3}}' VERSION.TXT`
RULES=$(pwd)/driver/91-mxuport-naming.rules
MXINSTALL_PATH=$(pwd)
KVER=`uname -r`
BUILT_IN_PATH="/lib/modules/${KVER}/kernel/drivers/usb/serial/"
PARAM=""

DISTRI=$(grep "^ID=" /etc/os-release | cut -d '=' -f 2- | tr -d '"')
K_RELEASE=$(uname -r)
K_SUFFIX=$(echo "$K_RELEASE" | cut -d '-' -f 2)
DISTRI_VERSION=$(echo "$K_SUFFIX" | grep -o -E '[0-9]+' | head -n 1)

# If there's no distribution version, set 0
if [ "$DISTRI_VERSION" = "" ]; then
    DISTRI_VERSION=0
fi

for DRIVER in $TARGET_DRIVER
do
	if lsmod | grep -qw $DRIVER ; then
		rm -f tmp1 tmp2
		echo "Found driver in system..."
		echo "Unloading driver..."
		rmmod $DRIVER > tmp1 2>&1
		if [ -s tmp1 ]
		then
			cat tmp1
			grep "is in use" tmp1 > tmp2 2>&1
			if [ -s tmp2 ]
			then
				rm -f tmp1 tmp2
				exit 1
			fi
			rm -f tmp2
		fi
		rm -f tmp1
		echo "Unload driver successfully."
		echo ""
	fi
done

echo
echo 'Linux assigns the tty port number by the sequence of discovery order starting from ttyUSB0.'
echo 'COM Preserver function can bind static symbolic name and settings to serial ports on UPort.'
echo 'You may be required to configure the UPort after installation.'
echo 'For more information, please read the README.TXT.'
echo 'Do you want to enable COM Preserver function? [y/N].'

read check
case $check in
[y])
	FIXED_PORT=y
	;;
[Y])
	FIXED_PORT=y
	;;
*)
	FIXED_PORT=n
    ;;
esac

grep -v SYMLINK $RULES > tmp.rules

if [ "$FIXED_PORT" = "y" ]
then
	echo
	echo 'Enter the prefix name of symbolic tty name: [ttyMXUSB]'

	read PREFIX
	if [ "$PREFIX" = "" ]
	then
		PREFIX=ttyMXUSB
	fi

	echo "ENV{DEVICE_ID}==\"110a:mxuport\", SUBSYSTEMS==\"usb-serial\", KERNEL==\"ttyUSB*\", SYMLINK+=\"$PREFIX%s{bComPreserver_com}\", MODE=\"0660\"" >> tmp.rules
        PARAM="kflags=-DAUTO_COM_PRESERVER"

else
	echo "ENV{DEVICE_ID}==\"110a:mxuport\", SUBSYSTEMS==\"usb-serial\", KERNEL==\"ttyUSB*\", SYMLINK+=\"ttyMXUSB%n\", MODE=\"0660\"" >> tmp.rules
fi

rm -f $RULES > /dev/null 2>&1
mv tmp.rules $RULES > /dev/null 2>&1

###############################################################################
# try to avoid driver conflict with build-in module
##############################################################################
cd ${BUILT_IN_PATH}
for file in mxuport.ko.*;
do
	if [ -f "$file" ]
	then
		backup_file="${file}.bak"
		mv "$file" "$backup_file"
	fi
done
echo ${MXINSTALL_PATH}
cd ${MXINSTALL_PATH}

if [ "$PARAM" = "" ] ; then
    PARAM="kflags="
fi

case $DISTRI in
    "rhel" | "rocky" | "centos" | "almalinux")
	PARAM="$PARAM -DREDHAT_BASE"
	;;
esac

PARAM="$PARAM -DDISTRI_VERSION=$DISTRI_VERSION"

if make install "$PARAM" ; then
	echo ""
	echo "Loading driver..."
	modprobe $TARGET_DRIVER
	echo "************************************************************************"
	echo " MOXA UPort 1200/1400/1600 series driver ver $MXVER loaded successfully."
	echo "************************************************************************"
fi
