REM *************************************************************************
REM
REM Licensed Materials - Property of IBM
REM
REM "Restricted Materials of IBM"
REM
REM IBM Informix Dynamic Server
REM Copyright IBM Corporation 2008 All rights reserved.
REM
REM Title:       blduser.bat
REM Description: Script to create database sysuser
REM
REM *************************************************************************

REM ###
REM # Set infdir and outfile name
REM ###
set INFDIR=%INFORMIXDIR%
set OUTFILE=%INFDIR%\tmp\blduser.out.%INFORMIXSERVER%
set CHKFILE=%INFDIR%\tmp\sysuserdbchk.out.%INFORMIXSERVER%
set BLDUSER_STATFILE=%INFDIR%\tmp\blduser.%INFORMIXSERVER%.sql
set SYSUSERDB=sysuser

REM ###
REM # Inherit the message constants
REM ###
call %INFDIR%\etc\conv\strmaps.bat

REM ###
REM # Setup the WINROOT path
REM ###
set WINROOT=%SystemRoot%
if "x%WINROOT%" == "x" set WINROOT=C:\WINDOWS

REM ###
REM # Remove emergency boot file since it's data is now obsolete
REM ###
copyboot

REM # Put something in output file, so user knows something happened
type %INFDIR%\etc\blduser.1in > %OUTFILE% 2>&1

set DBPATH_PREV=%DBPATH%
set DBPATH=

set DBCOUNT=0
set TABCOUNT=0

REM Create 'sysuser' in English

set DB_LOCALE=en_us.8859-1
set CLIENT_LOCALE=en_us.8859-1

REM Create print_msg.sql and call it through dbaccess from print_msg.bat
REM              to print message to online.log

echo echo set lock mode to  wait 60; ^> print_msg.sql > print_msg.bat
echo echo database sysmaster; ^>^> print_msg.sql >> print_msg.bat
echo echo insert into logmessage values (%%1%%); ^>^> print_msg.sql >> print_msg.bat
echo %INFDIR%\bin\dbaccess - print_msg.sql >> print_msg.bat

REM If the database 'sysuser' exists then we don't have much to do

echo output to "%CHKFILE%" without headings > %INFDIR%\tmp\blduser1.sql
echo select count^(*^) from sysdatabases where name = '%SYSUSERDB%'; >> %INFDIR%\tmp\blduser1.sql

%INFDIR%\bin\dbaccess sysmaster %INFDIR%\tmp\blduser1.sql

del %INFDIR%\tmp\blduser1.sql

IF EXIST %CHKFILE% (
   FOR /f "delims== " %%i IN ('type %CHKFILE%') do set DBCOUNT=%%i
   del %CHKFILE%
) ELSE set DBCOUNT=0

del %OUTFILE%
IF %DBCOUNT% == 0 GOTO bldsysuser

REM If the database 'sysuser' exists then we check 'sysauth' table

echo output to "%CHKFILE%" without headings > %INFDIR%\tmp\blduser2.sql
echo select count^(*^) from systables where tabname = 'sysauth'; >> %INFDIR%\tmp\blduser2.sql

%INFDIR%\bin\dbaccess sysuser %INFDIR%\tmp\blduser2.sql >> %OUTFILE% 2>&1

del %INFDIR%\tmp\blduser2.sql

IF EXIST %CHKFILE% (
   FOR /f "delims== " %%i IN ('type %CHKFILE%') do set TABCOUNT=%%i
   del %CHKFILE%
) ELSE set TABCOUNT=0

IF %TABCOUNT% == 1 (
   call print_msg.bat %S_SYSUSER_EXIST%
   del %OUTFILE%
   del print_msg.bat print_msg.sql
   goto exit
) ELSE (
   REM 'sysuser' exists, but it's not created by IDS
   call print_msg.bat %S_BLD_SYSUSER_FAIL%

   echo Database 'sysuser' exists, but it is not created by IDS. >> %OUTFILE% 2>&1
   echo >> %OUTFILE% 2>&1
   echo IBM Informix database server will store remote trusted user's authentication  >> %OUTFILE% 2>&1
   echo information and database security administrator metadata in database 'sysuser'. >> %OUTFILE% 2>&1
   echo >> %OUTFILE% 2>&1
   echo Please drop database 'sysuser' and rerun blduser.bat as informix after the  >> %OUTFILE% 2>&1
   echo server comes On-Line. >> %OUTFILE% 2>&1

   del print_msg.bat print_msg.sql
   goto exit
) 

:bldsysuser
REM ###
REM # Build the 'sysuser' database
REM ###
echo "About to run sysuser.sql" >> %OUTFILE% 2>&1
dbaccess - %INFDIR%\etc\sysuser.sql >> %OUTFILE% 2>&1

REM # Check to see that there were no errors
REM # Make sure find.exe runs from \winnt\system32 and not from a DOS directory
%WINROOT%\System32\find /V "%INFDIR%" %OUTFILE% | %WINROOT%\System32\find /I "error"

REM # 0 means found and error so sysuser.sql failed
if ERRORLEVEL 1 goto SUCCESS
if ERRORLEVEL 0 goto FAILURE

:SUCCESS
REM ###
REM # Else it was successful, so remove OUTFILE  and ...
REM # put s_sysuser_succ msg into sysmaster 'logmessage' table.
REM #   See olmsg.src for msg.
REM ###
echo "Inserting success msg into logmessage table" >> %OUTFILE% 2>&1
echo insert into logmessage(message_num) values (%S_BLD_SYSUSER_SUCC%); > %BLDUSER_STATFILE%
dbaccess sysmaster %BLDUSER_STATFILE% >> %OUTFILE% 2>&1
del %OUTFILE% print_msg.bat
goto exit

:FAILURE
REM ###
REM # Was there an error?
REM # If yes, then drop 'sysuser' and ...
REM # put s_bld_sysuser_err msg into the sysmaster 'logmessage' table.
REM #   See olmsg.src for msg.
REM ###
echo Cleaning up whatever was successfully created >> %OUTFILE%
dbaccess - - < %INFDIR%\etc\blduser.2in >> %OUTFILE% 2>&1

echo insert into logmessage(message_num) values (%S_BLD_SYSUSER_FAIL%); > %BLDUSER_STATFILE%
dbaccess sysmaster %BLDUSER_STATFILE% >> %OUTFILE% 2>&1
del print_msg.bat
goto exit

:exit
del %BLDUSER_STATFILE%