#!/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_cleanup
 #
 # Description:       This script drops all sample tables, indexes
 #                    disables the database, and disables the text
 #                    columns.
 # *******************************************************************

 # *******************************************************************
 # S Y N T A X :
 #        xmlsample_cleanup <databasename> -all
 #
 #        If -all is specified database <databasename> will be dropped 
 #        as well.
 #
 # ********************************************************************
 #
 # Environment: Unix
 #
 # ********************************************************************

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

help1()
{
echo "------------------------------------------------------------------------"
echo "Usage : xmlsample_cleanup database_name [ -all ]" 
echo "------------------------------------------------------------------------"
exit
}

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


DROP_DB=0

if [[ -n $2 ]]
 then
   if [ $2 = -all ]
    then 
     DROP_DB=1
    else
     help1
   fi
fi

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


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

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


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

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

 # *********************************************************************
 # Drop index
 # *********************************************************************

echo "Drop the text index ... " 
echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo " Drop index:                                              ">> $HOME/xmlsample_cleanup.log
echo db2text "drop index DB2EXT.XML for text                    ">> $HOME/xmlsample_cleanup.log
db2text "drop index DB2EXT.XML for text                         ">> $HOME/xmlsample_cleanup.log

 # *********************************************************************
 # Disable the database
 # *********************************************************************
echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo "Disable the database $1:                                  ">> $HOME/xmlsample_cleanup.log
echo "db2text disable database for text                         ">> $HOME/xmlsample_cleanup.log
db2text disable database for text                                >> $HOME/xmlsample_cleanup.log
 # *********************************************************************


 # ********************************************************************
 # Drop sample table
 # ********************************************************************
echo "********************************************************* ">> $HOME/xmlsample_cleanup.log
echo "Drop sample table db2ext.testSearch in database $1:          ">> $HOME/xmlsample_cleanup.log
echo "db2 drop table db2ext.testSearch                             ">> $HOME/xmlsample_cleanup.log
db2 "drop table db2ext.testSearch                                  ">> $HOME/xmlsample_cleanup.log

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

echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo "connect reset                    *************************">> $HOME/xmlsample_cleanup.log
echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo db2 connect reset >> $HOME/xmlsample_cleanup.log
db2 connect reset >> $HOME/xmlsample_cleanup.log

echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo "deactivate database $1           *************************">> $HOME/xmlsample_cleanup.log
echo "**********************************************************">> $HOME/xmlsample_cleanup.log
echo db2 deactivate database $1 >> $HOME/xmlsample_cleanup.log
db2 deactivate database $1 >> $HOME/xmlsample_cleanup.log

if [ $DROP_DB = 1 ]
then
 echo "**********************************************************">> $HOME/xmlsample_cleanup.log
 echo "drop database $1           *************************">> $HOME/xmlsample_cleanup.log
 echo "**********************************************************">> $HOME/xmlsample_cleanup.log
 echo db2 drop database $1 >> $HOME/xmlsample_cleanup.log
 db2 drop database $1 >> $HOME/xmlsample_cleanup.log
fi


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

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