#!/bin/sh
### BEGIN INIT INFO
# Provides: altagent
# Required-Start: $syslog $network cron
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 6
# Description: Installs Altiris agent
### END INIT INFO

##################################################
# Install Altiris Agent for Linux
##################################################
echo Altiris Agent Install Beginning    
sgtk_dir=/opt/ibm/sgtk/log
log=$sgtk_dir/adlagent.inst.log
if [ ! -d  $sgtk_dir ]; then
 mkdir -p $sgtk_dir 
fi
echo Altiris Agent Install Beginning    >$log
if [ ! -d  /tmp/agents ]; then
  mkdir -p /tmp/agents
fi
if [ ! -d  /mnt/altiris ]; then
  mkdir -p /mnt/altiris
fi
if [ ! -f  /tmp/tkvars ]; then
  echo "Not Found /tmp/tkvars"  >>$log
  mv -f /etc/rc.d/*adlagent.sh  /tmp/S99adlagent.sh.ran 
  rm -f /etc/rc.d/rc3.d/*adlagent.sh
  rm -f /etc/rc.d/rc5.d/*adlagent.sh
  exit 1
fi
. /tmp/tkvars
# NOTE: The following line assumes that the Altiris share is configured with
# access enabled for samba
# Credentials for access are saved in /tmp/tkvars during deployment
umount /mnt/altiris
mount -o username=$LOGON,password=$PASSWORD //$SERVERIP/$ALTSHARE /mnt/altiris
if [ $? -ne 0 ]; then
	echo "Mounting Failed" >>$log
	echo "mount -o username=$LOGON,password=$PASSWORD //$SERVERIP/$ALTSHARE /mnt/altiris" >>$log
        #To remove script from executing on next reboot
        echo "Removing init scripts for install"  >>$log
        mv -f /etc/rc.d/*adlagent.sh  /tmp/S99adlagent.sh.ran 
        rm -f /etc/rc.d/rc3.d/*adlagent.sh
        rm -f /etc/rc.d/rc5.d/*adlagent.sh
	exit 1
fi
#Determine the platform
uname -i | grep -q i386
if [ $? -eq 0 ]; then
	echo "Running on 32bit platform" >>$log
	platform=i386
else
	echo "Running on 64bit platform" >>$log
	platform=x86_64
fi
cp /mnt/altiris/agents/adlagent/altiris-adlagent*$platform*.* 	/tmp/agents 2>/dev/null
cp /mnt/altiris/altiris-adlagent*$platform*.* 			/tmp/agents 2>/dev/null
cp /mnt/altiris/Agents/ADLAgent/altiris-adlagent*$platform*.* 	/tmp/agents 2>/dev/null
mkdir -p /opt/altiris/deployment/adlagent/conf
if [ -f /mnt/altiris/Agents/ADLAgent/adlagent.conf ]; then
cp /mnt/altiris/Agents/ADLAgent/adlagent.conf /opt/altiris/deployment/adlagent/conf/adlagent.conf
elif [ -f /mnt/altiris/agents/adlagent/adlagent.conf ]; then
cp /mnt/altiris/agents/adlagent/adlagent.conf /opt/altiris/deployment/adlagent/conf/adlagent.conf
elif [ -f /mnt/altiris/adlagent.conf ]; then
cp /mnt/altiris/adlagent.conf /opt/altiris/deployment/adlagent/conf/adlagent.conf
fi
#Update DServerIP using $SERVERIP
sed 's/^TcpAddr=[0-9].*/TcpAddr='$SERVERIP'/' </opt/altiris/deployment/adlagent/conf/adlagent.conf > /tmp/adlagent.conf
cp -f /tmp/adlagent.conf /opt/altiris/deployment/adlagent/conf/adlagent.conf


#Finish copy umount share
umount /mnt/altiris

cd /tmp/agents
#Initialize type install
type_inst=0
ls *.bin 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  for agentBIN in *.bin
  do
     echo "Altiris Agent Binary Found"   >>$log
     echo "Agent Binary = $agentBIN"     >>$log
  done
else
  #rpm its only for redhat
  if [ -f /etc/redhat-release ]; then
    ls *.rpm 1>/dev/null 2>/dev/null
    if [ $? -eq 0 ]; then
      for agentRPM in *.rpm
      do
        echo "Altiris Agent RPM Found"     >>$log
        echo "Agent RPM = $agentRPM"        >>$log
	type_inst=1
      done
    fi
  fi
fi
#Remove script from executing on next reboot
echo "Removing init scripts for install"  >>$log
mv -f /etc/rc.d/*adlagent.sh  /tmp/S99adlagent.sh.ran 
rm -f /etc/rc.d/rc3.d/*adlagent.sh
rm -f /etc/rc.d/rc5.d/*adlagent.sh

if [ $type_inst -eq 0 ]; then
     echo "Installing Agent Binary $agentBIN"     >>$log
     chmod +x $agentBIN 
     /tmp/agents/$agentBIN & 
else
     echo "Installing Agent RPM  $agentRPM"        >>$log
     rpm -Uvh $agentRPM &                 
fi
echo "Finish Installing Altiris Agent"     >>$log
shutdown -r +1 &



