#!/bin/sh

mode="$1"

boot=0

BIN_DIR=/usr/local/bin

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

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

module_path=lib

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

curr_path=$(pwd)
installer_path=$curr_path/linux-touch-driver
src_dir=$installer_path/$platform

if [ ! -d $installer_path ]; then
	tar xmzf linux-touch-driver.tgz > tar.log
fi

echo "close proccess"

if [ -f $BIN_DIR/closedev ]; then
	$BIN_DIR/closedev
else
	if [ -f $src_dir/closedev ]; then
		$src_dir/closedev
	fi
fi

paths="lib"

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

its_old_ver()
{
	if [ -f /opt/.touch_panel/uin ] || [ -f /opt/.touch_panel/nuin ]; then
		echo "0"
		return		
	fi

	libs="librisintp libtpinfo libtpdrv libPct libtouchapi libLinear"
	
	for path in $paths; do
		for lib in $libs
		do
			dir=/usr/$path
			
			if [ -f $dir/$lib.so.1.0 ]; then
				echo "1"
				return
			fi
		done
	done
	
	echo "0"
}
	
if [ "$mode" != "update" ]; then
	ret=$(its_old_ver)
	
	if [ "$ret" = "1" ]; then
		_in=c
		
		echo "Removing this driver will automatically"
		echo "restart of the operating system,"
		echo "Do you want to continue?"
		read -p "(y/n): " _in
		
		if [ "$_in" = "n" ]; then
			echo "exit"
			exit 1
		fi
		
		if [ "$_in" = "y" ]; then
			boot=1
		fi
	fi
fi

#-----------------------------------------------------------------------
#delete service
ret=$($src_dir/TouchConfig --flag services)

echo "services type:$ret"

if [ "$ret" = "1" ]; then
	systemd_paths="lib etc"
	
	for path in $systemd_paths; do
		if [ -f /$path/systemd/system/tpservice.service ]; then
			systemctl stop tpservice.service 2>/dev/null	
			systemctl disable tpservice.service 2>/dev/null
			rm -f /$path/systemd/system/tpservice.service
			break
		fi
	done
else
	if [ -f /etc/init.d/tpservice ]; then
		if [ -f /sbin/chkconfig ]; then
			chkconfig tpservice off 2>/dev/null
			
			if [ "$ret" = "2" ]; then
				chkconfig tpservice stop 2>/dev/null
			fi
			
			chkconfig --del tpservice 2>/dev/null
		else
			service tpservice stop 2>/dev/null	
			update-rc.d -f tpservice remove 2>/dev/null
		fi
		
		rm -f /etc/init.d/tpservice
		
		if [ -f /etc/default/tpservice ]; then
			rm -f /etc/default/tpservice
		fi
	fi
fi

#-----------------------------------------------------------------------
#remove rtouch
if [ -f /lib/modules/$(uname -r)/modules.dep.old ]; then
	mv -f /lib/modules/$(uname -r)/modules.dep.old /lib/modules/$(uname -r)/modules.dep
fi

if [ -f /lib/modules/$(uname -r)/kernel/drivers/input/touchscreen/rtouch.ko ]; then
	rm -f /lib/modules/$(uname -r)/kernel/drivers/input/touchscreen/rtouch.ko
	/sbin/depmod
fi
#-----------------------------------------------------------------------
if [ -f /etc/modprobe.d/touchmodp.conf ]; then
	rm -f /etc/modprobe.d/touchmodp.conf
fi

if [ -f /etc/X11/xinit/xinitrc.d/99-setscreen.sh ]; then
	rm -f /etc/X11/xinit/xinitrc.d/99-setscreen.sh
fi

if [ -f /etc/X11/xinit/xinitrc.d/99-setscreen-def.sh ]; then
	rm -f /etc/X11/xinit/xinitrc.d/99-setscreen-def.sh
fi

if [ -f /etc/init.d/pctservice.sh ]; then
	rm -f /etc/init.d/pctservice.sh
	unlink /etc/rcS.d/S99pctservice
fi

if [ -f /etc/lightdm/setpdrv.sh ]; then
	rm -f /etc/lightdm/setpdrv.sh
fi

#-----------------------------------------------------------------------
disable_uinput=0
disable_pcspkr=0

if [ -f /etc/modules-load.d/pcspkr.conf ]; then
	rm -f /etc/modules-load.d/pcspkr.conf
	disable_pcspkr=1
fi

if [ -f /etc/sysconfig/modules/pcspkr.modules ]; then
	rm -f /etc/sysconfig/modules/pcspkr.modules
	disable_pcspkr=1
fi

if [ -f /etc/modprobe.d/beep.conf ]; then
	rm -f /etc/modprobe.d/beep.conf
	disable_pcspkr=1
fi

if [ -f /etc/modules-load.d/uinput.conf ]; then
	rm -f /etc/modules-load.d/uinput.conf
	disable_uinput=1
fi

if [ -f /etc/sysconfig/modules/uinput.modules ]; then
	rm -f /etc/sysconfig/modules/uinput.modules
	disable_uinput=1
fi

file="/etc/modprobe.d/blacklist.conf"

if [ -f $file.old ]; then
	ret=$(cat $file | grep '#blacklist pcspkr')
	
	if [ "$ret" != "0" ]; then
		disable_pcspkr=1
	fi
	
	ret=$(cat $file | grep '#blacklist uinput')
	
	if [ "$ret" != "0" ]; then
		disable_uinput=1
	fi
		
	mv $file.old $file
fi

file="/etc/modules"

if [ -f $file.old ]; then
	ret=$(cat $file | grep 'pcspkr')
	
	if [ "$ret" != "0" ]; then
		disable_pcspkr=1
	fi
	
	ret=$(cat $file | grep 'uinput')
	
	if [ "$ret" != "0" ]; then
		disable_uinput=1
	fi
		
	mv $file.old $file
fi

if [ "$disable_uinput" = "1" ]; then
	ret=$(lsmod 2>/dev/null | grep -ic 'uinput')

	if [ "$ret" != "0" ]; then
		/sbin/modprobe -r uinput >/dev/null 2>&1
	fi
fi

if [ "$disable_pcspkr" = "1" ]; then
	ret=$(lsmod 2>/dev/null | grep -ic 'pcspkr')

	if [ "$ret" != "0" ]; then
		/sbin/modprobe -r pcspkr >/dev/null 2>&1
	fi
fi
#-----------------------------------------------------------------------

if [ -f /usr/share/lightdm/lightdm.conf.d/50-setpdrv.conf ]; then
	rm -f /usr/share/lightdm/lightdm.conf.d/50-setpdrv.conf
fi

if [ -f /usr/share/applications/touchtool.desktop ]; then
	rm /usr/share/applications/touchtool.desktop
fi
#-----------------------------------------------------------------------
name_list="/etc/rc.local /etc/rc.d/rc.local /etc/init.d/after.local /etc/X11/xorg.conf /etc/profile"
for name in $name_list
do
	if [ -f $name.old ]; then
		mv -f $name.old $name
	fi
done
#-----------------------------------------------------------------------
path_list="/etc/X11/xorg.conf.d /usr/share/X11/xorg.conf.d /usr/lib/X11/xorg.conf.d"

for path in $path_list
do
	name_list="11-risintech.conf 99-risintech.conf 99-cooltouch.conf"
	
	for name in $name_list
	do
		if [ -f $path/$name ]; then
			rm -f $path/$name
		fi
	done
	
	name_list="05-evdev.conf 10-evdev.conf"
	
	for name in $name_list
	do
		if [ -f $path/$name.old ]; then
			mv -f $path/$name.old $path/$name
		fi
	done	
done
#-----------------------------------------------------------------------
input_fdi_path=/usr/share/hal/fdi/policy

if [ -d $input_fdi_path ]; then
	name_list="10-x11-cooltouch.fdi 10-x11-risintech.fdi"

	for name in $name_list
	do
		if [ -f $input_fdi_path/10osvendor/$name ]; then
			rm -f $input_fdi_path/10osvendor/$name
		fi
	done
	
	name_list="10-x11-input.fdi 11-x11-synaptics.fdi 10-synaptics.fdi"

	for name in $name_list
	do
		for sub_path in 10osvendor 20thirdparty
		do
			if [ -f $input_fdi_path/$sub_path/$name.old ]; then
				mv $input_fdi_path/$sub_path/$name.old $input_fdi_path/$sub_path/$name
			fi
		done
	done
fi
#-----------------------------------------------------------------------
path_list="/lib/systemd /etc/systemd"

for path in $path_list
do
	if [ -f $path/user/setpdrv.service ]; then
		rm -f $path/user/setpdrv.service
	fi
	
	if [ -f $path/system-sleep/touch-sleep.sh ]; then
		rm -f $path/system-sleep/touch-sleep.sh
	fi
	
	for file in $path/system/touchattach*; do		
		rm -f $file
	done
done
#-----------------------------------------------------------------------
name_list="sertp.conf tpinit tpxorg .pctbackup .restpbin .pctpbin tpdef.conf TouchUtil.ini idlist.conf manuflist.conf rmanuflist.conf"

for name in $name_list
do
	if [ -f /etc/$name ]; then
		rm -f /etc/$name
	fi
done
#-----------------------------------------------------------------------
name_list="setscreen chgxorg closedev tpservice setpdrv Touchpack FreeDraw LinearAp Touch_Alignment"

for name in $name_list
do
	if [ -f $BIN_DIR/$name ]; then
		rm -f $BIN_DIR/$name
	fi
done
#-----------------------------------------------------------------------
# delete binarie
for file in /usr/bin/touchattach*; do		
	rm -f $file
done

name_list="bcomp setpdrv.sh Align_Touch touchtool touch_utility setdev tpsndsev touchpackservice debug_tool touch_default touchsndservice touchattach"

for name in $name_list
do
	if [ -f $BIN_DIR/$name ]; then
		rm -f $BIN_DIR/$name
	fi
done
#-----------------------------------------------------------------------
path_list="/usr/share/wayland-sessions /usr/share/gdm/greeter/autostart /usr/share/gdm/autostart/LoginWindow /etc/xdg/autostart"

for path in $path_list
do
	name_list="setpdrv setpdrv-wayland cooltouch-xhost"
	
	for name in $name_list
	do
		if [ -f $path/$name.desktop ]; then
			rm -f $path/$name.desktop
		fi
	done
done
#-----------------------------------------------------------------------
#for path in /usr/lib /usr/lib64
for path in $paths; do
	modules="xfhiddrv_drv.so risintech_drv.so revdev_drv.so"
	
	for so in $modules; do
		dir=/usr/$path/xorg/modules/input
		
		if [ -f $dir/$so ]; then
			rm -f $dir/$so
		fi
	done

	libs="libtouchapi libLinear librisintp libtpinfo libtpdrv libPct"

	for lib in $libs
	do
		dir=/usr/$path
		
		if [ -f $dir/$lib.so.1.0 ]; then
			rm -f $dir/$lib.so.1.0
			rm -f $dir/$lib.so
			rm -f $dir/$lib.so.1
		fi
	done
done
#-----------------------------------------------------------------------
for udev_path in /etc/udev /lib/udev
do
	name_list="10-serialtouch.rules 99-hiddev.rules 99-touchscreen.rules 50-mouse.rules 99-mouse.rules 10-touch-wakup.rules 99-touch-wakup.rules 99-uinput.rules 99-cooltouch.rules"
	
	for name in $name_list
	do
		if [ -f $udev_path/rules.d/$name ]; then
			rm -f $udev_path/rules.d/$name
		fi
	done
	
	name_list="99-risintech-touchpanel.hwdb 99-cooltouch.hwdb"
	for name in $name_list
	do
		if [ -f $udev_path/hwdb.d/$name ]; then
			rm -f $udev_path/$name
		fi
	done	
done
#-----------------------------------------------------------------------
for path in /usr/local/share/touchtool /opt/.touch_panel
do
	if [ -d $path ]; then
		rm -rf $path
	fi
done
#-----------------------------------------------------------------------
file_list="/etc/lightdm/lightdm.conf /etc/selinux/config /etc/gdm/Init/Default ~/.bashrc"

for file in $file_list
do
	if [ -f $file.old ]; then
		mv $file.old $file
	fi
done
#-----------------------------------------------------------------------
path_list="/usr/share/sddm /etc/kde4/kdm /etc/kde/kdm"

for path in $path_list
do
	if [ -f $path/Xsetup.old ]; then
		mv $path/Xsetup.old $path/Xsetup
	fi
done
#-----------------------------------------------------------------------
contrys="ar el ja zh_TW zh_CN fr de de_BE de_DE pt_BR pt_PT ru es_MX es_ES tr it nl da hu sk zh_HK"

locale_dir=/usr/share/locale

for contry_code in $contrys; do
	for lang_app_name in Touchpack LinearAp FreeDraw Touch_Alignment
	do			
		if [ -f $locale_dir/$contry_code/LC_MESSAGES/$lang_app_name.mo ]; then
			rm -f $locale_dir/$contry_code/LC_MESSAGES/$lang_app_name.mo
		fi
	done
done
#-----------------------------------------------------------------------

echo "Remove completed"

if [ $boot = "1" ]; then
	reboot
fi
