#!/bin/sh
# $Id$
# *===========================================================================*
# |                 Copyright (c) 2008 Applaud Solutions UK Ltd               |
# |                             All rights reserved                           |
# *===========================================================================*

header_string="$Id$"
prog_version=`echo "$header_string" | awk '{print $3}'`
program=`basename $0 .sh`

usage="\t$program"

$XXAS_LOG "Looking for the custom schema:"
#
# Set the location of the schema scripts
#
get_schema_file=$XXAS_PATCH_SQL/xxas_com_user.sql

#
# Check that the get schema script exists
#
if test ! -f "$get_schema_file" ; then
  printf "\nERROR: The following SQL file cannot be found:\n"
  printf "$get_schema_file\n\n"
  exit 1;
fi

#
# Fetch the selected schema
#
$XXAS_LOG "Retrieving custom schema..."
selected_schema=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$get_schema_file`
exit_code=$?
if [ $exit_code -ne 0 ];
then
  printf "\nERROR: Unable to establish the custom schema.\n"
  exit 1;
fi
$XXAS_LOG "$selected_schema"

XXAS_ONETI="N"

#
# Check that a value was returned
#
if [ ${selected_schema}x = "x" ];
then
  $XXAS_LOG "Custom schema not registered. This is the first install:"
  XXAS_ONETI="Y"
  #
  # Initiate first-time install
  #
  printf "\nInstallation has detected that this is the first time this install\n"
  printf "has run on this database.  This requires additional one-time setup.\n\n"
  printf "The default setting will create a new XXAS Oracle Schema.\n"
  printf "It is strongly recommended that you accept this default so that this\n"
  printf "product is separated from any other custom code.\n\n"

  #
  # Offer the default XXAS schema. If the default is not chosen, ask for
  # the existing custom schema and validate the name.
  #
  while true
  do
    printf "\nAccept default new XXAS Schema (Y or N): "
    read accept_default
    case $accept_default in
      y|Y|YES|yes|Yes)
        selected_schema="XXAS"
        $XXAS_LOG "Default XXAS schema selected."
        $XXAS_LOG "Request new password for XXAS schema..."
        #
        # Set and confirm the new password
        #
        while test "$new_pw1" != "$new_pw2" -o "${new_pw1:=}x" = "x"
        do
          while test "${new_pw1:=}x" = "x"
          do
            printf "\nEnter a password for the new XXAS schema: "
            stty -echo
            read new_pw1
            stty echo
          done

          while test "${new_pw2:=}x" = "x"
          do
            printf "\nConfirm the password: "
            stty -echo
            read new_pw2
            stty echo
            printf "\n\n"
          done
          if test "$new_pw1" != "$new_pw2" ;
          then
            printf "\nERROR: The passwords do not match.\n\n"
            unset new_pw1
            unset new_pw2
          fi
        done
        XXAS_SCHEMA_PW=$new_pw1
        $XXAS_LOG "accepted"

        #
        # Retrieve the SYSTEM password so that the schema can be created
        #
        $XXAS_LOG "Requesting SYSTEM password..."
        printf "\nThe installation requires SYSTEM privileges to create the\n"
        printf "XXAS schema.\n\n"
        while test "${system_pw:=}x" = "x"
        do
          printf "\nEnter the SYSTEM password: "
          stty -echo
          read system_pw
          stty echo
          printf "\n\n"
          #
          # Test the system login
          #
          $XXAS_PATCH_BIN/xxas_com_testsqlp.sh SYSTEM $system_pw
          exit_code=$?
          if [ $exit_code -ne 0 ];
          then
            unset system_pw
          fi
        done
        $XXAS_LOG "accepted"
        break
      ;;
      n|N|NO|no|No)
        while test "${selected_schema:=}x" = "x"
        do
          printf "\nEnter an existing custom schema: "
          $XXAS_LOG "User opted to use existing custom schema."
          $XXAS_LOG "Requesting name of existing schema..."
          read selected_schema
          selected_schema=`echo "$selected_schema" | tr '[:lower:]' '[:upper:]'`
          $XXAS_LOG "$selected_schema"
          if [ ${selected_schema}x != "x" ];
          then
            $XXAS_LOG "$selected_schema"
            #
            # Fetch the password for this schema
            #
            $XXAS_LOG "Request custom schema password..."
            while test "${schema_pw:=}x" = "x"
            do
			  printf "\nEnter the $selected_schema password: "
              stty -echo
              read schema_pw
              stty echo
              printf "\n\n"
            done
            $XXAS_LOG "accepted"
            #
            # Test the schema login
            #
            $XXAS_LOG "Testing custom schema password..."
            $XXAS_PATCH_BIN/xxas_com_testsqlp.sh $selected_schema $schema_pw
            exit_code=$?
            if [ $exit_code -ne 0 ];
            then
              unset selected_schema
              unset schema_pw
            fi
            $XXAS_LOG "accepted"
          fi
        done
        XXAS_SCHEMA_PW=$schema_pw
        break

      ;;
#      *) break
    esac
  done
fi

#
# Set environment variables
#
export XXAS_ONETI
XXAS_SCHEMA_UN=$selected_schema; export XXAS_SCHEMA_UN
XXAS_SYSTEM_PW=$system_pw; export XXAS_SYSTEM_PW
export XXAS_SCHEMA_PW

$XXAS_LOG "Chosen Schema: $XXAS_SCHEMA_UN"

