#!/bin/ksh

AWK=/usr/bin/awk
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
MKDIR=/usr/bin/mkdir

##############################################################
#
# Test if the sdk package is installed.
#
##############################################################
check_for_sdk() {
   pkginfo -q SUNWamsdk
   if [ $? -eq 0 ]; then
      sdk="yes"
   else
      sdk="no"
   fi
}

##############################################################
#
# Test if the sdk package is installed.
#
##############################################################
check_for_fcd() {
   pkginfo -q SUNWamfcd
   if [ $? -eq 0 ]; then
      fcd="yes"
   else
      fcd="no"
   fi
}

##############################################################
#
# Test if cross domain sso package is installed.
#
##############################################################
check_for_cds() {
   pkginfo -q SUNWamcds
   if [ $? -eq 0 ]; then
      cds="yes"
   else
      cds="no"
   fi
}

##############################################################
#
# Test if cross domain sso package is installed.
#
##############################################################
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 1
         fi
     fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
}


##############################################################
#
# used by the license accept routine to get a valid yes or no
# answer.
#
##############################################################
yesno() {
  ok="no"
 
  question=$1
  default=$2

  valids="ynYN"

  while [ "$ok" = "no" ]; do
    if [ "$default" != "${default%y}" ] || [ "$default" != "${default%Y}" ]
    then
      $ECHO "$question [y]/n \c"
      default="y"
    elif [ "$default" != "${default%n}" ] || [ "$default" != "${default%N}" ]
    then
      $ECHO "$question y/[n] \c"
      default="n"
    else
      $ECHO "$question y/n \c"
      default=""
    fi

    read ans


    if [ "$ans" = "" ]; then
      if [ "$default" != "" ]; then
        ans=$default
      fi 
    fi  

    if [ "$ans" != "${ans%[$valids]}" ]; then
      ok="yes"
    fi

    if [ "$ok" = "no" ]; then
      $ECHO
      $ECHO "Invalid response"
      $ECHO
    fi

  done

    # make sure we have a std response so client doesn't need to check

    if [ "$ans" = "y" ]; then
      ans="y"
    elif [ "$ans" = "n" ]; then
      ans="n"
    elif [ "$ans" = "N" ]; then
      ans="n"
    fi
}

##############################################################
#
# Get the users response to accepting the license
#
##############################################################
license_accept() {
  more LICENSE.TXT
  $ECHO
  yesno "Do you agree to the license terms?" "n"
 
  if [ "$ans" = "n" ]; then
    exit 1
  fi
}

##############################################################
#
# main start
#
##############################################################
rm -f /tmp/.patch.ans 2>/dev/null 1>/dev/null
touch /tmp/.patch.ans

   license_accept 

   DIR_MANAGER="cn=Directory Manager"
   $ECHO
   $ECHO "What is the dn of the Directory Manager [$DIR_MANAGER] \c"
   read ANS
   if [ "$ANS" != "" ]; then
      DIR_MANAGER=$ANS
   fi
   $ECHO "DIR_MANAGER=$DIR_MANAGER" >> /tmp/.patch.ans

   while [ 1 ];do
    $ECHO "What is the password for the Directory Manager [] \c"
    stty -echo
    read PASSWORD_ANS
    stty echo
    if [ "$PASSWORD_ANS" != "" ]; then
      PASSWORD=$PASSWORD_ANS
      $ECHO
      break
    else
      $ECHO "\nPlease enter the password."
    fi
    $ECHO
   done
   $ECHO "DS_PASSWORD=$PASSWORD_ANS" >> /tmp/.patch.ans

get_base_dir
if [ ! -d $BASE_DIR/SUNWam/ldaplib/ldapsdk ]; then
   $ECHO "Can't locate the LDAP lib at $BASE_DIR/SUNWam/ldaplib/ldapsdk"
   exit 0
fi

LD_LIBRARY_PATH=$BASE_DIR/SUNWam/ldaplib/ldapsdk:$BASE_DIR/SUNWam/lib/jss:/usr/lib/mps/secv1:/usr/lib/mps
export LD_LIBRARY_PATH

INSTALL_DIR=$BASE_DIR/SUNWam
DS_HOST=`$GREP com.iplanet.am.directory.host $INSTALL_DIR/lib/AMConfig.properties | $AWK ' BEGIN { FS="=" } {print $2}`
DS_PORT=`$GREP com.iplanet.am.directory.port $INSTALL_DIR/lib/AMConfig.properties | $AWK ' BEGIN { FS="=" } {print $2}`
DS_SSL=`$GREP com.iplanet.am.directory.ssl.enabled $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.directory.ssl.enabled=//g"`
CERTDB_DIR=`$GREP com.iplanet.am.admin.cli.certdb.dir $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.admin.cli.certdb.dir=//g"`
CERTDB_PREFIX=`$GREP com.iplanet.am.admin.cli.certdb.prefix  $INSTALL_DIR/lib/AMConfig.properties | $SED -e "s/com.iplanet.am.admin.cli.certdb.prefix=//g"`
CERTDB=$CERTDB_DIR/"$CERTDB_PREFIX"cert7.db


cd $INSTALL_DIR/bin
if [ "$DS_SSL" = "true" ]; then
   ./ldapsearch -Z -o -P $CERTDB -b "cn=schema" -D "$DIR_MANAGER" -w "$PASSWORD" -p $DS_PORT -h $DS_HOST "(objectclass=*)" > /dev/null 2>&1
else
   ./ldapsearch -b "cn=schema" -D "$DIR_MANAGER" -w "$PASSWORD" -p $DS_PORT -h $DS_HOST "(objectclass=*)" > /dev/null 2>&1
fi

if [ $? -ne 0 ]; then
   $ECHO
   $ECHO "Could not connect to the Directory Server. Please make"
   $ECHO "sure the user name and password are correct and that"
   $ECHO "the Directory Server is running."
   exit 1
fi

# check using AS or WS
if [ -f $BASE_DIR/SUNWam/lib/AMConfig.properties ];then
	flag=`$GREP "^com.sun.identity.webcontainer=" $BASE_DIR/SUNWam/lib/AMConfig.properties |nawk -F=  '{print $NF}'`
	if [ $flag = "IAS7.0" ];then	# AS based
	       while [ 1 ];do
		AS7_BASE="/"
   		$ECHO "In general, enter / if AS is installed in /opt/SUNWappserver7"
   		$ECHO "What is the rootdir of Application Server [$AS7_BASE] \c"
   		read ANS
   		if [ "$ANS" != "" ]; then
      			AS7_BASE=$ANS
   		fi		
		if [ -x $AS7_BASE/opt/SUNWappserver7/bin/asadmin ];then
   			$ECHO "AS7_BASE=$AS7_BASE" >> /tmp/.patch.ans
			break
		else
			$ECHO "\nBad AS basedir entered !"
		fi
	       done

	       while [ 1 ];do
		AS7_DOMAIN="domain1"
   		$ECHO
   		$ECHO "What is the domain of Application Server [$AS7_DOMAIN] \c"
   		read ANS
   		if [ "$ANS" != "" ]; then
      			AS7_DOMAIN=$ANS
   		fi		
		if [ -d /var/opt/SUNWappserver7/domains/$AS7_DOMAIN ];then
   			$ECHO "AS7_DOMAIN=$AS7_DOMAIN" >> /tmp/.patch.ans
			break
		else
			$ECHO "\nBad AS DOMAIN entered !"
		fi
	       done

	       while [ 1 ];do
		AS7_INSTANCE="server1"
   		$ECHO
   		$ECHO "What is the instance of Application Server [$AS7_INSTANCE] \c"
   		read ANS
   		if [ "$ANS" != "" ]; then
      			AS7_INSTANCE=$ANS
   		fi		
		if [ -x /var/opt/SUNWappserver7/domains/$AS7_DOMAIN/$AS7_INSTANCE/bin/startserv ];then
   			$ECHO "AS7_INSTANCE=$AS7_INSTANCE" >> /tmp/.patch.ans
			break
		else
			$ECHO "\nBad AS INSTANCE entered !"
		fi
	       done

		AS7_ADMPORT="4848"
   		$ECHO
   		$ECHO "What is the port number of admin instance of AS [$AS7_ADMPORT] \c"
   		read ANS
   		if [ "$ANS" != "" ]; then
      			AS7_ADMPORT=$ANS
   		fi		
   		$ECHO "AS7_ADMPORT=$AS7_ADMPORT" >> /tmp/.patch.ans

		AS7_ADMINID="admin"
   		$ECHO
   		$ECHO "What is the user name of admin instance of AS [$AS7_ADMINID] \c"
   		read ANS
   		if [ "$ANS" != "" ]; then
      			AS7_ADMINID=$ANS
			break
   		fi		
   		$ECHO "AS7_ADMINID=$AS7_ADMINID" >> /tmp/.patch.ans

   		$ECHO "WEB_CONTAINER=AS7" >> /tmp/.patch.ans

	else				# WS based
	       while [ 1 ];do
		WS61_BASE="/sample"
		WS61_INSTANCE=""

   		$ECHO
   		$ECHO "What is the dirname where WS 6.1 installed [$WS61_BASE] \c"
   		read ANS
   		if [ -d "/$ANS" ]; then
      			WS61_BASE=$ANS
   		fi		

   		$ECHO
   		$ECHO "What is the subdirname where WS 6.1 instance installed under $WS61_BASE [] \c"
   		read ANS
   		if [ -d "$WS61_BASE/$ANS" ]; then
      			WS61_INSTANCE=$ANS
   		fi		
		if [ -x $WS61_BASE/$WS61_INSTANCE/start ];then
   			$ECHO "WS61_BASE=$WS61_BASE" >> /tmp/.patch.ans
			num=`grep "<VS id" $WS61_BASE/$WS61_INSTANCE/config/server.xml |wc -l |nawk '{print $1}'`
			if [ ${num:-1} -eq 1 ];then
			 VS_ID=`$WS61_BASE/bin/https/bin/parsexml $WS61_BASE/$WS61_INSTANCE/config VSCLASS/VS id`
			else
				$ECHO "WARNING: More than one VS entries found in $WS61_BASE/$WS61_INSTANCE/config/server.xml"
				$ECHO "enter VS id value where IS was deployed at installation time [] : \c"
				read VS_ID
			fi
   			$ECHO "WS61_INSTANCE=$VS_ID" >> /tmp/.patch.ans
			break
		else
			$ECHO "\nBad WS 6.x basedir entered !"
		fi
	       done
   	       $ECHO "WEB_CONTAINER=WS6" >> /tmp/.patch.ans
	fi

	# shared values
	IS_SERVER_URI=`$GREP "^com.iplanet.am.services.deploymentDescriptor" $BASE_DIR/SUNWam/lib/AMConfig.properties | sed -e 's/com.iplanet.am.services.deploymentDescriptor=\///g'`
   	$ECHO "IS_SERVER_URI=$IS_SERVER_URI" >> /tmp/.patch.ans

        IS_CONSOLE_URI=`$GREP "^com.iplanet.am.console.deploymentDescriptor" $BASE_DIR/SUNWam/lib/AMConfig.properties | sed -e 's/com.iplanet.am.console.deploymentDescriptor=\///g'`
   	$ECHO "IS_CONSOLE_URI=$IS_CONSOLE_URI" >> /tmp/.patch.ans
else
   $ECHO
   $ECHO "This system cannot be upgraded do to a missing resource. The file"
   $ECHO "$BASE_DIR/SUNWam/lib/AMConfig.properties could not be located."
   $ECHO
   exit 1
fi
	

# make all subdirs for new patch new files structure related
if [ ! -d $BASE_DIR/SUNWam/include ];then
 mkdir -p $BASE_DIR/SUNWam/include
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/api/Cert ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/api/Cert
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/api/LDAP ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/api/LDAP
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/api/jcdi ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/api/jcdi
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/spi/jcdi ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/spi/jcdi
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/spi/jdbc ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/spi/jdbc
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/spi/msisdn ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/spi/msisdn
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/spi/postprocess ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/spi/postprocess
fi

if [ ! -d $BASE_DIR/SUNWam/samples/authentication/spi/providers ];then
 mkdir -p $BASE_DIR/SUNWam/samples/authentication/spi/providers
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample1/idp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample1/idp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample1/idp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample1/idp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample1/sp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample1/sp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample1/sp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample1/sp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/idp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample2/sp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/idp2/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp1/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/WEB-INF/classes ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/WEB-INF/classes
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/images ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/images
fi

if [ ! -d $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/css ];then
 mkdir -p $BASE_DIR/SUNWam/samples/liberty/sample3/sp2/css
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/paos ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/paos
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/bin ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/bin
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/jsp ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/jsp
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/ldif ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/ldif
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/ep ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/ep
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/xml ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/xml
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/xsd ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/sis-ep/src/xsd
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/wsc ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/wsc
fi

if [ ! -d $BASE_DIR/SUNWam/samples/phase2/wsc ];then
 mkdir -p $BASE_DIR/SUNWam/samples/phase2/wsc
fi

if [ ! -d $BASE_DIR/SUNWam/samples/csdk ];then
 mkdir -p $BASE_DIR/SUNWam/samples/csdk
fi


