Rem  Copyright (c) Oracle Corporation 2014. All Rights Reserved.
Rem
Rem    NAME
Rem      ords_manual_install.sql
Rem
Rem    DESCRIPTION
Rem      Manual installation for the Oracle REST Data Services (ORDS).
Rem
Rem    NOTES
Rem      Assumes user with SYSDBA privilege is connected.
Rem      Assumes the default tablespace and temporary tablespace that is 
Rem      provided for ORDS schema (ORDS_METADATA) and ORDS Public user (ORDS_PUBLIC_USER) already exists.
Rem      If installing into a CDB, then must be run locally to the database and the
Rem      ORACLE_HOME environment variable must be set.
Rem
Rem    REQUIREMENTS
Rem      - Oracle Database 11.1 or later
Rem
Rem    Arguments:
Rem      1  : Log folder (must include the forward slash at the end)
Rem      2  : Default tablespace for ORDS_METADATA
Rem      3  : Temporary tablespace for ORDS_METADATA
Rem      4  : Default tablespace for ORDS_PUBLIC_USER
Rem      5  : Temporary tablespace for ORDS_PUBLIC_USER
Rem
Rem    Example:
Rem      Specifying the default and temporary tablespaces
Rem      sqlplus "sys as sysdba" @ords_manual_install /logs/ SYSAUX TEMP SYSAUX TEMP
Rem
Rem      Use -default to use the database default tablespaces for default tablespace and temporary tablespaces 
Rem      sqlplus "sys as sysdba" @ords_manual_install /logs/ -default -default -default -default
Rem
Rem
Rem    MODIFIED    (MM/DD/YYYY)
Rem     epaglina    05/18/2015 Support installing ORDS into CDB.  
Rem                            Moved executing ORDS install scripts to ords_install.sql
Rem     epaglina    12/10/2015 Removing prompting default and temporary tablespaces for ORDS Public User.
Rem                            Must be passed as a parameter. Bug 22336058.
Rem
Rem
set verify off
set termout off
spool off

set define '^'
set termout on

timing start "ORDS Installation"

define LOGFOLDER    = '^1'
define ORDS_DATATBS = '^2'
define ORDS_TEMPTBS = '^3'
define PUB_DATATBS  = '^4'
define PUB_TEMPTBS  = '^5'

whenever sqlerror exit

prompt ******************************************************
prompt * Oracle REST Data Services (ORDS) Installation.
prompt ******************************************************

-- Prompt user for the password for the ORDS Public user
accept PUB_PWD CHAR prompt 'Enter a password for the ORDS_PUBLIC_USER: ' HIDE

--  Verify the prerequisites. Installation will abort if prerequisites are not met.
@@ords_prereq_env.sql


COLUMN :script NEW_VALUE install_script NOPRINT
VARIABLE script VARCHAR2(255)

-- Check the Oracle database release and assign the appropriate script
declare
  l_script  VARCHAR2(30)   := 'ords_install.sql';
  l_is_cdb  VARCHAR2(30)   := 'NO';
  l_pdbs    VARCHAR2(2000) := null;
  
begin
 $IF DBMS_DB_VERSION.VER_LE_10 $THEN
      raise_application_error(-20010, 'ERROR: You can only install ORDS in Oracle Database release 11.1 or later.');
 $ELSIF DBMS_DB_VERSION.VER_LE_11 $THEN
      null;
 $ELSE
   if (sys.dbms_db_version.VERSION >= 12) then
      -- Check if this is a container database
      --    YES indicates a container database
      --    NO indicates a non-CDB
      select cdb into l_is_cdb from v$database;
      
      if (l_is_cdb = 'YES') and (sys_context('USERENV','CON_NAME') = 'CDB$ROOT') then
            
           -- Container is CDB$ROOT. Check if ORDS is already installed in any of the PDBS.
           for c in
              (select a.con_id con_id, b.name name
                 from sys.cdb_objects a, sys.v$pdbs b
                 where a.con_id = b.con_id
                   and a.con_id > 1
                   and a.sharing = 'NONE'
                   and a.owner = 'ORDS_METADATA'
                   and a.object_type = 'PACKAGE'
                   and a.object_name = 'ORDS'
                   order by 1
            ) loop
               if l_pdbs is null then
                  l_pdbs := c.name;
               else
                  l_pdbs := l_pdbs || ' ' || c.name;
               end if;
            end loop;
            
            if l_pdbs is null then
               l_script := 'ords_install_cdb.sql';
            else
               raise_application_error(-20020, 'ERROR: Cannot install ORDS in CDB because ORDS exists in ' || l_pdbs || 
                                               '. ORDS must be uninstalled in PDB prior to installing in CDB.');
            end if;            
      end if;
   end if;
   
 $END
 
 :script := l_script;
   
end;
/

select :script from dual;

@@^install_script ^ORDS_DATATBS ^ORDS_TEMPTBS ^PUB_DATATBS ^PUB_TEMPTBS ^LOGFOLDER ^PUB_PWD

prompt
prompt *********************************************************
prompt INFO: Completed Oracle REST Data Services Installation.
timing stop
prompt *********************************************************

exit
