#!/bin/bash
#
# Copyright 2007-2014 3M. All rights reserved.
#
# This script installs the MT7 touch screen driver
# During installation, all directories must be writeable.
#

# Check for root privileges
if [ "$(id -u)" != "0" ]
then
   echo "You must have root privileges to run this script."
   exit
fi

# Check for perl
if [ "x$(whereis perl)" == "xperl:" ]
then
   echo "'perl' not found; please install perl."
   exit
fi

# These symbols point to where the MT7 software binaries and data reside.
# The script attempts to detect where the installation kit is. If this
# fails, you need to set BinDir.
# The data directory must be on writeable media. The script normally uses
# the directory where the installation kit resides as the data directory.
# If you need the data directory to be elsewhere, set DataDir.
BinDir=""
DataDir=""

# If desired, define a file to contain driver startup options and set
# the TwDrv_Cfg symbol to the full path of the file. Normally this is
# not needed.
TwDrv_Cfg=""

# This symbol points to where the Java VM binaries reside.
JavaBinDir="/usr/bin"

# These symobls point to system and applictaion directories other than
# those specific to the MT7 software. This script attempts to determine
# the symbols LibDir.
UdevDir="/etc/udev"
HotplugDir="/etc/hotplug"
LibDir=""
SEDir1="/usr/selinux/booleans"
SEDir2="/selinux/booleans"
LSBDir="/lib/lsb"
HalDir=""

# The InitDir symbol points to where this script places an 'init' script.
# If left blank, this script first looks for /etc/init.d and then /etc/rc.d.
# If this is not appropriate or this script otherwise fails, set this value.
InitDir=""

# This symbol enables permission for some MT7 shared objects on
# SELinux systems. On most systems SEGivePermission is texrel_shlib_t.
# Change this variable if another security type is appropriate.
SEGivePermission="texrel_shlib_t"

# These symbols define where the 50MT7-xinit script needs to go and what
# suffix it requires. The script places this file automatically in
# /etc/X11/xinit/xinitrc.d and /etc/X11/Xsession.d without a suffix. If
# your distribution requires another location or requires a suffix on the
# file, set these symbols.
XinitDir=""
XinitSuffix=""

# Get the machine type
OSMachine=$(uname -m)

# Determine the library installation directory
if [ x$LibDir == x ]
then
   LibDir="/usr/lib"
   if [ $OSMachine == x86_64 ]
   then
      if [ -d "/usr/lib64" ]
      then
         LibDir="/usr/lib64"
      fi
   fi
fi

# Determine the installation directory
if [ -z $BinDir ]
then
   if [ $(echo $0 | grep ^/) ]
   then
      BinDir=$0
   else
      BinDir=$(echo $PWD"/"$0 | sed s#[.]/##)
   fi
   BinDir=$(echo $BinDir | sed s%/[^/]*$%%)
fi

# Check for uinput
if [ ! -e /dev/uinput -a ! -e /dev/input/uinput ]
then
   ModInfoCmd=$(perl $BinDir/TwIsThere.perl modinfo uinput)
   if [ "x$ModInfoCmd" != "x" ]
   then
      $ModInfoCmd >/dev/null 2>/dev/null
      ModInfoCmd=$?
   else
      Kernel=$(uname -r)
      ModInfoCmd=$(perl $BinDir/TwIsThere.perl modinfo -k $Kernel uinput)
      if [ "x$ModInfoCmd" != "x" ]
      then
         $ModInfoCmd >/dev/null 2>/dev/null
         ModInfoCmd=$?
      else
         ModInfoCmd=1
      fi
   fi
   if [ x$ModInfoCmd != x0 ]
   then
      echo "'uinput' not found."
      exit
   fi
fi

# Determine if the system is compatible
$BinDir/TwCompat
if [ $? != 0 ]
then
   echo "Cannot find shared memory support"
   exit
fi

# Check for Java
if [ ! -e $JavaBinDir/java ]
then
   echo "Cannot find $JavaBinDir/java; the control panel will not work"
fi

# Determine the data directory
[ -z $DataDir ] && DataDir=$BinDir

# Create the data and fifo directories
if [ $DataDir != $BinDir ]
then
   [ -e $DataDir ] || mkdir $DataDir
   chmod a+w $DataDir
   ln -s $DataDir $BinDir/data
else
   [ -e $BinDir/data ] || mkdir $BinDir/data
fi
chmod a+w $BinDir/data
[ -e $BinDir/data/fifo ] || mkdir $BinDir/data/fifo
chmod a+w $BinDir/data/fifo

# Determine the init script directories
if [ -z $InitDir ] && [ -d /etc/init.d ]
then
   if [ $(ls -l /etc/init.d/ | sed -e /functions/d -e /^total\ [0-9]*$/d | wc -l) != 0 ]
   then
      InitDir="/etc/init.d"
   fi
fi
if [ -z $InitDir ]
then
   if [ -e /etc/rc.d/rc.local ]
   then
      InitDir=/etc/rc.d
   else
      InitDir=$BinDir
   fi
fi

# Install the HAL policy file
if [ x$HalDir == x ]
then
   if [ "x$(whereis hald)" != "xhald:" ]
   then
      if [ -d /usr/share/hal/fdi/policy ]
      then
         HalDir=/usr/share/hal/fdi/policy
      elif [ -d /etc/hal/fdi/policy ]
      then
         HalDir=/etc/hal/fdi/policy
      else
         echo "Unable to determine where to install the HAL policy file."
         exit
      fi
   fi
fi
if [ x$HalDir != x ]
then
   if [ ! -d $HalDir ]
   then
      echo "HAL policy directory $HalDir not found."
      exit
   fi
   for x in $HalDir/*
   do
      if [ -d $x ]
      then
         HalDir=$x
      fi
   done
   cp -f 99-3M-touchscreens.fdi $HalDir/
   echo "HAL policy file copied. You may need to reboot your system."
fi

# Install the init script
[ -e $InitDir/TWDrvStartup ] && rm -f $InitDir/TWDrvStartup
sed -e s#%BINDIR%#$BinDir#g \
    -e s#%INITDIR%#$InitDir#g \
    -e s#%LSBDIR%#$LSBDir#g \
    -e s#%TWDRV_CFG%#$TwDrv_Cfg#g $BinDir/TWDrvStartup.ORIG \
    >$InitDir/TWDrvStartup
chmod a+x $InitDir/TWDrvStartup
InitCmd=$(perl $BinDir/TwIsThere.perl chkconfig --add TWDrvStartup)
if [ "x$InitCmd" == "x" ]
then
   InitCmd=$(perl $BinDir/TwIsThere.perl update-rc.d TWDrvStartup defaults)
fi
if [ "x$InitCmd" == "x" ]
then
   InitCmd=$(perl $BinDir/TwIsThere.perl update-rc add TWDrvStartup default)
fi
if [ "x$InitCmd" != "x" ]
then
   $InitCmd  >/dev/null
elif [ -e $InitDir/rc.local ]
then
   sed -e '$ a\
%INITDIR%/TWDrvStartup start
' $InitDir/rc.local >$InitDir/rc.local.TEMP
   rm -f $InitDir/rc.local
   sed -e s#%INITDIR%#$InitDir# $InitDir/rc.local.TEMP >$InitDir/rc.local
   rm -f $InitDir/rc.local.TEMP
   chmod +x $InitDir/rc.local
else
   echo "Cannot install the init script"
fi

# Test for USB support
if [ -z $(uname -r | grep ^2\.4\.) ]
then
   # Copy the udev rules script
   Hotplug=0
   if [ -d $UdevDir/rules.d ]
   then
      if [ -e $UdevDir/rules.d/99-TwDriver.rules ]
      then
         rm -f $UdevDir/rules.d/99-TwDriver.rules
      fi
      sed s#%BINDIR%#$BinDir#g $BinDir/99-TwDriver.rules.ORIG \
         >$UdevDir/rules.d/99-TwDriver.rules
      Hotplug=1
   fi
   if [ -d $HotplugDir/usb ] && [ -e $HotplugDir/usb.agent ]
   then
      [ -e $HotplugDir/usb/TwHotplug ] && rm -f $HotplugDir/usb/TwHotplug
      sed s#%BINDIR%#$BinDir#g $BinDir/TwHotplug.ORIG > $HotplugDir/usb/TwHotplug
      chmod a+x $HotplugDir/usb/TwHotplug
      [ -e $HotplugDir/usb.usermap ] || echo "# Created by MT7" >$HotplugDir/usb.usermap
      sed <$HotplugDir/usb.usermap >$HotplugDir/usb.usermap.TEMP '$ a\
# TwHotplug is for the MT7 for Linux software\
TwHotplug            0x0001      0x0596   0x0000    0x0000       0x0000      0x00         0x00            0x00            0x06            0x00               0x00               0x00000000
'
      rm -f $HotplugDir/usb.usermap
      mv $HotplugDir/usb.usermap.TEMP $HotplugDir/usb.usermap
      Hotplug=1
   fi
   if [ $Hotplug == 0 ]
   then
      echo "Hotplugging of USB touch screen controllers is not supported"
   fi
else
   echo "USB touch screen controllers are not supported under kernel 2.4"
fi

# Test for the version of C++ standard libraries
if [ $OSMachine == x86_64 ]
then
   CppExt=""
elif [ -e $LibDir/libstdc++.so.6 ]
then
   CppExt="6"
elif [ -e $LibDir/libstdc++.so.5 ]
then
   CppExt="5"
else
   # Test newer systems' loaded libraries rather than single library path
   $BinDir/TwLibTest libstdc++.so.6
   if [ x$? == x1 ]
   then 
      CppExt="6"
   else
     $BinDir/TwLibTest libstdc++.so.5
     if [ x$? == x1 ]
     then
       CppExt="5"
     else
       echo "Cannot find needed libstdc++.so in $LibDir"
       CppExt=""
     fi
   fi
fi

# Link the libraries into /usr/lib
perl $BinDir/TwLibInstall.perl install $LibDir $BinDir/lib*.so
if [ x$CppExt != x ]
then
   perl $BinDir/TwLibInstall.perl install $LibDir $BinDir/so$CppExt/lib*.so
fi

# Link Xinerama/RandR sensitive files
$BinDir/TwLibTest $LibDir/libTwSystem.so
if [ x$? == x0 ]
then
   rm -f $LibDir/libTwSystem.so
   ln -s $LibDir/libTwSystemNoXin.so $LibDir/libTwSystem.so
   ln -s $BinDir/TwMonitor.bin$CppExt $BinDir/TwMonitor
elif perl $BinDir/TwIsThere.perl xrandr >/dev/null
then
   #In case of no RnR, suppress the warnings
   RnRver="$(xrandr -v 2> /dev/null)"
   if [ $? == "0" ]
   then
      RnRver=""
      RnRver="$(xrandr -v | grep 1.[0-1])"
      if [ -n "$RnRver" ]
      then
         rm -f $LibDir/libTwSystem.so
         ln -s $LibDir/libTwSystemRnR.so $LibDir/libTwSystem.so
         ln -s $BinDir/TwMonitorRnR.bin$CppExt $BinDir/TwMonitor
      else
         rm -f $LibDir/libTwSystem.so
         ln -s $LibDir/libTwSystemRnR12.so $LibDir/libTwSystem.so
         ln -s $BinDir/TwMonitorRnR.bin$CppExt $BinDir/TwMonitor
      fi
   else
      ln -s $BinDir/TwMonitor.bin$CppExt $BinDir/TwMonitor
   fi
else
   ln -s $BinDir/TwMonitor.bin$CppExt $BinDir/TwMonitor
fi

# Install the xinit scripts
for d in /etc/X11/xinit/xinitrc.d /etc/X11/Xsession.d /etc/X11/xinit.d $XinitDir
do
   if [ -d $d ]
   then
      sed s#%BINDIR%#$BinDir#g $BinDir/50MT7-xinit.ORIG \
         >$d/50MT7-xinit$XinitSuffix
      chmod a+x $d/50MT7-xinit$XinitSuffix
   fi
done

# Set up the SELinux security types
if [ -d $SEDir1 ]
then
   SEDir=$SEDir1
elif [ -d $SEDir2 ]
then
   SEDir=$SEDir2
else
   SEDir=""
fi
if [ x$SEDir != x ]
then
   for Library in System Config IO_Utilities AppIO_JNI Common_JNI Config_JNI UI_JNI UICP
   do
      chcon -t $SEGivePermission $LibDir/libTw$Library.so
   done
fi

# Set up the configuration
[ -d /dev/shm ] && rm -f /dev/shm/*TwConfig*
sed s#%BINDIR%#$BinDir#g $BinDir/TwFramework.cfg.ORIG >$BinDir/TwFramework.cfg
$BinDir/TwCfgUtil /u $BinDir/TwFramework.cfg
$BinDir/TwCfgUtil /u $BinDir/TwFactory.cfg

# Produce the Remove script
sed -e s#%BINDIR%#$BinDir#g \
    -e s#%UDEVDIR%#$UdevDir#g \
    -e s#%LIBDIR%#$LibDir#g \
    -e s#%SEDIR%#$SEDir#g \
    -e s#%HOTPLUGDIR%#$HotplugDir#g \
    -e s#%INITDIR%#$InitDir#g \
    -e s#%XINITDIR%#$XinitDir#g \
    -e s#%XINITSUFFIX%#$XinitSuffix#g \
    -e s#%HALDIR%#$HalDir#g \
    $BinDir/Remove.ORIG >$BinDir/Remove

# Produce the CP start script
sed -e s#%JAVABINDIR%#$JavaBinDir#g \
    -e s#%BINDIR%#$BinDir# \
    $BinDir/StartCP.ORIG >$BinDir/StartCP

# Set any necessary permissions
chmod a+x $BinDir/TwCalib
chmod u+x $BinDir/Remove
chmod a+x $BinDir/StartCP
