#!/bin/sh

platform=NONE
spec_os=NONE

echo "installing..."

curr_path=$(pwd)

BIN_DIR=/usr/local/bin
touch_path=/opt/.touch_panel

ret=$(file /bin/bash 2>/dev/null | grep -ic 'ARM')

if [ "$ret" = "1" ]; then
	ret=$(file /bin/bash 2>/dev/null | grep -ic 'aarch64')
	
	if [ "$ret" = "1" ]; then
		platform=aarch64
	else
		ret=$(file /bin/bash 2>/dev/null | grep -ic 'armhf')
		
		if [ "$ret" = "1" ]; then
			platform=armhf
		fi
	fi
else
	ret=$(file /bin/bash 2>/dev/null | grep -ic '32-bit')

	if [ "$ret" = "1" ]; then
		platform=i686
	else
		platform=x86_64
	fi
fi

echo "platform : $platform"

if [ "$platform" = "NONE" ]; then
	file /bin/bash
	echo "This platform is not supported!!"
fi

installer_path=$curr_path/linux-touch-driver
src_dir=$installer_path/$platform

if [ -d $installer_path ]; then
	rm -rf $installer_path
fi

tar xmzf linux-touch-driver.tgz > tar.log

if [ ! -f $src_dir/TouchConfig ]; then
	echo "This platform is not supported. [$platform]"
	exit 0
fi

chmod 777 $src_dir/*

remove_drv()
{
	cd $curr_path
	sh remove.sh update
}

if [ -f $touch_path/.config ]; then
	remove_drv
else
	paths="lib"

	if [ "$platform" = "x86_64" ]; then
		paths="lib lib64"
	fi

	for path in $paths; do
		uninstall=0
		
		for so in risintech_drv.so xfhiddrv_drv.so; do
			dir=/usr/$path/xorg/modules/input
			
			if [ -f $dir/$so ]; then
				uninstall=1
				break
			fi
		done
		
		if [ "$uninstall" = "1" ]; then
			if [ -f $touch_path/.config ]; then
				if [ -f $touch_path/uin ] || [ -f $touch_path/nuin ]; then
					remove_drv
					break
				fi
			fi	
			
			echo "You Have already installed a driver,"
			echo "If you want to install the new driver,"
			echo "Please uninstall the old one first."
			exit 0
		fi
	done
fi

if [ ! -d $touch_path/lang ]; then
	mkdir -p $touch_path/lang
	chmod 777 $touch_path
	chmod 777 $touch_path/lang	
fi

cp -f $curr_path/TouchUtil.ini $touch_path/TouchUtil.ini
cp -f $installer_path/conf/config $touch_path/.config

if [ ! -d /usr/local/share/touchtool/ui ]; then
	mkdir -p /usr/local/share/touchtool/ui
fi
#-----------------------------------------------------------------------
$src_dir/TouchConfig --install r "$src_dir" "$installer_path/conf" "$spec_os"

installed=$?

if [ "$installed" != "0" ]; then
	remove_drv
	exit 0
fi

if [ -f $BIN_DIR/bcomp ]; then
	rm -f $BIN_DIR/bcomp
fi

cp -f $src_dir/closedev $BIN_DIR

echo "Setup completed, please restart system to enable touch panel."

exit 1

