#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bosinst/samples/AE/AS/ConfigDB2.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2009 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)32        1.1  src/bos/usr/lpp/bosinst/samples/AE/AS/ConfigDB2.sh, bosinst, bos720 3/29/09 15:20:08

#
# A utility for activating DB2 server with table.
#
# usage: sh ConfigDB2 -localhostname "db2v91" -db2inst1_password "pw4db2" -service_port "55000" -fcm_start_port "60000" -fcm_end_port "60003"

ECHOE="echo -e"
UNAME=`uname -s`;

if [ "$UNAME" = "AIX" ]
then
  ECHOE="echo"
  HostName=`hostname`
  IBM="IBM"

  # Make Asynchronous I/O available on system
  # mksysb capture/restore doesn't seem to either capture or restore this status properly

  lsdev -l aio0 -r status
  AIO_STATUS=`lsdev -l aio0 -r status`
  if [ "$AIO_STATUS" != "Available" ]
  then
      mkdev -l aio0				# change state to Available
      chdev -l aio0 -a autoconfig='available'	# make Available on next boot
  fi
else
  IBM="ibm"
fi

DB2Version=""

while [ $# -ne 0 ]
do
	case $1 in
		-localhostname*)
		HostName=$2
		;;
		-db2inst1_password*)
		InstPassword=$2
		;;
		-db2_version*)
		DB2Version=$2
		;;
		-service_port*)
		ServicPort=$2
		;;
		-fcm_start_port*)
		FcmSPort=$2
		;;
		-fcm_end_port*)
		FcmEPort=$2
		;;
		*)
		;;
	esac
	shift 2
done

if [ "$DB2Version" = "" ]
then
    NUM_DB2s=`ls -1 /opt/$IBM/db2 | wc -l`
    if [ $NUM_DB2s -eq 1 ]
    then
	DB2Version=`ls -1 /opt/$IBM/db2`
    else
	DB2Version="V9.1"
    fi
fi

#update hostname in default.env
/opt/$IBM/db2/$DB2Version/adm/db2set -g DB2SYSTEM=${HostName}

#update hostname in db2nodes.cfg
chmod u+w /home/db2inst1/sqllib/db2nodes.cfg
$ECHOE "0 ${HostName} 0" > /home/db2inst1/sqllib/db2nodes.cfg

#change instance password
if [ "$InstPassword" != "" ]
then
    echo "db2inst1:$InstPassword" | chpasswd
fi

#update service port
if [ "$ServicPort" != "" ]
then
    /opt/$IBM/db2/$DB2Version/instance/db2isrv -remove -s db2c_db2inst1
    /opt/$IBM/db2/$DB2Version/instance/db2isrv -add -s db2c_db2inst1 -n ${ServicPort}
fi

#update fcm port
if [[ "$FcmSPort" != "" && "$FcmEPort" != "" ]]
then
    /opt/$IBM/db2/$DB2Version/instance/db2isrv -remove -s DB2_db2inst1 -remove -s DB2_db2inst1
    /opt/$IBM/db2/$DB2Version/instance/db2isrv -add -s DB2_db2inst1 -n ${FcmSPort} -e ${FcmEPort}
fi

#start db2 instance
su - db2inst1 <<END
db2start
db2sampl
END
