#!/bin/ksh

 #*************************************************************************
 # Licensed Materials - Property of IBM                                   *
 # 5724-E49                                                               *
 # 5765-F35                                                               *
 # 5765-F41                                                               *
 # 5724-N76                                                               *
 # 5724-B55
 # (c) Copyright IBM Corporation 2006.                                    *
 # All rights reserved.                                                   *
 #                                                                        *
 # US Government Users Restricted Rights - Use, duplication or            *
 # disclosure restricted by GSA ADP Schedule Contract with                *
 # IBM Corporation.                                                       *
 #*************************************************************************
 #  Permission Notice                                                     *
 #                                                                        *
 #  Permission is granted to copy, use, modify, and merge this sample     *
 #  software into your applications and to permit others to do any of the *
 #  foregoing. You may further distribute this software for               *
 #  commercial purposes only as part of your application that adds        *
 #  significant value and function beyond that provided by these          *
 #  samples.                                                              *
 #  You must include this permission statement and retain the copyright   *
 #  notice in all copies and modified versions of this software.          *
 #                                                                        *
 #*************************************************************************
 #                                                                        *
 #  DISCLAIMER OF WARRANTIES                                              *
 #                                                                        *
 #  The sample software is provided to you by IBM to assist you in        *
 #  developing your applications. THIS SOFTWARE IS PROVIDED AS-IS,        *
 #  WITHOUT WARRANTY OF ANY KIND. IBM SHALL NOT BE LIABLE FOR ANY         *
 #  DAMAGES ARISING OUT OF YOUR USE OR THE USE BY ANY THIRD PARTY         *
 #  OF THE SAMPLE SOFTWARE EVEN IF IT HAS BEEN ADVISED OF THE POSSIBILITY *
 #  OF SUCH DAMAGES. IN ADDITION, IBM SHALL NOT BE LIABLE FOR ANY THIRD   *
 #  PARTY CLAIMS AGAINST YOU.                                             *
 #                                                                        *
 #*************************************************************************
 # Product Name:      DB2 Net Search Extender
 #
 # Source File Name:  xmlsample
 #
 # Description:       This script creates a sample UTF8 database,
 #                    creates a sample table,
 #                    imports rows of sample data into it,
 #                    enables the database, and enables the text
 #                    columns.
 #                    To perform searches call the script xmlsearch.
 #                    To clean up call the script xmlsample_cleanup.
 #  
 # *******************************************************************

 # *******************************************************************
 # S Y N T A X :
 #        xmlsample <databasename> 
 #
 #        If database <databasename> does not exist it will be created.
 #        If database <databasename> exists, the codeset must be UTF-8
 #        and the territory mus tbe US. Otherwise the create table 
 #        statement will fail.
 #
 # ********************************************************************
 #
 # 0. The following steps have been done in advance:
 #    - DB2 Net Search Extender has been installed.
 #    - DB2 default instance has been created.
 #
 # ********************************************************************
 #
 # Environment: Unix
 #
 # ********************************************************************

 # ********************************************************************
 # Get syntax help
 # ********************************************************************

help1()
{
echo "------------------------------------------------------------------------"
echo "Usage : xmlsample database_name "
echo ""
echo "If database <database_name> does not exist it will be created."
echo "If database <databasename> exists, the codeset must be UTF-8"
echo "and the territory must be US."
echo "------------------------------------------------------------------------"
exit
}

 # Remove xmlsample log file if exists ********************************
rm $HOME/xmlsample.log 2> /dev/null

 # ********************************************************************
 # Get database name and http server root directory
 # ********************************************************************


if [[ -z $1 ]] 
  then help1
fi

if [ $1 = -h ] 
  then help1 
fi

if [ $1 = --help ] 
  then help1 
fi

 # ********************************************************************
 # Set install directory for model file
 # ******************************************************************** 

SQLLIBDIR=`eval echo ~$DB2INSTANCE/sqllib`

echo "**********************************************************">> $HOME/xmlsample.log
echo "**********************************************************">> $HOME/xmlsample.log
echo "Start xmlsample ...                                       ">> $HOME/xmlsample.log


echo "Setup the database $1 ..."
 # ********************************************************************
 # Start DB2
 # ********************************************************************
echo "********************************************************* ">> $HOME/xmlsample.log
echo "Start the database manager:                               ">> $HOME/xmlsample.log
echo "db2start                                                  ">> $HOME/xmlsample.log
db2start                                                         >> $HOME/xmlsample.log

 # ********************************************************************
 # Create database
 # ********************************************************************
echo "**********************************************************">> $HOME/xmlsample.log
echo "Create database $1 using codeset UTF-8 territory US       ">> $HOME/xmlsample.log
db2 create database $1 using codeset UTF-8 territory US          >> $HOME/xmlsample.log


 # ********************************************************************
 # Connect to database
 # ********************************************************************
echo "**********************************************************">> $HOME/xmlsample.log
echo "Connect to database $1                                    ">> $HOME/xmlsample.log
db2 connect to $1                                              >> $HOME/xmlsample.log


 # ********************************************************************
 # Create sample table
 # ********************************************************************
echo "********************************************************* ">> $HOME/xmlsample.log
echo "Create sample table db2ext.testSearch in database $1:     ">> $HOME/xmlsample.log
echo "db2 create table db2ext.testSearch (docID int not null primary key,text xml)     ">> $HOME/xmlsample.log
db2 "create table db2ext.testSearch (docID int not null primary key,text xml)"  >> $HOME/xmlsample.log

 # ********************************************************************


 # ********************************************************************
 # Import data into sample table
 # ********************************************************************
echo "******************************************************** ">> $HOME/xmlsample.log
echo "Import data into the sample table:                      ">> $HOME/xmlsample.log
echo "db2 import from xmlimport.del of del lobs from xmlfiles/ modified by lobsinfile insert into db2ext.testSearch                           ">> $HOME/xmlsample.log
db2 "import from xmlimport.del of del lobs from xmlfiles/ modified by lobsinfile insert into db2ext.testSearch                                ">> $HOME/xmlsample.log


 # *********************************************************************


 # *********************************************************************
 # Start the Update and Locking services
 # *********************************************************************
echo "********************************************************* ">> $HOME/xmlsample.log
echo "Start the Update and Locking services:                    ">> $HOME/xmlsample.log
echo "db2text start                                             ">> $HOME/xmlsample.log
db2text start                                                    >> $HOME/xmlsample.log
 # *********************************************************************


 # *********************************************************************
 # Set the environment variable DB2DBDFT
 # *********************************************************************
echo "**********************************************************">> $HOME/xmlsample.log
echo "export DB2DBDFT=$1                                        ">> $HOME/xmlsample.log
export DB2DBDFT=$1
 # *********************************************************************


 # *********************************************************************
 # Enable the database
 # *********************************************************************
echo "**********************************************************">> $HOME/xmlsample.log
echo "Enable the database $1:                                   ">> $HOME/xmlsample.log
echo "db2text enable database for text                          ">> $HOME/xmlsample.log
db2text enable database for text                                 >> $HOME/xmlsample.log
 # *********************************************************************


 # *********************************************************************
 # Create index
 # *********************************************************************

echo "Create the text index ... " 
echo "**********************************************************">> $HOME/xmlsample.log
echo " Create and update index:                                 ">> $HOME/xmlsample.log
echo "db2text create index DB2EXT.XML for text on db2ext.testSearch (text) FORMAT XML DOCUMENTMODEL XMLModel in \"$SQLLIBDIR/samples/extenders/db2ext/xmlfiles/birdmodel.xml\"                                                ">> $HOME/xmlsample.log
db2text "create index DB2EXT.XML for text on db2ext.testSearch (text) FORMAT XML DOCUMENTMODEL XMLModel in \"$SQLLIBDIR/samples/extenders/db2ext/xmlfiles/birdmodel.xml\"                                                     ">> $HOME/xmlsample.log

echo "db2text update index DB2EXT.XML for text                  ">> $HOME/xmlsample.log
db2text update index DB2EXT.XML for text                         >> $HOME/xmlsample.log


 # *********************************************************************


 # *********************************************************************
 # Inform user
 # *********************************************************************
echo "**********************************************************">> $HOME/xmlsample.log
echo "Finished xmlsample.script ***                             ">> $HOME/xmlsample.log
echo "**********************************************************">> $HOME/xmlsample.log
echo "**********************************************************">> $HOME/xmlsample.log
echo "Check $HOME/xmlsample.log for execution information."

 #**********************************************************************
 #*** End of DB2 Net Search Extender sample 'xmlsample'
 #**********************************************************************

