#!/bin/sh 

NAWK=/usr/bin/nawk
ECHO=/usr/bin/echo
MV=/usr/bin/mv
CP=/usr/bin/cp
RM=/usr/bin/rm
GREP=/usr/bin/grep
SED=/usr/bin/sed
TOUCH=/usr/bin/touch
MKD="/usr/bin/mkdir -p"

VERSION_FILE=.version

check_for_con() {
   pkginfo -q SUNWamcon
   if [ $? -eq 0 ]; then
      con="yes"
   else
      con="no"
   fi
}

check_for_sdk() {
   pkginfo -q SUNWamsdk
   if [ $? -eq 0 ]; then
      sdk="yes"
   else
      sdk="no"
   fi
}

check_for_iws() {
   pkginfo -q SUNWamsws
   if [ $? -eq 0 ]; then
      iws="yes"
   else
      iws="no"
   fi
}

check_for_fcd() {
   pkginfo -q SUNWamfcd
   if [ $? -eq 0 ]; then
      fcd="yes"
   else
      fcd="no"
   fi
}

check_for_cds() {
   pkginfo -q SUNWamcds
   if [ $? -eq 0 ]; then
      cds="yes"
   else
      cds="no"
   fi
}

check_for_sam() {
   pkginfo -q SUNWamsam
   if [ $? -eq 0 ]; then
      sam="yes"
   else
      sam="no"
   fi
}

check_for_sac() {
   pkginfo -q SUNWamsac
   if [ $? -eq 0 ]; then
      sac="yes"
   else
      sac="no"
   fi
}

check_for_wlc() {
   pkginfo -q SUNWamwlc
   if [ $? -eq 0 ]; then
      wlc="yes"
   else
      wlc="no"
   fi
}

check_for_wsc() {
   pkginfo -q SUNWamwsc
   if [ $? -eq 0 ]; then
      wsc="yes"
   else
      wsc="no"
   fi
}


get_base_dir() {
  check_for_sdk
  if [ "$sdk" = "yes" ]; then
    pkg="SUNWamsdk"
  else
     check_for_fcd
     if [ "$fcd" = "yes" ]; then
        pkg="SUNWamfcd"
     else
         check_for_cds
         if [ "$cds" = "yes" ]; then
           pkg="SUNWamcds"
         else
           exit
         fi
     fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
}

##############################################
#
restore_file() {
    file=$1
    backupVersion=.$file-version-61-20
    backupFile=.$file.bak

    if [ -f $file ]; then
	$MV $file $backupFile
    fi

    if [ -f $backupVersion ]; then
	$MV $backupVersion $file
	$TOUCH $file
    fi
}

##############################################
# main start

TMPDIR=/tmp/IS61sp1
/usr/bin/mkdir -p $TMPDIR

get_base_dir
INSTALL_DIR=$BASE_DIR/SUNWam
LD_LIBRARY_PATH=$BASE_DIR/SUNWam/ldaplib/solaris/sparc/ldapsdk:$BASE_DIR/SUNWam/lib/solaris/sparc/jss:/usr/lib/mps/secv1:/usr/lib/mps
AM_AUTH_DIR=$INSTALL_DIR/web-apps/services/config/auth

check_for_sac
if [ "$sac" = "yes" ]; then
   IAS7INSTANCEDIR=`$GREP IAS7INSTANCEDIR /var/sadm/pkg/SUNWamsac/pkginfo | $SED -e "s/IAS7INSTANCEDIR=//g"`
   IAS7INSTANCE=`$GREP IAS7INSTANCE /var/sadm/pkg/SUNWamsac/pkginfo | $SED -e "s/IAS7INSTANCE=//g"`
   IAS7ADMINPORT=`$GREP IAS7_ADMINPORT /var/sadm/pkg/SUNWamsac/pkginfo | $SED -e "s/IAS7_ADMINPORT=//g"`

   # redeploy iAS
   if [ `$GREP ${IS_SERVER_URI:-amserver}_1 $IAS7INSTANCEDIR/config/server.xml |wc -l| $NAWK '{print $1}'` -gt 0 ];then
   	#IS_SERVER_URI="amserver"
	$ECHO ""
   else
	# can't find the URI in patch answer file during patchadd time from AS config
   	$ECHO "What is the URI of IS services [$IS_SERVER_URI] \c"
   	read ANS
   	if [ "$ANS" != "" ]; then
           	IS_SERVER_URI=$ANS
		break
   	fi
   fi
   if [ `$GREP ${IS_CONSOLE_URI:-amconsole}_1 $IAS7INSTANCEDIR/config/server.xml |wc -l| $NAWK '{print $1}'` -gt 0 ];then
   	#IS_CONSOLE_URI="amconsole"
	$ECHO ""
   else
	# can't find the URI in patch answer file during patchadd time from AS config
   	$ECHO "What is the URI of IS console [$IS_CONSOLE_URI] \c"
   	read ANS
   	if [ "$ANS" != "" ]; then
           	IS_CONSOLE_URI=$ANS
   	fi
   fi
   if [ `$GREP ${IS_PASSWD_URI:-ampassword}_1 $IAS7INSTANCEDIR/config/server.xml |wc -l| $NAWK '{print $1}'` -gt 0 ];then
   	#IS_PASSWD_URI="ampassword"
	$ECHO ""
   else
	# can't find the URI in patch answer file during patchadd time from AS config
   	$ECHO "What is the URI of IS password [$IS_PASSWD_URI] \c"
   	read ANS
   	if [ "$ANS" != "" ]; then
           	IS_PASSWD_URI=$ANS
   	fi
   fi

   $ECHO ""
   AS7_ADMINID="admin"
   $ECHO "What is the AS admin user id [$AS7_ADMINID] \c"
   read ANS
   if [ "$ANS" != "" ]; then
          AS7_ADMINID=$ANS
   fi
   AS7_ADMINPASSWD=""
   $ECHO "What is the AS admin user password [$AS7_ADMINPASSWD] \c"
   stty -echo
   read ANS
   stty echo
   if [ "$ANS" != "" ]; then
          AS7_ADMINPASSWD=$ANS
   fi

   # setup env for redeployment
   IAS7_BASE=`pkgparam SUNWasro BASEDIR`
   PATH=$IAS7_BASE/bin:/usr/bin:/usr/sbin:/bin:/usr/openwin/bin:.
   export PATH
   LD_LIBRARY_PATH=$IAS7_BASE/lib:/usr/lib:/lib:/usr/openwin/lib:.
   export LD_LIBRARY_PATH

   # discard all manual changes before redeployment
   asadmin reconfig --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --discardmanualchanges=true ${AS7_INSTANCE:-server1}

   $ECHO "Undeploying IS ..."
   asadmin undeploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web ${IS_SERVER_URI:-amserver}

   asadmin undeploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web ${IS_CONSOLE:-amconsole}

   #asadmin undeploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web ${IS_PASSWD_URI:-ampassword}

   BASEDIR=`pkgparam SUNWamsvc BASEDIR`
   $ECHO "Deploying IS ..."
   asadmin deploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web --contextroot ${AS7_SERVER_URI:-amserver} --name ${AS7_SERVER_URI:-amserver} $BASEDIR/SUNWam/services.war

   asadmin deploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web --contextroot ${IS_CONSOLE_URI:-amconsole} --name ${IS_CONSOLE_URI:-amconsole} $BASEDIR/SUNWam/console.war
   INDEXPAGE=$IAS7INSTANCEDIR/applications/j2ee-modules/${IS_SERVER_URI:-amconsole}_1/index.html

   #asadmin deploy --user ${AS7_ADMINID:-admin} --password ${AS7_ADMINPASSWD:-11111111} --instance ${AS7_INSTANCE:-server1} --type web --contextroot ${AS7_PASSWD_URI:-ampassword} --name ${AS7_PASSWD_URI:-ampassword} $BASEDIR/SUNWam/password.war

   AS7_DOMAIN=`$GREP AS7_DOMAIN /tmp/.patch.ans | tail -1| $SED -e 's/AS7_DOMAIN=//g'`
   cd $IAS7INSTANCEDIR/applications/j2ee-modules/${IS_SERVER_URI:-amserver}_1/
   $MV index.html index.html+
   $SED -e "s/SERVER_DEPLOY_URI/\/${IS_SERVER_URI:-amserver}/g" index.html+ > index.html
   $RM -f index.html+

   $ECHO "All restore of IS done."
fi

check_for_wlc
if [ "$wlc" = "yes" ]; then
   WL61_BASE=`$GREP WL61_BASE /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_BASE=//g"`
   WL61_PROD_DIR=`$GREP WL61_PROD_DIR /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_PROD_DIR=//g"`
   WL61_DOMAIN=`$GREP WL61_DOMAIN /var/sadm/pkg/SUNWamwlc/pkginfo | $SED -e "s/WL61_DOMAIN=//g"`
   cd ${WL61_BASE}/${WL61_PROD_DIR}/config/${WL61_DOMAIN}
   restore_file startWebLogic.sh
   restore_file startManagedWebLogic.sh
fi

check_for_wsc
if [ "$wsc" = "yes" ]; then
   $ECHO "restore file SUNWamwsc classpath"
fi

check_for_iws
if [ "$iws" = "yes" ]; then
 if [ -f /tmp/.patch.ans ];then
   WS61_BASE=`$GREP WS61_BASE /tmp/.patch.ans | tail -1| $SED -e "s/WS61_BASE=//g"`
   WS61_INSTANCE=`$GREP WS61_INSTANCE /tmp/.patch.ans | tail -1| $SED -e "s/WS61_INSTANCE=//g"`
   IS_SERVER_URI=`$GREP IS_SERVER_URI /tmp/.patch.ans | tail -1| $SED -e "s/IS_SERVER_URI=//g"`
   WS61_CONFIG_BASE=$WS61_BASE/$WS61_INSTANCE/config
   cd $WS61_CONFIG_BASE
   IS_BASE=`pkgparam SUNWamsvc BASEDIR`

   PATH=$WS61_BASE/bin/https/bin:/usr/bin:/usr/sbin:/bin:.
   export PATH
   LD_LIBRARY_PATH=$WS61_BASE/bin/https/lib:/usr/lib:/lib:/usr/openwin/lib:.
   export LD_LIBRARY_PATH

   for locale_dir in de es fr ja ko zh_CN zh_TW
   do
        if [ -d $AM_AUTH_DIR/default_$locale_dir ]; then
        echo "backing up $AM_AUTH_DIR/default_$locale_dir"
        $MKD $TMPDIR/default_$locale_dir
        $CP $AM_AUTH_DIR/default_$locale_dir/*.xml $TMPDIR/default_$locale_dir/
        fi
   done
   wdeploy delete -u /$IS_SERVER_URI -i $WS61_INSTANCE -v $WS61_INSTANCE -n hard

   wdeploy deploy -u /$IS_SERVER_URI -i $WS61_INSTANCE -v $WS61_INSTANCE -d $IS_BASE/SUNWam/web-apps/services $IS_BASE/SUNWam/services.war

   check_for_con
   if [ "$con" = "yes" ]; then
     wdeploy delete -u /${IS_CONSOLE_URI:-amconsole} -i $WS61_INSTANCE -v $WS61_INSTANCE -n hard
     wdeploy deploy -u /${IS_CONSOLE_URI:-amconsole} -i $WS61_INSTANCE -v $WS61_INSTANCE -d $BASE_DIR/SUNWam/web-apps/applications $BASE_DIR/SUNWam/console.war
     INDEXPAGE=$BASE_DIR/SUNWam/web-apps/applications/index.html
   fi

   for locale_dir in de es fr ja ko zh_CN zh_TW
   do
        if [ -d $TMPDIR/default_$locale_dir ];then
          echo "Restoring default_$locale_dir"
          $MKD $AM_AUTH_DIR/default_$locale_dir
          $CP $TMPDIR/default_$locale_dir/*.xml $AM_AUTH_DIR/default_$locale_dir/
          #$RMF $TMPDIR/default_$locale_dir
        fi
   done
 
   # tag swap after restore
   $MV $IS_BASE/SUNWam/web-apps/services/index.html $IS_BASE/SUNWam/web-apps/services/index.html+
   $SED -e "s/SERVER_DEPLOY_URI/\/${IS_SERVER_URI:-amserver}/g" $IS_BASE/SUNWam/web-apps/services/index.html+ > $IS_BASE/SUNWam/web-apps/services/index.html
   $RM -f $IS_BASE/SUNWam/web-apps/services/index.html+
 else
   $ECHO "Please do redeployment of IS services, console and password manually"
   $ECHO "Since the WS location info are missed"
 fi
fi

if [ x$INDEXPAGE != "x" ]; then
  $ECHO "Updating $INDEXPAGE"
  mv $INDEXPAGE $INDEXPAGE+
  sed -e "s/CONSOLE_DEPLOY_URI/\/${is_cons_uri:-amconsole}/g" $INDEXPAGE+ > $INDEXPAGE
  rm -f $INDEXPAGE+
fi

rm -f /tmp/.patch.ans 2>/dev/null 1>/dev/null
