#!/bin/bash

ARCH=`uname -m`
RELEASE=`uname -r`

if [ "$(whoami)" != "root" ]; then
	echo "Please run as root or sudo."
	exit 1
fi

echo "Current architecture: $ARCH"

if [ -d "/opt/weida" ]; then
	if [ -f "/opt/weida/uninstall" ]; then
		echo "Uninstall the origin version"
		/opt/weida/uninstall >& /dev/null;
		sleep 1
	fi
fi

echo "Current architecture: $ARCH"
echo "Start to install..."

mkdir /opt/weida >& /dev/null;
cp ./weida/scripts/* /opt/weida/ >& /dev/null;
cp ./weida/$ARCH/* /opt/weida/ >& /dev/null;
chmod 755 /opt/weida/ct_util

cp ./weida/option/ct_util_mse.ini /opt/weida/ct_util.ini >& /dev/null;
cp ./weida/run/uinput_run /opt/weida/ct_run >& /dev/null;
echo "Option: mouse emultion"

chmod 755 /opt/weida/ct_run

plat_id=`cat /etc/*release`

if echo "$plat_id" | grep -q "Ubuntu"; then
  	echo "Ubuntu platform... "
	chmod 755 /opt/weida/Ubuntu_install
	/opt/weida/Ubuntu_install
fi

if echo "$plat_id" | grep -q "Fedora"; then
  	echo "Fedora platform... "
   	if echo "$plat_id" | grep -q "16"; then
		cp ./weida/run/uinput_run /opt/weida/ct_run >& /dev/null;
		echo "Use uinput"
   	fi
	chmod 755 /opt/weida/ct_run
	chmod 755 /opt/weida/Fedora_install
	/opt/weida/Fedora_install
fi

if echo "$plat_id" | grep -q "openSUSE"; then
  	echo "openSUSE platform... "
 	if echo "$plat_id" | grep -q "12.1"; then
		cp ./weida/run/uinput_run /opt/weida/ct_run >& /dev/null;
		echo "Use uinput"
   	fi
	chmod 755 /opt/weida/ct_run
 	if echo "$plat_id" | grep -q "11."; then
		cp /opt/weida/OpenSuse11_install /opt/weida/OpenSuse_install
	fi
	chmod 755 /opt/weida/OpenSuse_install
	/opt/weida/OpenSuse_install
fi

if echo "$plat_id" | grep -q "CentOS"; then
  	echo "CentOS platform... "
	chmod 755 /opt/weida/CentOS_install
	/opt/weida/CentOS_install
fi

#if echo "$plat_id" | grep -q "Mint"; then
#  	echo "Mint platform... "
#	chmod 755 /opt/weida/Mint_install
#	/opt/weida/Mint_install
#fi

if echo "$ARCH" | grep -q "armv7l"; then
  	echo "Raspbian platform... "
	chmod 755 /opt/weida/Ubuntu_install
	/opt/weida/Ubuntu_install
fi

if echo "$ARCH" | grep -q "armv6"; then
  	echo "armv6 arch... "
	chmod 755 /opt/weida/Ubuntu_install
	/opt/weida/Ubuntu_install
fi

