#!/bin/bash

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

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

echo "Show some information and check them!"

# Check what kind of the distribution is it!
if [ -f /etc/fedora-release ]; then
  read distribution < /etc/fedora-release
  version=$(echo $distribution|cut -c1-16)  
  if [ "$version" == "Fedora release 7" ]; then
    echo $distribution
  else
    echo "Warning: Your distribution is not Fedora 7"
    echo "         Installation Failed!"
    exit
  fi
else
  echo "Warning: Your distribution is not Fedora 7"
  echo "         Installation Failed!"
  exit
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"
  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"
else
  echo "Warning: Don't know where is your X Server config file."
  echo "         Installation Failed!"
  exit
fi

# Check position of hiddev0
PATH_hiddev0=$(find /dev -name hiddev0)
if [ $? == 0 ]; then
  echo "hiddev0 is in the $PATH_hiddev0"
else
  echo "Warning: Can't find hiddev0 device!"
  echo "         Installation Failed!"
  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"/driver/ETouch_drv.so "$PATH_XServerModule"/input

selinux=$(egrep "SELINUX=" /etc/selinux/config|cut -d: -f2|cut -d= -f2)
if [ $selinux != "disabled" ]; then
  chcon -t textrel_shlib_t "$PATH_XServerModule"/input/ETouch_drv.so
fi

cp $TARGET_FILE ${TARGET_FILE}.bak.touch

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.touch>$TARGET_FILE 

if [ ! -d /usr/local/bin ]; then
  echo 1
  mkdir -p /usr/local/bin
fi
cp -f "$INSTALL_PATH"/utility/* /usr/local/bin

echo
echo "Touch Driver has Installed."
echo
echo "Calibration tools are in the directory /usr/local/bin,"
echo "you can copy them to your desktop."

choice=" "
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
