#!/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
GREP=/bin/grep
SED=/usr/bin/sed
TOUCH=/usr/bin/touch

VERSION_FILE=.version
PATCH_ID=115766-07
BKFILESUFFIX=-pre-$PATCH_ID

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

check_server_xml()
{
conf_file=${1:-server.xml}
file_type=${2:-xml}
container=${3:-ws61}

if [ "$container" = "ws61" ];then
 suffixstring="classpathsuffix"
elif [ "$container" = "as70" ];then
 suffixstring="classpath-suffix"
elif [ "$container" = "was51" ];then
 suffixstring="classpath"
fi

  exist=0
  #step1: grep for classpath suffix in server.xml
  file=$conf_file
  classpath=`grep $suffixstring $file`

  #step2: Get the number of tokens in the <JAVA> .. </JAVA>
  var=`echo $classpath | nawk ' { print NF } '`

  #step3: Add our classpath to the existing Classpathsuffix in server.xml
  count=1
  newline=""
  while [ $count -le $var ]
  do
      currentToken=`echo $classpath | cut -f$count -d " "`
      classpathToken=`echo $currentToken | cut -f$count -d " " | grep "$suffixstring" |grep "am_sdk.jar"`
      if [ "$classpathToken" != "" ]; then
          exist=1
      fi
      count=`expr $count + 1`
  done

return $exist
}

###############################################
# determine web container type
###############################################

check_web_container() {
  WEB_CONTAINER=`$GREP com.sun.identity.webcontainer= $AMCONFIG | sed -e "s#com.sun.identity.webcontainer=##"`
  $ECHO
  if [ $WEB_CONTAINER = "IAS7.0" ]; then
    AS7_INSTANCE=`$GREP '^com.iplanet.am.admin.cli.certdb.dir' $AMCONFIG \
        | $SED -e 's/com.iplanet.am.admin.cli.certdb.dir=//g' \
        | $SED -e 's/\/config//g'`
    while [ 1 ]; do
      $ECHO "What is the path of Application Server instance [$AS7_INSTANCE] \c"
      read ANS
      if [ x$ANS != "x" ]; then
        AS7_INSTANCE=$ANS
      fi
      if [ -x $AS7_INSTANCE/bin/startserv ]; then
        break
      else
        $ECHO "\nBad AS INSTANCE entered !"
        $ECHO
      fi
    done

  # WS6.1
  else
    while [ 1 ]; do
      WS61_INSTANCE="https-`hostname`.`domainname`"
      WS61_DIR="$BASE_DIR/SUNWwbsvr/$WS61_INSTANCE"
      $ECHO "What is the path of the WS 6.1 instance [$WS61_DIR] \c"
      read ANS
      if [ x$ANS != "x" ]; then
        WS61_DIR=$ANS
      fi
      if [ -x $WS61_DIR/start ]; then
        break
      else
        $ECHO "\nBad WS 6.x basedir entered !"
        $ECHO
      fi
    done
  fi

  # shared values
  IS_SERVICES_URI=`$GREP "^com.iplanet.am.services.deploymentDescriptor" $AMCONFIG | $SED -e 's/com.iplanet.am.services.deploymentDescriptor=\///g'`

  IS_CONSOLE_URI=`$GREP "^com.iplanet.am.console.deploymentDescriptor" $AMCONFIG | $SED -e 's/com.iplanet.am.console.deploymentDescriptor=\///g'`
}

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
}

check_for_svc() {
   pkginfo -q SUNWamsvc
   if [ $? -eq 0 ]; then
      svc="yes"
   else
      svc="no"
   fi
}

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

check_for_consdk() {
   pkginfo -q SUNWamconsdk
   if [ $? -eq 0 ]; then
      consdk="yes"
   else
      consdk="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 files from backups
###############################################

restore_file() {
  file=$1
  backupVersion=$file$BKFILESUFFIX
  backupFile=$file.bak

  if [ -f $backupVersion ]; then
    $ECHO "Restoring `pwd`/$file"
    if [ -f $file ]; then
      $MV $file $backupFile
    fi
    $MV $backupVersion $file
    $TOUCH $file
  else
    $ECHO "Cannot find backup file of $file"
  fi
}

###############################################
# Restore am_auth_ui.jar and jsp files
###############################################

config_services_cp() {
  # am_auth_ui.jar
  restore_file WEB-INF/lib/am_auth_ui.jar
  # jsp files
  restore_file config/auth/default/membership.jsp
  restore_file config/auth/default/Login.jsp
  restore_file config/auth/default/new_org.jsp
}

config_services() {
  $ECHO
  $ECHO "Restoring am_auth_ui.jar, Login.jsp, membership.jsp and new_org.jsp"
  check_for_svc
  if [ "$svc" = "no" ]; then
    $ECHO "SUNWamsvc is not installed, no services.war ..."
    return
  fi

  cd $INSTALL_DIR/web-src/services/
  config_services_cp

  if [ $WEB_CONTAINER = "IAS7.0" ]; then
    AMSERVERDIR=$AS7_INSTANCE/applications/j2ee-modules/${IS_SERVICES_URI:-amserver}_1
  else
    AMSERVERDIR=$WS61_DIR/is-web-apps/services
  fi

  while [ ! -f $AMSERVERDIR/WEB-INF/lib/am_auth_ui.jar ]
  do
    $ECHO
    $ECHO "What is the path of the deployment directory of /${IS_SERVICES_URI:-amserver} [$AMSERVERDIR] ? \c"
    read ANS
    if [ x$ANS != "x" ]; then
      AMSERVERDIR=$ANS
    fi
  done
  cd $AMSERVERDIR
  config_services_cp

  cd $AMSERVERDIR
  restore_file index.html
}

###############################################
# Restore am_console.jar
###############################################

config_console() {
  $ECHO
  $ECHO "Restoring am_console.jar"
  check_for_con
  if [ "$con" = "no" ]; then
    $ECHO "SUNWamcon is not installed, no am_console.jar ..."
    return
  fi

  if [ $WEB_CONTAINER = "IAS7.0" ]; then
    file=$AS7_INSTANCE/applications/j2ee-modules/${IS_CONSOLE_URI:-amconsole}_1
  else
    file=$WS61_DIR/is-web-apps/applications
  fi

  while [ ! -f $file/WEB-INF/lib/am_console.jar ]
  do
    $ECHO
    $ECHO "What is the path of the deployment directory of /${IS_CONSOLE_URI:-amconsole} [$file] ? \c"
    read ANS
    if [ x$ANS != "x" ]; then
      file=$ANS
    fi
  done

  cd $file/WEB-INF/lib
  restore_file am_console.jar
}

###############################################
# Restore server.xml
###############################################

update_classpath() {
  if [ $WEB_CONTAINER != "IAS7.0" ]; then
    cd $WS61_DIR/config
  else
    cd $AS7_INSTANCE/config
  fi
  restore_file server.xml
}

###############################################
# update the .version string
###############################################

update_version_string() {
  cd /etc/opt/SUNWam/config
  if [ -f $VERSION_FILE ]; then
    $GREP -v $PATCH_ID $VERSION_FILE > $VERSION_FILE-tmp
    $MV $VERSION_FILE-tmp $VERSION_FILE
  else
    $ECHO "Sun Java System Identity Server version 2004Q2" > $VERSION_FILE
  fi
}

###############################################
# Main start
###############################################

get_base_dir
INSTALL_DIR=$BASE_DIR/SUNWam
AMCONFIG=/etc/opt/SUNWam/config/AMConfig.properties
check_web_container

###############################################
# Restore xalan jars 
###############################################

cd $INSTALL_DIR/lib
#restore_file  jaxp-api.jar
#restore_file  dom.jar 
#restore_file  sax.jar 
#restore_file  xsltc.jar 

#ln -s "${INSTALL_DIR}/lib/jaxp-api.jar" "${INSTALL_DIR}/lib/endorsed/jaxp-api.jar" 2>/dev/null
#ln -s "${INSTALL_DIR}/lib/dom.jar" "${INSTALL_DIR}/lib/endorsed/dom.jar" 2>/dev/null
#ln -s "${INSTALL_DIR}/lib/sax.jar" "${INSTALL_DIR}/lib/endorsed/sax.jar" 2>/dev/null
#ln -s "${INSTALL_DIR}/lib/xsltc.jar" "${INSTALL_DIR}/lib/endorsed/xsltc.jar" 2>/dev/null

rm -f ${INSTALL_DIR}/lib/endorsed/xml-apis.jar

config_services
config_console
update_classpath
update_version_string
