#!/bin/bash -x


#BRAND=$1
#
#if [ "X${BRAND}" = "X" ]; then
#   echo "<<ERROR>> /nmops/bin/batchmediaUAT  <wn,wh,wb,wy,wf>"
#   exit 1
#fi

HOSTNAME=`/bin/hostname`
ENVIRONMENT_TO_DEPLOY_TO=uat
ACTION_TO_DO=all
SVN_BRANCH_NAME=ruum_rel_1.0.7 
##BUILDNUMBER=18

export ENVIRONMENT_TO_DEPLOY_TO
export ACTION_TO_DO
export SVN_BRANCH_NAME
export MOBILE_RELEASE_NUMBER
ID=`/usr/bin/id -u`


if [ ${ID} != "8820" ] ; then
   echo "Sorry you have user atg"
   exit 1
fi

if [ "X${HOSTNAME}" != "Xatgdeploy1" ]; then
   echo "Sorry you must be on atgdeploy1"
   exit 1
fi




export ATG_EAR_DIR=/apps/ears
export CLASSPATH=:/opt/netpoint/AccessServerSDK/oblix/lib/jobaccess.jar
export DYNAMO_DATA=/apps/jboss-eap-5.1/jboss-as/ATG-Data
export DYNAMO_HOME=/apps/ATG/ATG10.0.2/home
export DYNAMO_ROOT=/apps/ATG/ATG10.0.2
#export JAVA_HOME=/apps/jdk1.6.0_22
export JAVA_HOME=/usr/java/jdk1.6
export JBOSS_HOME=/apps/jboss-eap-5.1/jboss-as
export LD_LIBRARY_PATH=/apps/util/embedded/lib:/opt/netpoint/AccessServerSDK/oblix/lib
export OBACCESS_INSTALL_DIR=/opt/netpoint/AccessServerSDK







export ATG_EAR_DIR=/apps/ears
export CLASSPATH=:/opt/netpoint/AccessServerSDK/oblix/lib/jobaccess.jar
export DYNAMO_DATA=/apps/jboss-eap-5.1/jboss-as/ATG-Data
export DYNAMO_HOME=/apps/ATG/ATG10.0.2/home
export DYNAMO_ROOT=/apps/ATG/ATG10.0.2
#export JAVA_HOME=/apps/jdk1.6.0_22
export JAVA_HOME=/usr/java/jdk1.6
export JBOSS_HOME=/apps/jboss-eap-5.1/jboss-as
export LD_LIBRARY_PATH=/apps/util/embedded/lib:/opt/netpoint/AccessServerSDK/oblix/lib
export OBACCESS_INSTALL_DIR=/opt/netpoint/AccessServerSDK
#export PATH=/apps/jboss-eap-5.1/jboss-#as/bin:/apps/util/embedded/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/atg/bin:$JAVA_HOME/bin:/apps/apache-ant-#1.8.2/bin:/usr/libexec

hostname
whoami
id

DATESTART=`date`
export DATESTART


export BUILDNUMBER

# /apps/source/ruum_rel_0.0.1/deployment/Release-0.0.1/7
#
#get the build number from properties file ..
#RELEASE=5.2.6
#BUILDNUM=16
#external_release_number=$external_release_number
#build_number=$build_number

#echo $external_release_number
#echo $build_number



RELEASE=`echo ${SVN_BRANCH_NAME} | awk -F_ '{print $3}'`
. /apps/source/${SVN_BRANCH_NAME}/build/buildnumber.properties
BUILDNUM=$buildnumber

echo "Release $RELEASE}"
echo "Buildnumber ${BUILDNUM}"

  
# this is the deploy directory
echo /apps/source/${SVN_BRANCH_NAME}/deployment/Release-${RELEASE}/${BUILDNUM}


# over ride if build number is set
if [ ! -z $BUILDNUMBER ]
then
   BUILDNUM=$BUILDNUMBER
fi


echo "Release $RELEASE}"
echo "Buildnumber ${BUILDNUM}"

 
export RELEASE
export BUILDNUM


# starting deploy
#echo "deploying to ruum UAT" | mail -s "deploying to ruum UAT" rhill@speedfc.com


#echo "deploying to ruum UAT" \
#  | mail -s "deploying to ruum UAT" ruum_implementation@speedfc.com -- -r rhill@speedfc.com


prestageDeploy()
{


case $ENVIRONMENT_TO_DEPLOY_TO in
        all|uat)        
           #since they are all on the same servers  

           ## How to handle a patch..
           DIRR=/apps/source/${SVN_BRANCH_NAME}/deployment/Release-${RELEASE}/${BUILDNUM}
           rsync -avz -e ssh ${DIRR}/ atg@slruumapu02:/tmp/ruum-${RELEASE}.${BUILDNUM}/

           ssh atg@slruumapu01 "mkdir -p /tmp/ruum-${RELEASE}.${BUILDNUM}"
           ssh atg@slruumwbu01 "mkdir -p /tmp/ruum-${RELEASE}.${BUILDNUM}"
            

           if [ -f  ${DIRR}/ruum-static-assets.tgz ]
           then
                rsync -avz -e ssh ${DIRR}/ruum-static-assets.tgz   atg@slruumwbu01:/tmp/ruum-${RELEASE}.${BUILDNUM}
           fi 

           if [ -f  ${DIRR}/ruum.ear.tgz ]
           then
           rsync -avz -e ssh ${DIRR}/ruum.ear.tgz   atg@slruumapu01:/tmp/ruum-${RELEASE}.${BUILDNUM}
           fi

          ### now sync patch if there..
           if [ -f  ${DIRR}/ruum-patch.tgz ]
           then
           rsync -avz -e ssh ${DIRR}/ruum-patch.tgz   atg@slruumapu01:/tmp/ruum-${RELEASE}.${BUILDNUM}
           rsync -avz -e ssh ${DIRR}/ruum-patch.tgz   atg@slruumwbu01:/tmp/ruum-${RELEASE}.${BUILDNUM}
           fi
           

        ;;

      
       *)
        echo "unrecognized action"
        usage
        exit 1
        ;;

esac

}




stopInstances()
{




case $ENVIRONMENT_TO_DEPLOY_TO in
        all)
        

# shutdown apps 
##############################
echo "## slruumapu02...##"
ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
jbctl stopall
jbctl stopall
EOF


echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl stop ruum-store2
jbctl stop ruum-store1
jbctl stopall
jbctl stopall
EOF







        ;;

        uat)


# shutdown apps 
##############################
echo "## slruumapu02...##"
ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
jbctl stopall
jbctl stopall
EOF


echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl stop ruum-store2
jbctl stop ruum-store1
jbctl stopall
jbctl stopall
EOF


       ;;

       *)
        echo "unrecognized action"
        usage
        exit 1
        ;;

esac


}





deployInstances()
{


# this assumes the app is down.. before calling it.
# this is a lot of checking because you never know what kind of files your getting 
# could be a patch could be various with patch .. could be full build.
#

case $ENVIRONMENT_TO_DEPLOY_TO in
        all)
      



ssh atg@slruumwbu01 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployweb  $RELEASE  $BUILDNUM
EOF


ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployapu  $RELEASE  $BUILDNUM
EOF


ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployapu  $RELEASE  $BUILDNUM
EOF




  











        ;;

        uat)






ssh atg@slruumwbu01 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployweb  $RELEASE  $BUILDNUM
EOF


ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployapu  $RELEASE  $BUILDNUM
EOF


ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
sh -x /sfcops/deploy/uat/ruum/uat_deployapu  $RELEASE  $BUILDNUM
EOF





  





        ;;
       
       *)
        echo "unrecognized action"
        usage
        exit 1
        ;;

esac









}




startInstances()
{



case $ENVIRONMENT_TO_DEPLOY_TO in
       

all)


# Startup apps
##############################
echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl  start ruum-slm
# wait until it's all the way up..
#about 2 minutes
EOF

sleep 120



#While(true)
#do
#     startString=-`/bin/grep "Started in" /opt/app/jboss/server/${CPU_INST}/log/server.log`
#      if [ -n "$startString" ] ; then
#  curl or wget on port until up ... and sleep over and over. maybe this 


# you looking for
###2012-10-18 09:33:42,557 INFO  [org.jboss.bootstrap.microcontainer.ServerImpl] (main) JBoss (Microcontainer) [5.1.0 (build: ###SVNTag=JBPAPP_5_1_0 date=201009150028)] Started in 51s:215ms
#  grep "Started in"  /var/log/jboss/preprod2/preprod2-mpcs-slm/server.log
# this is how you know it's been started..
# you need to see started in the logs
# look for "Started in" in the logs instead of sleeps

 


# there are a lot of errors on startup and need to find common vs uncommon errors to see if a deployment fails.


echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl startall
EOF


# use ant to watch log files for startup .. 
#ant -f deployment.xml -Dserver.name=mpcs-admin start
#ant -f deployment.xml -Dserver.environment=qa -Dserver.name=mpcs-admin waitforInstanceStartUp


# the site should be up now for users..

# instead of the sleeps need to look at the log files and find out when it actually started..
sleep 120

echo "## slruumapu02...##"
ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
jbctl start ruum-aux
jbctl start ruum-staging
jbctl start ruum-pub
EOF








        
        ;;

        uat)



# Startup apps
##############################
echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl  start ruum-slm
# wait until it's all the way up..
#about 2 minutes
EOF

sleep 120



#While(true)
#do
#     startString=-`/bin/grep "Started in" /opt/app/jboss/server/${CPU_INST}/log/server.log`
#      if [ -n "$startString" ] ; then
#  curl or wget on port until up ... and sleep over and over. maybe this 


# you looking for
###2012-10-18 09:33:42,557 INFO  [org.jboss.bootstrap.microcontainer.ServerImpl] (main) JBoss (Microcontainer) [5.1.0 (build: ###SVNTag=JBPAPP_5_1_0 date=201009150028)] Started in 51s:215ms
#  grep "Started in"  /var/log/jboss/preprod2/preprod2-mpcs-slm/server.log
# this is how you know it's been started..
# you need to see started in the logs
# look for "Started in" in the logs instead of sleeps

 


# there are a lot of errors on startup and need to find common vs uncommon errors to see if a deployment fails.


echo "## slruumapu01...##"
ssh atg@slruumapu01 <<EOF
hostname
whoami
pwd
jbctl startall
EOF


# use ant to watch log files for startup .. 
#ant -f deployment.xml -Dserver.name=mpcs-admin start
#ant -f deployment.xml -Dserver.environment=qa -Dserver.name=mpcs-admin waitforInstanceStartUp


# the site should be up now for users..

# instead of the sleeps need to look at the log files and find out when it actually started..
sleep 120

echo "## slruumapu02...##"
ssh atg@slruumapu02 <<EOF
hostname
whoami
pwd
jbctl start ruum-aux
sleep 120
jbctl start ruum-staging
jbctl start ruum-pub
jbctl startall
EOF




        ;;
       *)
        echo "unrecognized action"
        usage
        exit 1
        ;;

esac


}






############################################
###
###  What kind of action do we want to do?
###
############################################

### What do we want to do?

case $ACTION_TO_DO in
        all)
         prestageDeploy   
         stopInstances
         deployInstances
         startInstances
        ;;
        prestageDeploy)
        prestageDeploy
        ;;

        deployInstances)
        deployInstances
        ;;

        restartInstances)
         stopInstances
         startInstances
        ;;

        startInstances)
        startInstances
        ;;

        stopInstances)
        stopInstances
        ;;

        *)
        echo "unrecognized action"
        usage
        exit 1
        ;;

esac









echo "Date Started"
echo $DATESTART


echo "Date Ended"
date


#echo "RUUM UAT --COMPLETE " \
#  | mail -s "Deploy ruum UAT COMPLETE" ruum_implementation@speedfc.com -- -r rhill@speedfc.com


