#!/bin/ksh

 #*************************************************************************
 # Licensed Materials - Property of IBM                                   *
 # 5724-E49                                                               *
 # 5765-F35                                                               *
 # 5765-F35                                                               *
 # 5724-N76                                                               *
 # 5724-B55
 # (c) Copyright IBM Corporation 2000,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:  nsesample
 #
 # Description:       This script creates a sample database,
 #                    creates a sample tables,
 #                    imports rows of sample data into it,
 #                    enables the database, and enables the text
 #                    columns.
 # *******************************************************************

 # *******************************************************************
 # S Y N T A X :
 #        nsesample <databasename> 
 #
 #        If database <databasename> does not exist it will be created.
 #        Creates sample tables and imports sample data into it.
 #
 #
 # ********************************************************************
 #
 # 0. The following steps have been done in advance:
 #    - DB2 Net Search Extender has been installed.
 #    - DB2 default instance has been created.
 #    - copy directory 'nsesamplehtml' into <http_server_root_dir>
 #
 # ********************************************************************
 #
 # Environment: Unix
 #
 # ********************************************************************

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

help1()
{
echo "------------------------------------------------------------------------"
echo "Usage : nsesample database_name "
echo "------------------------------------------------------------------------"
exit
}

 # Remove nsesample log file if exists ********************************
rm $HOME/nsesample.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

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


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

 # ********************************************************************
 # Create database
 # ********************************************************************
echo "**********************************************************">> $HOME/nsesample.log
echo "Create database $1                                        ">> $HOME/nsesample.log
db2 create database $1                                         >> $HOME/nsesample.log


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


 # ********************************************************************
 # Create sample table
 # ********************************************************************
echo "********************************************************* ">> $HOME/nsesample.log
echo "Create sample table db2ext.texttab in database $1:        ">> $HOME/nsesample.log
echo "db2 create table db2ext.texttab (docid INTEGER NOT NULL, author VARCHAR(50), title VARCHAR(50), comment LONG VARCHAR, price DECIMAL(8,2), PRIMARY KEY (docid)                                                          ">> $HOME/nsesample.log
db2 "create table db2ext.texttab (docid INTEGER NOT NULL, author VARCHAR(50), title VARCHAR(50), comment LONG VARCHAR, price DECIMAL(8,2), PRIMARY KEY (docid))" >> $HOME/nsesample.log

echo "Create sample table db2ext.htmltab in database $1:">> $HOME/nsesample.log
echo "db2 create table db2ext.htmltab (DOCID INTEGER NOT NULL PRIMARY KEY, CATEGORY VARCHAR(20), DATE DATE, HTMLFILE CLOB(1M), FILENAME VARCHAR(20)) ">> $HOME/nsesample.log
db2 "create table db2ext.htmltab (DOCID INTEGER NOT NULL PRIMARY KEY, CATEGORY VARCHAR(20), DATE DATE, HTMLFILE CLOB(1M), FILENAME VARCHAR(20))" >> $HOME/nsesample.log
 # ********************************************************************


 # ********************************************************************
 # Import data into sample table
 # ********************************************************************
echo "******************************************************** ">> $HOME/nsesample.log
echo "Import data into the sample tables:                      ">> $HOME/nsesample.log
echo "db2 import from texttab.del of del insert into DB2EXT.TEXTTAB (docid, author, title, comment, price)                                    ">> $HOME/nsesample.log
db2 "import from texttab.del of del insert into DB2EXT.TEXTTAB (docid, author, title, comment, price)"                                         >> $HOME/nsesample.log

echo "db2 load   from htmltab.del of del lobs from nsesamplehtml/ modified by lobsinfile insert into db2ext.htmltab ">> $HOME/nsesample.log
db2 "load   from htmltab.del of del lobs from nsesamplehtml/ modified by lobsinfile insert into db2ext.htmltab"                               >> $HOME/nsesample.log
 # *********************************************************************


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


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


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


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

echo "Create the text indexes ... " 
echo "**********************************************************">> $HOME/nsesample.log
echo " Create and update indexes:                   ">> $HOME/nsesample.log

echo db2text "create index DB2EXT.COMMENT for text on DB2EXT.TEXTTAB (COMMENT) ATTRIBUTES (CAST(PRICE AS DOUBLE) AS PRICE) CACHE TABLE(docid, title) TEMPORARY MAXIMUM CACHE SIZE 1 ">> $HOME/nsesample.log
db2text "create index DB2EXT.COMMENT for text on DB2EXT.TEXTTAB (COMMENT) ATTRIBUTES (CAST(PRICE AS DOUBLE) AS PRICE) CACHE TABLE(docid,title) TEMPORARY MAXIMUM CACHE SIZE 1 " >> $HOME/nsesample.log
echo db2text update index DB2EXT.COMMENT for text              >> $HOME/nsesample.log
db2text update index DB2EXT.COMMENT for text                   >> $HOME/nsesample.log
echo db2text activate cache for index DB2EXT.COMMENT for text            >> $HOME/nsesample.log
db2text activate cache for index DB2EXT.COMMENT for text                   >> $HOME/nsesample.log

echo "db2text create index DB2EXT.HTMLIDX for text on DB2EXT.HTMLTAB(HTMLFILE) format HTML"                                                     >> $HOME/nsesample.log
db2text "create index DB2EXT.HTMLIDX for text on DB2EXT.HTMLTAB(HTMLFILE) format HTML"                                                          >> $HOME/nsesample.log
echo db2text update index DB2EXT.HTMLIDX for text              >> $HOME/nsesample.log
db2text update index DB2EXT.HTMLIDX for text                   >> $HOME/nsesample.log
 # *********************************************************************


 # *********************************************************************
 # Compile the thesaurus definition file using the db2extth utility
 # *********************************************************************
echo "**********************************************************">> $HOME/nsesample.log
echo "Compile sample Thesaurus 'nsesamplethes'                  ">> $HOME/nsesample.log

echo "cp nsesamplethes.def $HOME/sqllib/db2ext/thes                 ">> $HOME/nsesample.log
cp nsesamplethes.def $HOME/sqllib/db2ext/thes/                      >> $HOME/nsesample.log

echo "db2extth -f $HOME/sqllib/db2ext/thes/nsesamplethes.def -ccsid 1252 ">> $HOME/nsesample.log
db2extth -f $HOME/sqllib/db2ext/thes/nsesamplethes.def -ccsid 1252    >> $HOME/nsesample.log
 # *********************************************************************


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

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

