#!/bin/sh

Script_Ver="1.00"

InitMember(){
	drivername="elo"
	
	installpath="/etc/opt"
	driverfolder="elo-mt-usb"
	driverpath="${installpath}/${driverfolder}"
	
	archivefile="elo-mt-usb.tar.xz"

	#/etc/system/systemd
	systemdpath="/etc/systemd/system" # for Ubuntu 18+
	daemonService="elo.service"

	UtilityDesktop="${drivername}.desktop"
	utilityExec="cpl"

	apppath="/usr/local/share/elo"
	iconfile="${drivername}.png"
	applicationpath="/usr/share/applications"

	udevpath="/etc/udev/rules.d"
	udevfile="99-elotouch.rules"
}

ShowTitle() {
    echo ""
    echo "(*) Driver installer for Elo touch controller "
	echo "(*) Script Version = ${Script_Ver} "
    echo ""
}

CheckPermission() {
    echo -n "(I) Check user permission:"
    account=`whoami`
    if [ ${account} = "root" ]; then
        echo " ${account}, you are the supervisor."
    else
        echo " ${account}, you are NOT the supervisor."
        echo "(E) The root permission is required to run this installer."
        echo ""
        exit 1
    fi
}

InstallDriver(){
	if [ -e ${installpath} ]; then
		if [ -e ${driverpath} ]; then
			echo "(E) The driver folder already exists at ${driverpath}."
			echo ""
			exit 1
		fi
		if [ ! -e ${archivefile} ]; then
			echo "(E) The archive file ${archivefile} is missing."
			echo ""
			exit 1
		fi
		
		tar -xf "${archivefile}" -C "${installpath}"
		chmod 777 -R ${driverpath}
		chmod 444 ${driverpath}/*.txt
	fi
}

RemoveDriver(){
	if [ -e ${driverpath} ]; then	
		rm -rf ${driverpath}
	fi
}


AddService(){
	if [ -e ${systemctlpath} ];then
		echo "(I) Found systemd service support in system"
		cp -r "${driverpath}/${daemonService}" ${systemdpath} 
		chmod a+x ${systemdpath}/${daemonService}
		
		if [ -e ${systemdpath}/${daemonService} ]; then
			systemctl enable elo.service
			systemctl start elo.service
		fi
	fi
}

RemoveService(){
	if [ -e ${systemdpath}/${daemonService} ]; then
		systemctl stop elo.service
		systemctl disable elo.service
		rm "${systemdpath}/${daemonService}"
	fi
}

AddUtilityShortCut() {
	if [ -e ${driverpath}/${utilityExec} ]; then	
		apt-get update
		apt-get install -y libxm4
		
		if [ ! -e ${apppath} ]; then
			mkdir ${apppath}
		fi
		cp -f ${driverpath}/${iconfile} ${apppath}/${iconfile}
		cp -f ${driverpath}/${utilityExec} ${apppath}/${utilityExec}
		
		if [ -e ${applicationpath} ]; then
			cp -f "${driverpath}/${UtilityDesktop}" ${applicationpath}/${UtilityDesktop}
		fi
		echo "(I) Create ${utilityExec} shortcut in application list."
	fi
}
RemoveUtilityShortCut() {
	if [ -e ${applicationpath}/${UtilityDesktop} ];then
		rm -f ${applicationpath}/${UtilityDesktop}
	fi
	if [ -e ${apppath} ]; then
		rm -rf ${apppath}
	fi
}

InstallUdevRule(){
	if [ -e ${udevpath} ];then 
		cp -f "${driverpath}/${udevfile}" ${udevpath}
		chmod a+x ${udevpath}/${udevfile}
		echo "(I) Copy ${udevfile} to ${udevpath}."
	fi
}

RemoveUdevRule(){
	if [ -e ${udevpath}/${udevfile} ];then  
		rm ${udevpath}/${udevfile}
		echo "(I) Removed ${udevpath}/${udevfile}"
	fi
}

if [ $# = 0 ]; then
	SilentMode="false"
	clear
	ShowTitle
	CheckPermission
	InitMember
	
	InstallDriver
	InstallUdevRule
	AddService
	AddUtilityShortCut
	
	echo ""
   	echo "(I) The ${drivername} driver has been installed successfully."
	echo "(I) Please reboot the system."
elif [ $# -ge 1 ]; then
	if [ $1 = "uninstall" ]; then
		SilentMode="false"
		clear
		ShowTitle
		CheckPermission
		InitMember
		
		RemoveUtilityShortCut
		RemoveService
		RemoveUdevRule
		RemoveDriver
		
       	echo ""
       	echo "(I) The ${drivername} driver has been removed successfully."
       	echo "(I) Please reboot the system."
	fi
fi

