/*
 * $Revision$
 * $Id$
 *
 * Script to initialise a FACILITOR database schema in Oracle
 */
#define FCLT_CREATE

#include "modules.h"
#include "comsql.h"

SET TIME ON
SET HEADING OFF
SET FEEDBACK OFF
SET ECHO OFF
SET SERVEROUTPUT ON SIZE UNLIMITED;

column fcltlogfile new_value fcltlogfile NOPRINT;
select 'FACILITOR'|| _DBV_STRING || '_' || UPPER(USER || '_' || sys_context('USERENV', 'SERVICE_NAME') || '_' || sys_context('USERENV', 'SERVER_HOST')) || '_' || to_char(sysdate,'YYYYMMDD_HH24MISS') || '.lst' as fcltlogfile from dual;
spool &fcltlogfile

WHENEVER SQLERROR EXIT;
DECLARE
    tekst VARCHAR2 (1000);
    n_object   NUMBER (10);
BEGIN
    CASE ASCII('€')
        WHEN 191 THEN tekst :='WE8ISO8859P1';
        WHEN 128 THEN tekst :='WE8MSWIN1252'; -- De enige goede
        ELSE tekst :='UNKNOWN ';
    END CASE;
    IF tekst <> 'WE8MSWIN1252' THEN
        tekst := '=================================================================' || CHR(10)
              || 'Foute clientside NLS_LANG ['|| TO_CHAR(ASCII('€')) ||'] voor dit Facilitor script.' || CHR(10)
              || 'WE8MSWIN1252 is nodig, ik vermoed '|| tekst || CHR(10)
              || 'Geef bijvoorbeeld in Windows voordat SQL*Plus opstart:' || CHR(10)
              || 'SET NLS_LANG=.WE8MSWIN1252' || CHR(10)
              || 'Het script wordt nu afgebroken!' || CHR(10)
              || '=================================================================' || CHR(10);
        RAISE_APPLICATION_ERROR(-20000, 'OR'||'A-' || CHR(10) || tekst);
    END IF;

    SELECT COUNT (*) INTO n_object FROM USER_OBJECTS;
    IF (n_object > 0)
    THEN
        RAISE_APPLICATION_ERROR (-20000, 'OR' || 'A-' || 'Schema ' || USER || ' must be empty before creating database');
    END IF;

    IF USER = 'SYS' OR USER = 'SYSTEM' THEN
        RAISE_APPLICATION_ERROR(-20000, 'Cannot create Facilitor database on user ' || USER);
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE;

PROMPT [&fcltlogfile]
PROMPT
PROMPT Copyright 1996-2023 Aareon Nederland B.V. All rights reserved.
PROMPT
PROMPT This is the generated list-file of EVALCONCAT(FCLT,_DBV).sql which was used
PROMPT to create the Facilitor(r) EVALCONCAT(DB,_DBV) schema in Oracle
PROMPT
SELECT 'Current os_user: ' || SYS_CONTEXT( 'userenv', 'os_user' ) FROM DUAL;
PROMPT

SET LINESIZE 1000
SET DEFINE OFF
SET ECHO ON
#include "adm\adm_tab.src"
#include "adm\adm_seq.src"
#include "adm\adm_tri.src"
#include "adm\adm_pac.src"

/* TYPES */
#include "mld\mld_typ.src"

/* TABLES */
#include "fac\fac_tab.src"
#include "alg\alg_tab.src"
#include "ins\ins_tab.src"
#include "prs\prs_tab.src"
#include "mld\mld_tab.src"
#include "mrk\mrk_tab.src"
#include "cad\cad_tab.src"
#include "res\res_tab.src"
#include "bez\bez_tab.src"
#include "web\web_tab.src"
#include "cnt\cnt_tab.src"
#include "bes\bes_tab.src"
#include "fin\fin_tab.src"
#include "bgt\bgt_tab.src"
#include "prj\prj_tab.src"
#include "msg\msg_tab.src"
#include "kpi\kpi_tab.src"
#include "ctr\ctr_tab.src"
#include "aut\aut_tab.src"
#include "cnd\cnd_tab.src"
#include "exc\exc_tab.src"

/* SEQUENCES */
#include "fac\fac_seq.src"
#include "alg\alg_seq.src"
#include "ins\ins_seq.src"
#include "prs\prs_seq.src"
#include "mld\mld_seq.src"
#include "mrk\mrk_seq.src"
#include "cad\cad_seq.src"
#include "res\res_seq.src"
#include "bez\bez_seq.src"
#include "web\web_seq.src"
#include "cnt\cnt_seq.src"
#include "bes\bes_seq.src"
#include "fin\fin_seq.src"
#include "bgt\bgt_seq.src"
#include "prj\prj_seq.src"
#include "msg\msg_seq.src"
#include "kpi\kpi_seq.src"
#include "ctr\ctr_seq.src"
#include "aut\aut_seq.src"
#include "exc\exc_seq.src"

/* INDICES */
#include "fac\fac_ind.src"
#include "alg\alg_ind.src"
#include "ins\ins_ind.src"
#include "prs\prs_ind.src"
#include "mld\mld_ind.src"
#include "mrk\mrk_ind.src"
#include "cad\cad_ind.src"
#include "res\res_ind.src"
#include "bez\bez_ind.src"
#include "web\web_ind.src"
#include "cnt\cnt_ind.src"
#include "bes\bes_ind.src"
#include "fin\fin_ind.src"
#include "bgt\bgt_ind.src"
#include "prj\prj_ind.src"
#include "msg\msg_ind.src"
#include "kpi\kpi_ind.src"
#include "ctr\ctr_ind.src"
#include "aut\aut_ind.src"
#include "exc\exc_ind.src"

/* VIEWS, PACKAGES, TRIGGERS and LOCALES */
#include "_up/recreate.inc"

/* INITIALISATIES */
#include "fac\fac_ini.src"
#include "alg\alg_ini.src"
#include "ins\ins_ini.src"
#include "prs\prs_ini.src"
#include "mld\mld_ini.src"
#include "mrk\mrk_ini.src"
#include "cad\cad_ini.src"
#include "res\res_ini.src"
#include "bez\bez_ini.src"
#include "web\web_ini.src"
#include "cnt\cnt_ini.src"
#include "bes\bes_ini.src"
#include "fin\fin_ini.src"
#include "bgt\bgt_ini.src"
#include "prj\prj_ini.src"
#include "msg\msg_ini.src"
#include "kpi\kpi_ini.src"
#include "ctr\ctr_ini.src"
#include "aut\aut_ini.src"
#include "exc\exc_ini.src"

COMMIT;

///////////////////////////// finally compile all views, triggers etc acc to the latest state ///////
BEGIN DBMS_UTILITY.COMPILE_SCHEMA(USER, FALSE); END;
/
BEGIN fac.registerversion(_DBV_STRING, 'NL'); END;
/
REGISTERONCE('$Id$')

INSERT INTO adm_tracking
    (adm_tracking_name, adm_tracking_revision)
VALUES
    ('Database created', 'DB' || _DBV_STRING);

COMMIT;
/* Report invalid objects, if any */
SET ECHO OFF
SET LINESIZE 132
PROMPT List of all invalid objects after FACILITOR initialisation:
SELECT 'OR'||'A-'||'DB'|| _DBV_STRING ||' Warning: ' || object_type || ' ' || object_name || ' is ' || status
  FROM user_objects
WHERE object_type IN ('VIEW', 'TRIGGER', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'PACKAGE BODY')
AND status <> 'VALID'
ORDER BY object_name;

PROMPT ======================================================================
SELECT 'Schema FACILITOR@' || USER || ' is now ' || fac.getdbversion() FROM DUAL;
PROMPT
PROMPT Your schema initialisation is ready. Please check the logfile for errors.
PROMPT If in doubt please contact: FACILITOR Helpdesk +31 53 4800 710
PROMPT Copyright (c) 1996-2023 Aareon Nederland B.V. All rights reserved.
SPOOL OFF
