#!/bin/bash

CONFIG_FILE=adlagent.conf
DEF_MCAST__ADDR=225.1.2.3

# see if there is an adlagent running
ps -C adlagent > /dev/null
if [ "$?" = 0 ]; then
    [ -f /etc/init.d/adlagent ]
    if [ "$?" = 0 ]; then
	 /etc/init.d/adlagent stop > /dev/null
    else
         killall -INT adlagent > /dev/null
    fi;

fi;
# remind the script to restart the service when we are done.
RESTART_SERVICE="YES"

    
echo       "Should the Altiris Deployment Agent use Multicast" 
echo  -n   "to connect with the Deployment Server? (Y/N) [default=Y] "
read ANSWER

# see if the user wants to enable multicast to contact
# Altiris Deployment Server
#
if [ "$ANSWER" = "y" -o "$ANSWER" = "Y" -o "$ANSWER" = "" ]; then
    MULTICAST=Y
else
    MULTICAST=N
fi;

# if we are multicasting, get the multicast address to use
# if not multicasting, get address of Altiris Deployment Server
if [ "$MULTICAST" = "Y" ]; then
    echo    "Using Multicast"
    echo -n "enter multicast address [default=${DEF_MCAST__ADDR}] "
    read MCAST_ADDR

    if [ "$MCAST_ADDR" == "" ];then
	MCAST_ADDR=$DEF_MCAST__ADDR
    fi;

    echo "Multicast using $MCAST_ADDR"
    
    # if we find the line where uncommented, great
     sed -e 's/^\;UseMCast=.*/UseMCast=True/' $CONFIG_FILE > $CONFIG_FILE.tmp
     sed -e 's/^UseMCast=.*/UseMCast=True/' $CONFIG_FILE.tmp > $CONFIG_FILE
      
     #Uncomment and set the mcast address
    sed -e "s/^\;MCastAddr=.*/MCastAddr=$MCAST_ADDR/" $CONFIG_FILE >  $CONFIG_FILE.tmp
    sed -e "s/^MCastAddr=.*/MCastAddr=$MCAST_ADDR/" $CONFIG_FILE.tmp  > $CONFIG_FILE 
    

    #remove the comment from Mcast Port if it exists
    sed -e "s/^\;MCastPort/MCastPort/" $CONFIG_FILE >  $CONFIG_FILE.tmp
    
    #remove the comment from the TTL
    sed -e "s/^\;TTL=/\TTL=/" $CONFIG_FILE.tmp > $CONFIG_FILE

    #comment out the TcpAddr line
    sed -e "s/^TcpAddr/\;TcpAddr/" $CONFIG_FILE > $CONFIG_FILE.tmp
    
    #and the TcpPort line
    sed -e "s/^TcpPort=/\;TcpPort=/" $CONFIG_FILE.tmp > $CONFIG_FILE

 else
    echo    "Using direct TCP connection"
    echo -n "Enter address of Deployment Server "
    read TCP_ADDR
    echo    "Contact Deployment Server at $TCP_ADDR"

    # first, make sure the multicast line is commented out and disabled
    # if we find the line where uncommented, great
    sed -e 's/^;UseMCast=.*/\;UseMCast=False/' $CONFIG_FILE > $CONFIG_FILE.tmp
    sed -e 's/^UseMCast=.*/\;UseMCast=False/' $CONFIG_FILE.tmp > $CONFIG_FILE

    # comment out the MCastAddr
    sed -e "s/^MCastAddr/\;MCastAddr/" $CONFIG_FILE > $CONFIG_FILE.tmp
    # comment out the MCastPort
    sed -e "s/^MCastPort/\;MCastPort/" $CONFIG_FILE.tmp > $CONFIG_FILE
    # comment out the TTL
    sed -e "s/^TTL=/\;TTL=/" $CONFIG_FILE > $CONFIG_FILE.tmp

    
    # uncomment the tcp port line
    sed -e "s/^;TcpPort=/\TcpPort=/" $CONFIG_FILE.tmp > $CONFIG_FILE
  
#
# brute force this so that we definately get rid of any semi-colon
# then make sure we get the address we want
#
    if [ "x$TCP_ADDR" != "x" ] ; then
	sed -e "s/^\;TcpAddr=.*/TcpAddr=$TCP_ADDR/" $CONFIG_FILE > $CONFIG_FILE.tmp
	sed -e "s/^TcpAddr=.*/TcpAddr=$TCP_ADDR/" $CONFIG_FILE.tmp > $CONFIG_FILE
    fi;
fi;

if [ "$RESTART_SERVICE" == "YES" ]; then
   [ -f /etc/init.d/adlagent ]
   if [ "$?" = 0 ]; then
       /etc/init.d/adlagent start
   else
       /opt/altiris/deployment/adlagent/adlagent -start
   fi;     
fi;
