Rem  Copyright (c) Oracle Corporation 2014. All Rights Reserved.
Rem
Rem    NAME
Rem      ords_prereq_env.sql
Rem
Rem    DESCRIPTION
Rem      This script checks prerequisites for Oracle REST Data Services (ORDS).
Rem        1. Verify that the connected user has SYSDBA privileges
Rem        2. Verify that the ORDS schema and ORDS Public user does not exist
Rem        3. Verify that the Oracle Database is 11.1 or later
Rem        4. Verify that PL/SQL Web Toolkit is installed
Rem
Rem      Do not invoke this script directly.  If you are doing a manual installation, use ords_manual_install.sql
Rem
Rem    NOTES
Rem      Assumes user with SYSDBA privilege is connected.
Rem
Rem    REQUIREMENTS
Rem      - Oracle Database Release 11.1 or later
Rem
Rem    Arguments:  None
Rem
Rem    MODIFIED    (MM/DD/YYYY)
Rem     epaglina    06/08/2015 - Moved verifying existence of tablespaces to ords_verify_tablespaces.sql.
Rem     epaglina    05/18/2015 - Validates the ORDS Public user default and tempoarary tablespaces. Removed INSTALLTYPE.
Rem     epaglina    01/28/2014 - Script adapted from APEX (creator: jkallman).  Modified for Oracle Rest Data Services.
Rem

set define '^'
set concat on
set concat .
set verify off
set termout off
set termout on

define ORDSSCHEMA   = 'ORDS_METADATA'
define ORDSPUBUSR   = 'ORDS_PUBLIC_USER'

whenever sqlerror exit sql.sqlcode
set serveroutput on

prompt
prompt INFO: Checking prerequisites for Oracle REST Data Services

--  Check SYSDBA Privilege
begin
    for c1 in (
        select null
          from sys.dual
         where not exists (select null
                             from sys.session_privs
                            where privilege = 'SYSDBA')
    ) loop
        raise_application_error(-20000, 'ERROR: Oracle REST Data Services installation requires a connection with the SYSDBA privilege.');
    end loop;
end;
/

--  Check if the Oracle REST Data Services schema or ORDS Public user already exists
begin
   for c1 in (
          select username from sys.dba_users where username in ('^ORDSSCHEMA', '^ORDSPUBUSR') order by 1
   ) loop
      raise_application_error(-20002, 'ERROR: The user ' || c1.username || ' already exists.  You must first uninstall ORDS using ords_uninstall.sql prior to running the install scripts.');
   end loop;
end;
/

--  Check the Oracle Database Version.  Must be release 11.1 or later
begin
	if (sys.dbms_db_version.VERSION < 11) then
       raise_application_error(-20001, 'ERROR: Oracle REST Data Services installation requires Oracle Database Release 11.1 or later.');
    end if;
end;
/

--  Check for PL/SQL Web Toolkit
declare
    l_xx varchar2(1000);
begin
    execute immediate
        'begin :a := sys.owa_util.get_version(); end;'
        using out l_xx;
exception
    when others then
        raise_application_error(-20003, 'ERROR: Oracle REST Data Services installation requires the PL/SQL Web Toolkit.');        
end;
/

prompt INFO: Prerequisites verified.
prompt
