###
### set_directory.sh
### 
### Script for creating Company directory structure 
### 
### This script is was tested on a Sun Solaris box
### and may not work for other UNIX flavors
###



directory=""
while [ "$directory" = "" ]
do
   echo "Enter Company home directory: \c"
   read directory
done


if [ ! -d $directory ]
then
    mkdir $directory
    if [ $? -gt 0 ]
    then
        echo "I do not have permissions to create directory $directory"
        echo "Exiting...."
        exit 1
    fi
fi


cp -rp config $directory

if [ $? -gt 0 ]
then
   echo "Unable to cp files into $directory"
   echo "Please adjust directory permissions"
   echo "Exiting..."
   exit 1
fi

echo "$directory created....files copied...."


CurMulticast=`grep swarmcache.multicast.ip $directory/config/swarmcache.properties | cut -d= -f2`

echo  "Use default swarmcache multicast IP address of ${CurMulticast}? (Y/n): \c"
read sanswer


if [ "$sanswer" = "n" ] || [ "$sanswer" = "N" ]
then
    echo  "Enter IP address to use for swarmcache multicast: \c"
    read multicast
    while [ -z "$multicast" ]
    do
        echo "Invalid.  Please enter swarmcache multicast IP address \c: "
        read multicast
    done
else
    multicast=$CurMulticast
fi

echo " "
echo "Checking multicast address....."
ping $multicast 
pingresult=$?
while [ $pingresult -eq 0 ]
do
    cmulticast=$multicast
    echo " "
    echo "IP address $multicast is already in use.  "
    echo "Please specify a different swarmcache multicast IP address: \c: "
    read multicast
    if [ -z "$multicast" ]
    then
        multicast=$cmulticast
    fi
    echo "Checking multicast address....."
    ping $multicast 
    pingresult=$?
done


   

if [ "$multicast" != "$CurMulticast" ]
then
    echo "Setting $multicast in company/was/swarmcache.properties"
    sed "s/swarmcache.multicast.ip=${CurMulticast}/swarmcache.multicast.ip=${multicast}/" $directory/company/was/swarmcache.properties > $directory/company/was/swarmcache.properties.tmp
    mv $directory/company/was/swarmcache.properties.tmp $directory/company/was/swarmcache.properties
fi