#!/bin/ksh

###############################################
# Sourcing macros
DIRNAME=/usr/bin/dirname
. `$DIRNAME $0`/../lib/psmacros
###############################################

###############################################
# Defines
###############################################

OMIT_CHAR='\c'
BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

###############################################
# Question
###############################################
Question() {
  local PROMPT=$1
  local DEFAULT=$2
  local VALID_CHARS=$3
  local DONE=""
  local CHECK=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    $ECHO "$PROMPT [$DEFAULT] $OMIT_CHAR"
    read ANSWER
    if [ "$ANSWER" = "" ]; then
      ANSWER=$DEFAULT
      DONE="y"
    else
      CHECK=`$ECHO $ANSWER | $GREP "[^$VALID_CHARS]"`
      if [ "$CHECK" = "" ]; then
        DONE="y"
      else
        $ECHO "Invalid answer! $BELL_CHAR"
      fi
    fi
  done
}

###############################################
# Get password
###############################################
GetPassword() {
  local PROMPT=$1
  local DONE=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    eval print "$PROMPT \$OMIT_CHAR"
    $STTY -echo
    read ANSWER
    $STTY echo
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done
}

###############################################
# Do search deploy
###############################################
DoSearchDeploy() {
  local ROOT_DIR=""
  local SAVE_DIR=""
  local LIB_DIR=""
  local CLASSES=""
  local APPSERVER=""
  local FC_URL=""
  local FILE=""
  local PARAMS=""
  local SETUP=""
  local AMADMIN=""

  ROOT_DIR=`$ECHO $PS_VAR_DIR/https-$DEPLOY_INSTANCE/$DEPLOY_URI | $SED -e "s.//./.g"`
  if [ -d "$ROOT_DIR" ]; then
    SAVE_DIR=`$ECHO "$ROOT_DIR" | $SED -e "s/https-/SAVE-https-/"`
    if [ -d "$SAVE_DIR" ]; then
      $RM -rf "$SAVE_DIR"
    fi
    $MKDIR -p "$SAVE_DIR"
    $CP "$ROOT_DIR"/* "$SAVE_DIR"
  fi
  $MKDIR -p "$ROOT_DIR"

  LIB_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/lib"
  CLASSES=".:$LIB_DIR/searchserver.jar:$LIB_DIR/db.jar:$KTSEARCH_LIB_DIR/ktsearch.jar"
  if [ "$DEPLOY_TYPE" = "IWS" ]; then
    APPSERVER="n"
  else
    APPSERVER="y"
  fi
  SETUP="$ENV LD_LIBRARY_PATH=$LIB_DIR $JDK_DIR/bin/java -classpath $CLASSES com/sun/portal/search/rdmgr/SearchSetup"
  eval $SETUP $PS_BASEDIR \"$ROOT_DIR\" $APPSERVER $PS_HOST $PS_PORT \"$DEPLOY_INSTANCE\"

  if [ -d $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR ]; then
    AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"

    FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
    ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`

    FC_URL="$PS_PROTOCOL://$PS_HOST:$PS_PORT$DEPLOY_URI"
    
    FILE="/tmp/SUNWpsse_requests.xml"
    $CAT > $FILE << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
    PUBLIC "-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN"
    "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd"
>
<Requests>
  <SchemaRequests serviceName="SunPortalSearchService" SchemaType="global">
    <AddDefaultValues>
      <AttributeValuePair>
	<Attribute name="sunPortalSearchInstances"/>
	<Value>$FC_URL</Value>
      </AttributeValuePair>
    </AddDefaultValues>
  </SchemaRequests> 
</Requests>
EOF
    eval $AMADMIN -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -t $FILE
    $RM -f $FILE
  fi
}

###############################################
# Main
###############################################

if [ `$ID | $NAWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO "You must be root user. $BELL_CHAR"
  exit 1
fi

if [ ! -f $STATE_FILE ]; then
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "PS_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  PS_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PROTOCOL. $BELL_CHAR"
  PS_PROTOCOL="http"
fi
GrabConfig $STATE_FILE "PS_HOST" "="
if [ "$ANSWER" != "" ]; then
  PS_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine PS_HOST. $BELL_CHAR"
  PS_HOST=`hostname`.`domainname` 
fi
GrabConfig $STATE_FILE "PS_PORT" "="
if [ "$ANSWER" != "" ]; then
  PS_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PORT. $BELL_CHAR"
  PS_PORT="80" 
fi
GrabConfig $STATE_FILE "DEPLOY_URI" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_URI=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
  exit 1
fi

if [ "$1" != "" ]; then
  while [ "$1" != "" ]; do
    if [ "$1" = "-instance" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -instance. $BELL_CHAR"
	exit 1
      else
	DEPLOY_INSTANCE=$2      
	shift; shift
      fi
    elif [ "$1" = "-port" ]; then
      if [ "$2" = "" ] || [ '-' = `$ECHO $2 | $CUT -c1` ]; then
	$ECHO "Error: Missing value for -port. $BELL_CHAR"
	exit 1
      else
	SERVER_PORT=$2      
	shift; shift
      fi
    elif [ "$1" = "-is_admin_password" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -is_admin_password. $BELL_CHAR"
	Usage
	exit 1
      else
	IDSAME_ADMIN_PASSWORD=$2      
	shift; shift
      fi
    else
      $ECHO "Error: Unknown parameter $1. $BELL_CHAR"
      exit 1
    fi
  done
else
  DEPLOY_INSTANCE="$PS_HOST"
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  fi

  PS_PORT="80"
  GrabConfig $STATE_FILE "PS_PORT" "="
  if [ "$ANSWER" != "" ]; then
    PS_PORT=$ANSWER
  fi

  Question "Deploy into which instance?" "$DEPLOY_INSTANCE" "a-zA-Z0-9 /\._-"
  DEPLOY_INSTANCE="$ANSWER"

  Question "What is the instance port?" $PS_PORT "0-9"
  PS_PORT=$ANSWER

  GetPassword "What is the Identity Server administration password?"
  IDSAME_ADMIN_PASSWORD=$ANSWER
fi

DoSearchDeploy

exit 0
