#!/bin/sh
[ "$USER" != "root" ] && echo "need root" && exit 1

driver_ver=ubuntu-13.04.x
machine=$(uname -p)

# Decompress tar
echo "*******Decompress tar*******"
tar xzvf $driver_ver.tgz
cd $driver_ver

if [ -f /usr/local/bin/LinearAp ]; then
	sh remove.sh
fi

cd $machine

version=$(./checkVer)
echo "X Server :"$version

if [ ! -d "$version" ]; then
	echo "X Server version not match!!"
	exit 1;
fi

# Modify xorg.conf
if [ -f /etc/X11/xorg.conf ]; then
	# Backup xorg.conf
	cp /etc/X11/xorg.conf /etc/X11/xorg.conf.old
	cp /etc/X11/xorg.conf ./xorg.conf
else
	Xorg -configure :1
	if [ -f ~/xorg.conf.new ]; then
		cp ~/xorg.conf.new ./xorg.conf
	else
		echo "Modify xorg.conf fail!!" 
		exit 1;
	fi
fi

./chgxorg
result=$?
if [ "$result" = "1" ]; then
	#echo "Touch Device not found!!"
	echo "Setup Fail,please check the touch device."
	if [ -f /etc/X11/xorg.conf.old ]; then
		rm -f /etc/X11/xorg.conf.old
	fi
	exit 1;
fi

if [ -f /usr/share/X11/xorg.conf.d/10-evdev.conf ]; then
	cp -f /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/10-evdev.conf.old
fi

cp -f ./xorg.conf /etc/X11/xorg.conf

cp -f /etc/profile ./
chmod 777 setboot
./setboot
cp -f /etc/profile /etc/profile.old
cp -f ./profile /etc/profile

# Copy binaries 
echo "*******Copy binaries*******"
cp ./LinearAp /usr/local/bin/
chmod 777 /usr/local/bin/LinearAp

cp ./setparams /usr/local/bin/
chmod 777 /usr/local/bin/setparams

cp ./setevdev /usr/local/bin/
chmod 777 /usr/local/bin/setevdev

#cp ./Align_Touch /usr/local/bin/
#chmod 777 /usr/local/bin/Align_Touch

cp -f $version/xfhiddrv_drv.so /usr/lib/xorg/modules/input/
cd ..
cp -f ./10-evdev.conf /usr/share/X11/xorg.conf.d/
sh install.sh

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

