#!/bin/bash

# Check are you root?
whoami | egrep "^root$" >/dev/null

if [ $? -ne 0 ]; then
  echo "Warning:you are not root!"
  echo
  exit
fi

echo "Show some information and check them!"

# Check what kind of the distribution is it!
if [ -f /etc/SuSE-release ]; then
  read distribution < /etc/SuSE-release
  echo $distribution
elif [ -f /etc/redhat-release ]; then
  read distribution < /etc/redhat-release
  echo $distribution
else
  distribution=$(echo "I don't know what kind of distribution!")
  echo $distribution
fi

# Check what kind of the X Server is!
if [ -f /var/log/Xorg.0.log ]; then
  PATH_XServerConfig=$(egrep "Using config file" /var/log/Xorg.0.log|cut -d" " -f5|cut -d\" -f2)
  echo "X Window Config is in $PATH_XServerConfig"    #/etc/X11/xorg.conf
  PATH_XServerModule=$(egrep "ModulePath set to" /var/log/Xorg.0.log|cut -d" " -f5|cut -d\" -f2)
  echo "X Window Module Path is in $PATH_XServerModule"
elif [ -f /var/log/XFree86.0.log ]; then
  PATH_XServerConfig=$(egrep "Using config file" /var/log/XFree86.0.log|cut -d" " -f5|cut -d\" -f2)
  echo "X Window Config is in $PATH_XServerConfig"    #/etc/X11/XFree86.conf
  PATH_XServerModule=$(egrep "ModulePath set to" /var/log/XFree86.0.log|cut -d" " -f5|cut -d\" -f2)
  echo "X Window Module Path is in $PATH_XServerModule"
else
  echo "I don't know where is your X Server config file."
  echo "Install Fail!!"
  exit
fi

# Check position of hiddev0
PATH_hiddev0=$(find /dev -name hiddev0|egrep hiddev0)
if [ $? == 0 ]; then
  echo "hiddev0 is in the $PATH_hiddev0"
else
  echo "I can't find hiddev0!"
  echo "Install Fail!!"
  exit
fi

# I want to know where is your path!
echo $0 | egrep ^/ >/dev/null
if [ $? == 0 ]; then
  INSTALL_PATH=${0%/*}
else
  INSTALL_PATH=${PWD}/${0%/*}
fi

# the file is main file that I want to change!
TARGET_FILE=$PATH_XServerConfig

# Did I install before?
TEST_BEFOR=$(grep -ni "ETouch" $TARGET_FILE|cut -d ":" -f 1)
HAS_INSTALL=0

for i in $TEST_BEFOR
do
  if [ $i -gt 0 ]; then
    echo "TouchPanel Installed already"
    HAS_INSTALL=1
    break;
  fi
done

if [ $HAS_INSTALL -eq 1 ]; then
   exit
fi

# Start to install
mkdir /etc/etandt
cp -f "$INSTALL_PATH"/obj/ETouch_drv.o "$PATH_XServerModule"/input

cp $TARGET_FILE ${TARGET_FILE}.bak

sed -e '$a\
Section "InputDevice"\
    Identifier  "ETouch"\
    Driver "ETouch"\
    Option "Device" "'$PATH_hiddev0'"\
EndSection' \
-e '/ServerLayout/,/EndSection/s/EndSection/  InputDevice     "ETouch"  "SendCoreEvents"\
EndSection/' ${TARGET_FILE}.bak>$TARGET_FILE 

cp -f "$INSTALL_PATH"/obj/tscpl /root/Desktop/Calibration
cp -f "$INSTALL_PATH"/obj/swap /root/Desktop/Swap
cp -f "$INSTALL_PATH"/obj/liftoff /root/Desktop/Lift-off

if [ ! -d /usr/local/bin ]; then
  echo 1
  mkdir -p /usr/local/bin
fi
cp -f "$INSTALL_PATH"/obj/tscpl /usr/local/bin/Calibration
cp -f "$INSTALL_PATH"/obj/swap /usr/local/bin/Swap
cp -f "$INSTALL_PATH"/obj/liftoff /usr/local/bin/Lift-off

choice=" "
echo
echo Touch Driver has Installed.
while [ "$choice" != "y" ]; do
echo
echo -n "Do you want to Reboot now? (y/n):"
read choice
echo
case $choice in
      y|Y) echo 'Rebooting...'
           reboot
           echo;;
      n|N) echo 'It is recommaned to reboot after driver installed'
           echo
           break;;
      *) ;;
esac
done
